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"

70 lines (68 loc) 2.93 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/union.ts var union_exports = {}; __export(union_exports, { unionValidation: () => unionValidation }); module.exports = __toCommonJS(union_exports); function unionValidation(schemas) { return { name: "union", type: "medium", callback: /* @__PURE__ */ __name(async (value, path, options) => { const parsedValues = { parsed: value, errors: [] }; const startingToInternalBubbleUpLength = options.toInternalToBubbleUp?.length || 0; for (let i = 0; i < schemas.length; i++) { const schemaWithProtected = schemas[i]; const parsedData = await schemaWithProtected.__parse(value, path, options); parsedValues.parsed = parsedData.parsed; if (Array.isArray(parsedData.errors)) if (Array.isArray(parsedValues.errors)) parsedValues.errors.push(...parsedData.errors); else parsedValues.errors = parsedData.errors; const hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors = ( // eslint-disable-next-line ts/no-unnecessary-condition parsedData.errors === void 0 || (parsedData.errors || []).length === 0 ); if (hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors) { return { parsed: parsedValues.parsed, errors: [] }; } else if (startingToInternalBubbleUpLength < (options.toInternalToBubbleUp?.length || 0)) { const numberOfElementsToRemove = (options.toInternalToBubbleUp?.length || 0) - startingToInternalBubbleUpLength; options.toInternalToBubbleUp?.splice(startingToInternalBubbleUpLength, numberOfElementsToRemove); } } return { parsed: parsedValues.parsed, // eslint-disable-next-line ts/no-unnecessary-condition errors: Array.isArray(parsedValues.errors) ? parsedValues.errors : [] }; }, "callback") }; } __name(unionValidation, "unionValidation"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { unionValidation });