From f30d1e38629e4cac30c2a3b0bff5ab395ce652f1 Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Sat, 9 Apr 2016 16:12:47 -0400 Subject: [PATCH] [fixed] bug in date min/max with ref fixes #40 --- lib/mixed.js | 2 +- src/date.js | 23 +++++++++++++++-------- test/date.js | 44 ++++++++++++++++++++++++++++++++------------ 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/lib/mixed.js b/lib/mixed.js index cc6c12327..a92d4ac4d 100644 --- a/lib/mixed.js +++ b/lib/mixed.js @@ -67,7 +67,7 @@ SchemaType.prototype = { if (arguments.length === 0) return this._meta; var next = this.clone(); - next._meta = _extends(next._meta || {}, _extends({}, obj)); + next._meta = _extends(next._meta || {}, obj); return next; }, withMutation: function withMutation(fn) { diff --git a/src/date.js b/src/date.js index 4dc92d74f..b0bac9872 100644 --- a/src/date.js +++ b/src/date.js @@ -3,6 +3,7 @@ var MixedSchema = require('./mixed') , isoParse = require('./util/isodate') , locale = require('./locale.js').date , isAbsent = require('./util/isAbsent') + , Ref = require('./util/reference') , { isDate, inherits } = require('./util/_'); let invalidDate = new Date('') @@ -32,16 +33,19 @@ inherits(DateSchema, MixedSchema, { }, min(min, msg){ - var limit = this.cast(min); + var limit = min; - if(!this._typeCheck(limit)) - throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date') + if (!Ref.isRef(limit)) { + limit = this.cast(min) + if (!this._typeCheck(limit)) + throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date') + } return this.test({ name: 'min', exclusive: true, message: msg || locale.min, - params: { min: min }, + params: { min }, test(value) { return isAbsent(value) || value >= this.resolve(limit) } @@ -49,16 +53,19 @@ inherits(DateSchema, MixedSchema, { }, max(max, msg){ - var limit = this.cast(max); + var limit = max; - if(!this._typeCheck(limit)) - throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date') + if (!Ref.isRef(limit)) { + limit = this.cast(max) + if (!this._typeCheck(limit)) + throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date') + } return this.test({ name: 'max', exclusive: true, message: msg || locale.max, - params: { max: max }, + params: { max }, test(value) { return isAbsent(value) || value <= this.resolve(limit) } diff --git a/test/date.js b/test/date.js index c44949881..bcaf22d59 100644 --- a/test/date.js +++ b/test/date.js @@ -2,7 +2,7 @@ var chai = require('chai') , chaiAsPromised = require('chai-as-promised') , Promise = require('promise/src/es6-extensions') - , date = require('../src/date'); + , { ref, date } = require('../src'); chai.use(chaiAsPromised); chai.should(); @@ -56,28 +56,48 @@ describe('Date types', function(){ }) it('should check MIN correctly', function(){ - var v = date().min(new Date(2014, 3, 15)); + var min = new Date(2014, 3, 15) + , invalid = new Date(2014, 1, 15) + , valid = new Date(2014, 5, 15) ;(function(){ date().max('hello') }).should.throw(TypeError) + ;(function(){ date().max(ref('$foo')) }).should.not.throw return Promise.all([ - v.isValid(new Date(2014, 5, 15)).should.eventually.equal(true), - v.isValid(new Date(2014, 1, 15)).should.eventually.equal(false), - - v.isValid(null).should.eventually.equal(false) + date().min(min).isValid(valid).should.eventually.equal(true), + date().min(min).isValid(invalid).should.eventually.equal(false), + date().min(min).isValid(null).should.eventually.equal(false), + + date().min(ref('$foo')) + .isValid(valid, { context: { foo: min }}) + .should.eventually.equal(true), + date().min(ref('$foo')) + .isValid(invalid, { context: { foo: min }}) + .should.eventually.equal(false) ]) }) - it('should check MAX correctly', function(){ - var v = date().max(new Date(2014, 7, 15)); + it('should check MAX correctly', function() { + var max = new Date(2014, 7, 15) + , invalid = new Date(2014, 9, 15) + , valid = new Date(2014, 5, 15) ;(function(){ date().max('hello') }).should.throw(TypeError) + ;(function(){ date().max(ref('$foo')) }).should.not.throw return Promise.all([ - v.isValid(new Date(2014, 5, 15)).should.eventually.equal(true), - v.isValid(new Date(2014, 9, 15)).should.eventually.equal(false), - v.nullable(true).isValid(null).should.eventually.equal(true) + date().max(max).isValid(valid).should.eventually.equal(true), + date().max(max).isValid(invalid).should.eventually.equal(false), + date().max(max) + .nullable(true) + .isValid(null).should.eventually.equal(true), + + date().max(ref('$foo')) + .isValid(valid, { context: { foo: max }}) + .should.eventually.equal(true), + date().max(ref('$foo')) + .isValid(invalid, { context: { foo: max }}) + .should.eventually.equal(false) ]) }) }) -