@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"
177 lines (175 loc) • 6 kB
JavaScript
;
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/array.ts
var array_exports = {};
__export(array_exports, {
arrayValidation: () => arrayValidation,
maxLength: () => maxLength,
minLength: () => minLength,
nonEmpty: () => nonEmpty
});
module.exports = __toCommonJS(array_exports);
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");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
arrayValidation,
maxLength,
minLength,
nonEmpty
});