UNPKG

2.53 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/builtin/interopRequireDefault");
4
5exports.__esModule = true;
6exports.default = void 0;
7
8var _mixed = _interopRequireDefault(require("./mixed"));
9
10var _inherits = _interopRequireDefault(require("./util/inherits"));
11
12var _isodate = _interopRequireDefault(require("./util/isodate"));
13
14var _locale = require("./locale");
15
16var _isAbsent = _interopRequireDefault(require("./util/isAbsent"));
17
18var _Reference = _interopRequireDefault(require("./Reference"));
19
20var invalidDate = new Date('');
21
22var isDate = function isDate(obj) {
23 return Object.prototype.toString.call(obj) === '[object Date]';
24};
25
26var _default = DateSchema;
27exports.default = _default;
28
29function DateSchema() {
30 var _this = this;
31
32 if (!(this instanceof DateSchema)) return new DateSchema();
33
34 _mixed.default.call(this, {
35 type: 'date'
36 });
37
38 this.withMutation(function () {
39 _this.transform(function (value) {
40 if (this.isType(value)) return isDate(value) ? new Date(value) : value;
41 value = (0, _isodate.default)(value);
42 return value ? new Date(value) : invalidDate;
43 });
44 });
45}
46
47(0, _inherits.default)(DateSchema, _mixed.default, {
48 _typeCheck: function _typeCheck(v) {
49 return isDate(v) && !isNaN(v.getTime());
50 },
51 min: function min(_min, message) {
52 if (message === void 0) {
53 message = _locale.date.min;
54 }
55
56 var limit = _min;
57
58 if (!_Reference.default.isRef(limit)) {
59 limit = this.cast(_min);
60 if (!this._typeCheck(limit)) throw new TypeError('`min` must be a Date or a value that can be `cast()` to a Date');
61 }
62
63 return this.test({
64 message: message,
65 name: 'min',
66 exclusive: true,
67 params: {
68 min: _min
69 },
70 test: function test(value) {
71 return (0, _isAbsent.default)(value) || value >= this.resolve(limit);
72 }
73 });
74 },
75 max: function max(_max, message) {
76 if (message === void 0) {
77 message = _locale.date.max;
78 }
79
80 var limit = _max;
81
82 if (!_Reference.default.isRef(limit)) {
83 limit = this.cast(_max);
84 if (!this._typeCheck(limit)) throw new TypeError('`max` must be a Date or a value that can be `cast()` to a Date');
85 }
86
87 return this.test({
88 message: message,
89 name: 'max',
90 exclusive: true,
91 params: {
92 max: _max
93 },
94 test: function test(value) {
95 return (0, _isAbsent.default)(value) || value <= this.resolve(limit);
96 }
97 });
98 }
99});
100module.exports = exports["default"];
\No newline at end of file