UNPKG

altheia-async-data-validator

Version:

A very simple, fast and customizable async data validator

98 lines (97 loc) 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeDate = exports.messages = void 0; const base_1 = require("./base"); /* eslint-disable */ const iso = new RegExp(/^(?:[-+]\d{2})?(?:\d{4}(?!\d{2}\b))(?:(-?)(?:(?:0[1-9]|1[0-2])(?:\1(?:[12]\d|0[1-9]|3[01]))?|W(?:[0-4]\d|5[0-2])(?:-?[1-7])?|(?:00[1-9]|0[1-9]\d|[12]\d{2}|3(?:[0-5]\d|6[1-6])))(?![T]$|[T][\d]+Z$)(?:[T\s](?:(?:(?:[01]\d|2[0-3])(?:(:?)[0-5]\d)?|24\:?00)(?:[.,]\d+(?!:))?)(?:\2[0-5]\d(?:[.,]\d+)?)?(?:[Z]|(?:[+-])(?:[01]\d|2[0-3])(?::?[0-5]\d)?)?)?)?$/); /* eslint-enable */ exports.messages = { 'date.typeof': (name) => `${name} must be a valid date`, 'date.iso': (name) => `${name} must be an ISO-8601 date`, 'date.min': (name, { min }) => `${name} must be at least ${min.toISOString()}`, 'date.max': (name, { max }) => `${name} must be less than or equal to ${max.toISOString()}`, }; /** * Date class */ class TypeDate extends base_1.TypeBase { /** * Constructor */ constructor() { super(); this.name = 'date'; this.typeof(); } /** * Try to cast value * * @param {mixed} value * @return {Date|mixed} */ _cast(value) { return Date.parse(value); } /** * Test to validate the type of the value * * @return {this} */ typeof() { this.test('typeof', (val) => { return !isNaN(Date.parse(val)); }); return this; } /** * Force a date to be a valid ISO-8601. * * @return {this} */ iso() { this.test('iso', (date) => { return date.match(iso) !== null; }); return this; } /** * Force a date to be a at least or bigger than value passed * * @param {Date} min * @return {this} */ min(min) { this.test('min', (date) => { if (typeof min !== 'object' || min.constructor.name !== 'Date' || min.toString() === 'Invalid Date') { return false; } return Date.parse(date) >= min.getTime(); }, { min }); return this; } /** * Force a date to be less or equal than value passed * * @param {Date} max * @return {this} */ max(max) { this.test('max', (date) => { if (typeof max !== 'object' || max.constructor.name !== 'Date' || max.toString() === 'Invalid Date') { return false; } return Date.parse(date) <= max.getTime(); }, { max }); return this; } } exports.TypeDate = TypeDate; const def = { Class: TypeDate, messages: exports.messages, }; exports.default = def;