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"

151 lines (149 loc) 4.93 kB
var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); // src/validators/array.ts function arrayValidation(isTuple, schemas) { return { name: "array", type: "medium", callback: /* @__PURE__ */ __name(async (value, path, options) => { const isNotAnArray = Array.isArray(value) === false; if (isNotAnArray) return { parsed: value, preventChildValidation: true, errors: [ { isValid: false, code: "array", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: "The value must be an array. Received: " + typeof value } ] }; if (isTuple && value.length !== schemas.length) return { parsed: value, preventChildValidation: true, errors: [ { isValid: false, code: "tuple", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: "The tuple must have exactly " + schemas.length + " elements. Received: " + value.length } ] }; const errorsOfArray = []; const schemaIndexByTypeof = /* @__PURE__ */ new Map(); let parsedValues = []; parsedValues = await Promise.all(value.map(async (element, index) => { let errorsToAppendAfterLoopIfNoSchemaMatched = []; const typeofElement = typeof element; const schemaIndex = schemaIndexByTypeof.get(typeofElement); const existsASchemaIndex = typeof schemaIndex === "number"; const schemasToValidateAgainst = isTuple ? [ schemas[index] ] : existsASchemaIndex ? [ schemas[schemaIndex] ] : schemas; for (let indexOfSchema = 0; indexOfSchema < schemasToValidateAgainst.length; indexOfSchema++) { const schemaToValidate = schemasToValidateAgainst[indexOfSchema]; const schemaWithProtected = schemaToValidate; const { parsed, errors } = await schemaWithProtected.__parse(element, [ ...path, index ], options); if (schemaWithProtected.__toInternal && options.toInternalToBubbleUp) options.toInternalToBubbleUp.push(async () => parsedValues[indexOfSchema] = await schemaWithProtected.__toInternal?.(parsed)); if ((errors || []).length <= 0) { errorsToAppendAfterLoopIfNoSchemaMatched = []; schemaIndexByTypeof.set(typeofElement, indexOfSchema); return parsed; } errorsToAppendAfterLoopIfNoSchemaMatched.push(...errors); } errorsOfArray.push(...errorsToAppendAfterLoopIfNoSchemaMatched); return element; })); return { parsed: parsedValues, errors: errorsOfArray }; }, "callback") }; } __name(arrayValidation, "arrayValidation"); function minLength(args) { return { name: "minLength", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = args.inclusive ? value.length >= args.value : value.length > args.value; return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "minLength", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(minLength, "minLength"); function maxLength(args) { return { name: "maxLength", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = args.inclusive ? value.length <= args.value : value.length < args.value; return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "maxLength", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(maxLength, "maxLength"); function nonEmpty(args) { return { name: "nonEmpty", type: "low", // eslint-disable-next-line ts/require-await callback: /* @__PURE__ */ __name(async (value, path, _options) => { const isValid = value.length > 0; return { parsed: value, errors: isValid ? [] : [ { isValid: false, code: "nonEmpty", // eslint-disable-next-line ts/no-unnecessary-condition path: path || [], message: args.message } ] }; }, "callback") }; } __name(nonEmpty, "nonEmpty"); export { arrayValidation, maxLength, minLength, nonEmpty };