UNPKG

ottoman

Version:
110 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dateTypeFactory = exports.DateType = void 0; const core_type_1 = require("./core-type"); const helpers_1 = require("../helpers"); const errors_1 = require("../errors"); const utils_1 = require("../../utils"); const cast_strategy_1 = require("../../utils/cast-strategy"); const type_helpers_1 = require("../../utils/type-helpers"); /** * `Date` are plain JavaScript Date. * * ## Options * * - **required** flag to define if the field is mandatory * - **validator** that will be applied to the field a validation function, validation object or string with the name of the custom validator * - **default** that will define the initial value of the field, this option allows a value or a function * - **immutable** that will define this field as immutable. Ottoman prevents you from changing immutable fields if the schema as configure like strict * - **min** minimum date value that will be accepted * - **max** maximum date value that will be accepted * * @example * ```typescript * const userSchema = new Schema({ * birthday: { type: Date, min: '1990-12-31', max: new Date() }, * hired: Schema.Types.Date * }) * ``` */ class DateType extends core_type_1.CoreType { constructor(name, options) { super(name, DateType.sName, options); } get min() { const _min = this.options.min; if (typeof _min === 'string') { return new Date(String(_min)); } return _min; } get max() { const _max = this.options.max; if (typeof _max === 'string') { return new Date(String(_max)); } return _max; } buildDefault() { const result = super.buildDefault(); if (result) { return !(result instanceof Date) ? new Date(String(result)) : result; } return result; } cast(value, strategy = cast_strategy_1.CAST_STRATEGY.DEFAULT_OR_DROP) { if ((0, type_helpers_1.isDateValid)(value)) { return new Date(value); } else { return (0, cast_strategy_1.checkCastStrategy)(value, strategy, this); } } validate(value, strategy) { value = super.validate(value, strategy); if (this.isEmpty(value)) return value; const _value = this.isStrictStrategy(strategy) ? (0, utils_1.is)(value, Date) ? value : undefined : (0, utils_1.is)(value, Date) ? value : (0, utils_1.is)(value, String) ? new Date(String(value)) : (0, utils_1.is)(value, Number) ? new Date(Number(value)) : undefined; if (_value === undefined) { throw new errors_1.ValidationError(`Property '${this.name}' must be of type '${this.typeName}'`); } this.checkValidator(_value); let errors = []; errors.push(this._checkMinDate(_value)); errors.push(this._checkMaxDate(_value)); errors = errors.filter((e) => e !== ''); if (errors.length > 0) { throw new errors_1.ValidationError(errors.join('\n')); } return _value; } _checkMinDate(val) { const _min = typeof this.min === 'function' ? this.min() : this.min; if (_min === undefined) { return ''; } return (0, helpers_1.validateMinDate)(val, _min, this.name) || ''; } _checkMaxDate(val) { const _max = typeof this.max === 'function' ? this.max() : this.max; if (_max === undefined) { return ''; } return (0, helpers_1.validateMaxDate)(val, _max, this.name) || ''; } } exports.DateType = DateType; DateType.sName = Date.name; const dateTypeFactory = (name, opts) => new DateType(name, opts); exports.dateTypeFactory = dateTypeFactory; //# sourceMappingURL=date-type.js.map