Skip to content

Commit

Permalink
[fixed] bug in date min/max with ref
Browse files Browse the repository at this point in the history
fixes #40
  • Loading branch information
jquense committed Apr 9, 2016
1 parent 111ad90 commit f30d1e3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 15 additions & 8 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
Expand Down Expand Up @@ -32,33 +33,39 @@ 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)
}
})
},

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)
}
Expand Down
44 changes: 32 additions & 12 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
])
})
})

0 comments on commit f30d1e3

Please sign in to comment.