@bshg/validation
Version:
Validation Library for TypeScript projects
96 lines (95 loc) • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Times = void 0;
const base_1 = require("./base");
const messages_1 = require("../messages");
const msgs = () => messages_1.CurrentLocalize.time;
const toTimestamp = (date) => new Date(0, 0, 0, date.getHours(), date.getMinutes(), date.getSeconds()).valueOf();
const toFullTimestamp = (date) => new Date(date).valueOf();
class Times extends base_1.TypeValidator {
undefined() {
return new Times();
}
required(options) {
return this.useCostume({
error: value => value === undefined,
message: msgs().required,
options,
});
}
equals(time, options) {
return this.useCostume({
error: value => value !== undefined && toTimestamp(value) != toTimestamp(time),
message: msgs().equals,
options: options,
args: [time.toISOString()],
});
}
after(time, options) {
return this.useCostume({
error: value => value !== undefined && toTimestamp(value) <= toTimestamp(time),
message: msgs().after,
options,
args: [time.toTimeString()],
});
}
before(time, options) {
return this.useCostume({
error: value => value !== undefined && toTimestamp(value) >= toTimestamp(time),
message: msgs().before,
options,
args: [time.toTimeString()],
});
}
between(start, end, options) {
return this.useCostume({
error: value => value !== undefined && (toTimestamp(value) < toTimestamp(start) || toTimestamp(value) > toTimestamp(end)),
message: msgs().between,
options,
args: [start.toTimeString(), end.toTimeString()],
});
}
nowOrAfter(options) {
const now = new Date();
return this.useCostume({
error: value => value !== undefined && toTimestamp(value) <= toTimestamp(now),
message: msgs().nowOrAfter,
options,
});
}
nowOrBefore(options) {
const now = new Date();
return this.useCostume({
error: value => value !== undefined && toTimestamp(value) >= toTimestamp(now),
message: msgs().nowOrBefore,
options,
});
}
past(options) {
const now = new Date();
return this.after(now, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().past });
}
future(options) {
const now = new Date();
return this.before(now, { ...options, message: (options === null || options === void 0 ? void 0 : options.message) || msgs().future });
}
within24Hours(options) {
const now = new Date();
const _24HoursFromNow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
return this.useCostume({
error: value => value !== undefined && (toFullTimestamp(value) < toFullTimestamp(now) ||
toFullTimestamp(value) > toFullTimestamp(_24HoursFromNow)),
message: msgs().within24Hours,
options,
});
}
as(key, options) {
return this.useCostume({
error: (value, parent) => value != undefined && toTimestamp(value) != toTimestamp(parent[key]),
message: msgs().as,
options: options,
args: [key],
});
}
}
exports.Times = Times;