@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"
167 lines (165 loc) • 4.06 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/validators/number.ts
function numberValidation() {
return {
name: "number",
type: "medium",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
return {
parsed: value,
errors: [
{
isValid: typeof value === "number",
code: "number",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: "The value must be a number. Received: " + typeof value
}
]
};
}, "callback")
};
}
__name(numberValidation, "numberValidation");
function max(args) {
return {
name: "max",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
if (args.inclusive) return {
parsed: value,
errors: [
{
isValid: value <= args.value,
code: "max",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
return {
parsed: value,
errors: [
{
isValid: value < args.value,
code: "max",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(max, "max");
function min(args) {
return {
name: "min",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path) => {
if (args.inclusive) return {
parsed: value,
errors: [
{
isValid: value >= args.value,
message: args.message,
code: "min",
path: path || []
}
]
};
return {
parsed: value,
errors: [
{
isValid: value > args.value,
message: args.message,
code: "min",
path: path || []
}
]
};
}, "callback")
};
}
__name(min, "min");
function integer(args) {
return {
name: "integer",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path) => {
const isValid = Number.isInteger(value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid,
message: args.message,
code: "integer",
path: path || []
}
]
};
}, "callback")
};
}
__name(integer, "integer");
function maxDigits(args) {
return {
name: "maxDigits",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path) => {
const isValid = value.toString().replace(".", "").length <= args.value;
return {
parsed: value,
errors: isValid ? [] : [
{
isValid,
message: args.message,
code: "maxDigits",
path: path || []
}
]
};
}, "callback")
};
}
__name(maxDigits, "maxDigits");
function decimalPlaces(args) {
return {
name: "decimalPlaces",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path) => {
const isValid = value.toString().split(".")[1]?.length <= args.value;
return {
parsed: value,
errors: isValid ? [] : [
{
isValid,
message: args.message,
code: "decimalPlaces",
path: path || []
}
]
};
}, "callback")
};
}
__name(decimalPlaces, "decimalPlaces");
export {
decimalPlaces,
integer,
max,
maxDigits,
min,
numberValidation
};