102 lines
2.3 KiB
JavaScript
102 lines
2.3 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.create = create;
|
|
exports.default = void 0;
|
|
|
|
var _isodate = _interopRequireDefault(require("./util/isodate"));
|
|
|
|
var _locale = require("./locale");
|
|
|
|
var _isAbsent = _interopRequireDefault(require("./util/isAbsent"));
|
|
|
|
var _Reference = _interopRequireDefault(require("./Reference"));
|
|
|
|
var _schema = _interopRequireDefault(require("./schema"));
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
// @ts-ignore
|
|
let invalidDate = new Date('');
|
|
|
|
let isDate = obj => Object.prototype.toString.call(obj) === '[object Date]';
|
|
|
|
function create() {
|
|
return new DateSchema();
|
|
}
|
|
|
|
class DateSchema extends _schema.default {
|
|
constructor() {
|
|
super({
|
|
type: 'date'
|
|
});
|
|
this.withMutation(() => {
|
|
this.transform(function (value) {
|
|
if (this.isType(value)) return value;
|
|
value = (0, _isodate.default)(value); // 0 is a valid timestamp equivalent to 1970-01-01T00:00:00Z(unix epoch) or before.
|
|
|
|
return !isNaN(value) ? new Date(value) : invalidDate;
|
|
});
|
|
});
|
|
}
|
|
|
|
_typeCheck(v) {
|
|
return isDate(v) && !isNaN(v.getTime());
|
|
}
|
|
|
|
prepareParam(ref, name) {
|
|
let param;
|
|
|
|
if (!_Reference.default.isRef(ref)) {
|
|
let cast = this.cast(ref);
|
|
if (!this._typeCheck(cast)) throw new TypeError(`\`${name}\` must be a Date or a value that can be \`cast()\` to a Date`);
|
|
param = cast;
|
|
} else {
|
|
param = ref;
|
|
}
|
|
|
|
return param;
|
|
}
|
|
|
|
min(min, message = _locale.date.min) {
|
|
let limit = this.prepareParam(min, 'min');
|
|
return this.test({
|
|
message,
|
|
name: 'min',
|
|
exclusive: true,
|
|
params: {
|
|
min
|
|
},
|
|
|
|
test(value) {
|
|
return (0, _isAbsent.default)(value) || value >= this.resolve(limit);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
max(max, message = _locale.date.max) {
|
|
var limit = this.prepareParam(max, 'max');
|
|
return this.test({
|
|
message,
|
|
name: 'max',
|
|
exclusive: true,
|
|
params: {
|
|
max
|
|
},
|
|
|
|
test(value) {
|
|
return (0, _isAbsent.default)(value) || value <= this.resolve(limit);
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
exports.default = DateSchema;
|
|
DateSchema.INVALID_DATE = invalidDate;
|
|
create.prototype = DateSchema.prototype;
|
|
create.INVALID_DATE = invalidDate; |