UNPKG

@bshg/validation

Version:

Validation Library for TypeScript projects

118 lines (117 loc) 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dates = void 0; const base_1 = require("./base"); const messages_1 = require("../messages"); const msgs = () => messages_1.CurrentLocalize.date; const toDate = (date) => date.toISOString().slice(0, 10); const toTimestamp = (date) => new Date(date.getFullYear(), date.getMonth(), date.getDate()).valueOf(); class Dates extends base_1.TypeValidator { undefined() { return new Dates(); } required(options) { return this.useCostume({ error: value => value === undefined, message: msgs().required, options, }); } equals(dateTime, options) { return this.useCostume({ error: value => value !== undefined && toTimestamp(value) != toTimestamp(dateTime), message: msgs().equals, options: options, args: [toDate(dateTime)], }); } after(date, options) { return this.useCostume({ error: value => value !== undefined && toTimestamp(value) <= toTimestamp(date), message: msgs().after, options: options, args: [toDate(date)], }); } before(date, options) { return this.useCostume({ error: value => value !== undefined && toTimestamp(value) >= toTimestamp(date), message: msgs().before, options: options, args: [toDate(date)], }); } between(start, end, options) { return this.useCostume({ error: value => value !== undefined && (toTimestamp(value) < toTimestamp(start) || toTimestamp(value) > toTimestamp(end)), message: msgs().between, options: options, args: [toDate(start), toDate(end)], }); } todayOrAfter(options) { const today = new Date(); today.setHours(0, 0, 0, 0); return this.useCostume({ error: value => value !== undefined && toTimestamp(value) < toTimestamp(today), message: msgs().todayOrAfter, options: options, }); } todayOrBefore(options) { const today = new Date(); today.setHours(23, 59, 59, 999); return this.useCostume({ error: value => value !== undefined && toTimestamp(value) > toTimestamp(today), message: msgs().todayOrBefore, options: options, }); } past(options) { return this.before(new Date(), { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().past }); } future(options) { return this.after(new Date(), { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().future }); } weekday(options) { return this.useCostume({ error: value => value !== undefined && value.getDay() % 6 === 0, // 0 or 6 represent Sunday and Saturday message: msgs().weekday, options: options, }); } weekend(options) { return this.useCostume({ error: value => value !== undefined && value.getDay() % 6 !== 0, // 1-5 represent Monday to Friday message: msgs().weekend, options: options, }); } leapYear(options) { return this.useCostume({ error: value => value !== undefined && (value.getFullYear() % 4 === 0 && value.getFullYear() % 100 !== 0 || value.getFullYear() % 400 === 0), message: msgs().leapYear, options: options, }); } sameDayAs(date, options) { return this.useCostume({ error: value => value !== undefined && toTimestamp(value) !== toTimestamp(date), message: msgs().sameDayAs, options: options, args: [toDate(date)], }); } //////////////////////////////////////////// as(key, options) { return this.useCostume({ error: (value, parent) => value != undefined && toTimestamp(value) != toTimestamp(parent[key]), message: msgs().as, options: options, args: [key], }); } } exports.Dates = Dates;