@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"
225 lines (223 loc) • 5.95 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/validators/string.ts
function stringValidation() {
return {
name: "string",
type: "medium",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
return {
parsed: value,
errors: typeof value === "string" ? [] : [
{
isValid: typeof value === "string",
code: "string",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: "The value must be a string. Received: " + typeof value
}
]
};
}, "callback")
};
}
__name(stringValidation, "stringValidation");
function maxLength(args) {
return {
name: "maxLength",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = 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 minLength(args) {
return {
name: "minLength",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = 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 endsWith(args) {
return {
name: "endsWith",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = value.endsWith(args.value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "endsWith",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(endsWith, "endsWith");
function startsWith(args) {
return {
name: "startsWith",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = value.startsWith(args.value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "startsWith",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(startsWith, "startsWith");
function includes(args) {
return {
name: "includes",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = value.includes(args.value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "includes",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(includes, "includes");
function regex(args) {
return {
name: "regex",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = args.value.test(value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "regex",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(regex, "regex");
function uuid(args) {
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
return {
name: "uuid",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = uuidRegex.test(value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "uuid",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(uuid, "uuid");
function email(args) {
const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
console.log("aquiiiiii");
return {
name: "email",
type: "low",
// eslint-disable-next-line ts/require-await
callback: /* @__PURE__ */ __name(async (value, path, _options) => {
const isValid = emailRegex.test(value);
return {
parsed: value,
errors: isValid ? [] : [
{
isValid: false,
code: "email",
// eslint-disable-next-line ts/no-unnecessary-condition
path: path || [],
message: args.message
}
]
};
}, "callback")
};
}
__name(email, "email");
export {
email,
endsWith,
includes,
maxLength,
minLength,
regex,
startsWith,
stringValidation,
uuid
};