@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.66 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/object.ts
var object_exports = {};
__export(object_exports, {
objectValidation: () => objectValidation
});
module.exports = __toCommonJS(object_exports);
function objectValidation(keysToFallback) {
return {
name: "object",
type: "low",
callback: /* @__PURE__ */ __name(async (value, path, options) => {
const isNotAnObject = typeof value !== "object" && Array.isArray(value) === false && value !== null;
if (isNotAnObject) return {
parsed: value,
preventChildValidation: true,
errors: [
{
isValid: false,
code: "object",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: "The value must be an object. Received: " + typeof value
}
]
};
const errors = {};
const toValidateEntries = Object.entries(keysToFallback);
await Promise.all(toValidateEntries.map(async ([key, schema]) => {
const schemaWithProtected = schema;
const { parsed, errors: parseErrors } = await schemaWithProtected.__parse(value[key], [
...path,
key
], options);
if (Array.isArray(parseErrors) && parseErrors.length > 0) errors[key] = parseErrors;
else value[key] = parsed;
if (schemaWithProtected.__toInternal && options.toInternalToBubbleUp) options.toInternalToBubbleUp.push(async () => value[key] = await schema.__toInternal(parsed));
}));
return {
parsed: value,
errors: Object.values(errors).flat()
};
}, "callback")
};
}
__name(objectValidation, "objectValidation");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
objectValidation
});