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"

195 lines (193 loc) 5.17 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/number.ts var number_exports = {}; __export(number_exports, { decimalPlaces: () => decimalPlaces, integer: () => integer, max: () => max, maxDigits: () => maxDigits, min: () => min, numberValidation: () => numberValidation }); module.exports = __toCommonJS(number_exports); function numberValidation() { return { name: "number", type: "medium", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { return { parsed: value, errors: [ { isValid: typeof value === "number", code: "number", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: "The value must be a number. Received: " + typeof value } ] }; }, "callback") }; } __name(numberValidation, "numberValidation"); function max(args) { return { name: "max", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { if (args.inclusive) return { parsed: value, errors: [ { isValid: value <= args.value, code: "max", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; return { parsed: value, errors: [ { isValid: value < args.value, code: "max", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(max, "max"); function min(args) { return { name: "min", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path) => { if (args.inclusive) return { parsed: value, errors: [ { isValid: value >= args.value, message: args.message, code: "min", path: path || [] } ] }; return { parsed: value, errors: [ { isValid: value > args.value, message: args.message, code: "min", path: path || [] } ] }; }, "callback") }; } __name(min, "min"); function integer(args) { return { name: "integer", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path) => { const isValid = Number.isInteger(value); return { parsed: value, errors: isValid ? [] : [ { isValid, message: args.message, code: "integer", path: path || [] } ] }; }, "callback") }; } __name(integer, "integer"); function maxDigits(args) { return { name: "maxDigits", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path) => { const isValid = value.toString().replace(".", "").length <= args.value; return { parsed: value, errors: isValid ? [] : [ { isValid, message: args.message, code: "maxDigits", path: path || [] } ] }; }, "callback") }; } __name(maxDigits, "maxDigits"); function decimalPlaces(args) { return { name: "decimalPlaces", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path) => { const isValid = value.toString().split(".")[1]?.length <= args.value; return { parsed: value, errors: isValid ? [] : [ { isValid, message: args.message, code: "decimalPlaces", path: path || [] } ] }; }, "callback") }; } __name(decimalPlaces, "decimalPlaces"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { decimalPlaces, integer, max, maxDigits, min, numberValidation });