@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"
256 lines (254 loc) • 7.15 kB
JavaScript
"use strict";
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/string.ts
var string_exports = {};
__export(string_exports, {
email: () => email,
endsWith: () => endsWith,
includes: () => includes,
maxLength: () => maxLength,
minLength: () => minLength,
regex: () => regex,
startsWith: () => startsWith,
stringValidation: () => stringValidation,
uuid: () => uuid
});
module.exports = __toCommonJS(string_exports);
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");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
email,
endsWith,
includes,
maxLength,
minLength,
regex,
startsWith,
stringValidation,
uuid
});