UNPKG

@palmares/schemas

Version:

This defines a default schema definition for validation of data, it abstract popular schema validation libraries like zod, yup, valibot and others"

130 lines (128 loc) 3.85 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/validators/datetime.ts var datetime_exports = {}; __export(datetime_exports, { above: () => above, allowStringParser: () => allowStringParser, below: () => below, datetimeValidation: () => datetimeValidation }); module.exports = __toCommonJS(datetime_exports); function datetimeValidation() { return { name: "datetime", type: "medium", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = value instanceof Date && !isNaN(value.getTime()); return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "datetime", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: "Value is not a date" } ], preventChildValidation: true }; }, "callback") }; } __name(datetimeValidation, "datetimeValidation"); function allowStringParser() { return { name: "allowString", type: "high", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, _path, _options) => { if (typeof value === "string") { const parsed = new Date(value); if (parsed instanceof Date && !isNaN(parsed.getTime())) { return { parsed, errors: [] }; } } return { parsed: value, errors: [] }; }, "callback") }; } __name(allowStringParser, "allowStringParser"); function below(args) { return { name: "below", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = args.inclusive ? value <= args.value : value < args.value; return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "below", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(below, "below"); function above(args) { return { name: "above", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = args.inclusive ? value <= args.value : value < args.value; return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "above", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(above, "above"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { above, allowStringParser, below, datetimeValidation });