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"

47 lines (45 loc) 1.95 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/validators/union.ts 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"); export { unionValidation };