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"

104 lines (102 loc) 2.77 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/validators/datetime.ts 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"); export { above, allowStringParser, below, datetimeValidation };