Skip to content

Commit f30d1e3

Browse files
committed
[fixed] bug in date min/max with ref
fixes #40
1 parent 111ad90 commit f30d1e3

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

lib/mixed.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ SchemaType.prototype = {
6767
if (arguments.length === 0) return this._meta;
6868

6969
var next = this.clone();
70-
next._meta = _extends(next._meta || {}, _extends({}, obj));
70+
next._meta = _extends(next._meta || {}, obj);
7171
return next;
7272
},
7373
withMutation: function withMutation(fn) {

src/date.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var MixedSchema = require('./mixed')
33
, isoParse = require('./util/isodate')
44
, locale = require('./locale.js').date
55
, isAbsent = require('./util/isAbsent')
6+
, Ref = require('./util/reference')
67
, { isDate, inherits } = require('./util/_');
78

89
let invalidDate = new Date('')
@@ -32,33 +33,39 @@ inherits(DateSchema, MixedSchema, {
3233
},
3334

3435
min(min, msg){
35-
var limit = this.cast(min);
36+
var limit = min;
3637

37-
if(!this._typeCheck(limit))
38-
throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date')
38+
if (!Ref.isRef(limit)) {
39+
limit = this.cast(min)
40+
if (!this._typeCheck(limit))
41+
throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date')
42+
}
3943

4044
return this.test({
4145
name: 'min',
4246
exclusive: true,
4347
message: msg || locale.min,
44-
params: { min: min },
48+
params: { min },
4549
test(value) {
4650
return isAbsent(value) || value >= this.resolve(limit)
4751
}
4852
})
4953
},
5054

5155
max(max, msg){
52-
var limit = this.cast(max);
56+
var limit = max;
5357

54-
if(!this._typeCheck(limit))
55-
throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date')
58+
if (!Ref.isRef(limit)) {
59+
limit = this.cast(max)
60+
if (!this._typeCheck(limit))
61+
throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date')
62+
}
5663

5764
return this.test({
5865
name: 'max',
5966
exclusive: true,
6067
message: msg || locale.max,
61-
params: { max: max },
68+
params: { max },
6269
test(value) {
6370
return isAbsent(value) || value <= this.resolve(limit)
6471
}

test/date.js

+32-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var chai = require('chai')
33
, chaiAsPromised = require('chai-as-promised')
44
, Promise = require('promise/src/es6-extensions')
5-
, date = require('../src/date');
5+
, { ref, date } = require('../src');
66

77
chai.use(chaiAsPromised);
88
chai.should();
@@ -56,28 +56,48 @@ describe('Date types', function(){
5656
})
5757

5858
it('should check MIN correctly', function(){
59-
var v = date().min(new Date(2014, 3, 15));
59+
var min = new Date(2014, 3, 15)
60+
, invalid = new Date(2014, 1, 15)
61+
, valid = new Date(2014, 5, 15)
6062

6163
;(function(){ date().max('hello') }).should.throw(TypeError)
64+
;(function(){ date().max(ref('$foo')) }).should.not.throw
6265

6366
return Promise.all([
64-
v.isValid(new Date(2014, 5, 15)).should.eventually.equal(true),
65-
v.isValid(new Date(2014, 1, 15)).should.eventually.equal(false),
66-
67-
v.isValid(null).should.eventually.equal(false)
67+
date().min(min).isValid(valid).should.eventually.equal(true),
68+
date().min(min).isValid(invalid).should.eventually.equal(false),
69+
date().min(min).isValid(null).should.eventually.equal(false),
70+
71+
date().min(ref('$foo'))
72+
.isValid(valid, { context: { foo: min }})
73+
.should.eventually.equal(true),
74+
date().min(ref('$foo'))
75+
.isValid(invalid, { context: { foo: min }})
76+
.should.eventually.equal(false)
6877
])
6978
})
7079

71-
it('should check MAX correctly', function(){
72-
var v = date().max(new Date(2014, 7, 15));
80+
it('should check MAX correctly', function() {
81+
var max = new Date(2014, 7, 15)
82+
, invalid = new Date(2014, 9, 15)
83+
, valid = new Date(2014, 5, 15)
7384

7485
;(function(){ date().max('hello') }).should.throw(TypeError)
86+
;(function(){ date().max(ref('$foo')) }).should.not.throw
7587

7688
return Promise.all([
77-
v.isValid(new Date(2014, 5, 15)).should.eventually.equal(true),
78-
v.isValid(new Date(2014, 9, 15)).should.eventually.equal(false),
79-
v.nullable(true).isValid(null).should.eventually.equal(true)
89+
date().max(max).isValid(valid).should.eventually.equal(true),
90+
date().max(max).isValid(invalid).should.eventually.equal(false),
91+
date().max(max)
92+
.nullable(true)
93+
.isValid(null).should.eventually.equal(true),
94+
95+
date().max(ref('$foo'))
96+
.isValid(valid, { context: { foo: max }})
97+
.should.eventually.equal(true),
98+
date().max(ref('$foo'))
99+
.isValid(invalid, { context: { foo: max }})
100+
.should.eventually.equal(false)
80101
])
81102
})
82103
})
83-

0 commit comments

Comments
 (0)