1 | import MixedSchema from './mixed';
|
2 | import inherits from './util/inherits';
|
3 | import isoParse from './util/isodate';
|
4 | import { date as locale } from './locale';
|
5 | import isAbsent from './util/isAbsent';
|
6 | import Ref from './Reference';
|
7 | var invalidDate = new Date('');
|
8 |
|
9 | var isDate = function isDate(obj) {
|
10 | return Object.prototype.toString.call(obj) === '[object Date]';
|
11 | };
|
12 |
|
13 | export default DateSchema;
|
14 |
|
15 | function DateSchema() {
|
16 | var _this = this;
|
17 |
|
18 | if (!(this instanceof DateSchema)) return new DateSchema();
|
19 | MixedSchema.call(this, {
|
20 | type: 'date'
|
21 | });
|
22 | this.withMutation(function () {
|
23 | _this.transform(function (value) {
|
24 | if (this.isType(value)) return value;
|
25 | value = isoParse(value);
|
26 |
|
27 | return !isNaN(value) ? new Date(value) : invalidDate;
|
28 | });
|
29 | });
|
30 | }
|
31 |
|
32 | inherits(DateSchema, MixedSchema, {
|
33 | _typeCheck: function _typeCheck(v) {
|
34 | return isDate(v) && !isNaN(v.getTime());
|
35 | },
|
36 | min: function min(_min, message) {
|
37 | if (message === void 0) {
|
38 | message = locale.min;
|
39 | }
|
40 |
|
41 | var limit = _min;
|
42 |
|
43 | if (!Ref.isRef(limit)) {
|
44 | limit = this.cast(_min);
|
45 | if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
|
46 | }
|
47 |
|
48 | return this.test({
|
49 | message: message,
|
50 | name: 'min',
|
51 | exclusive: true,
|
52 | params: {
|
53 | min: _min
|
54 | },
|
55 | test: function test(value) {
|
56 | return isAbsent(value) || value >= this.resolve(limit);
|
57 | }
|
58 | });
|
59 | },
|
60 | max: function max(_max, message) {
|
61 | if (message === void 0) {
|
62 | message = locale.max;
|
63 | }
|
64 |
|
65 | var limit = _max;
|
66 |
|
67 | if (!Ref.isRef(limit)) {
|
68 | limit = this.cast(_max);
|
69 | if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
|
70 | }
|
71 |
|
72 | return this.test({
|
73 | message: message,
|
74 | name: 'max',
|
75 | exclusive: true,
|
76 | params: {
|
77 | max: _max
|
78 | },
|
79 | test: function test(value) {
|
80 | return isAbsent(value) || value <= this.resolve(limit);
|
81 | }
|
82 | });
|
83 | }
|
84 | }); |
\ | No newline at end of file |