gemi-lib-hooks
Version:
Validadores de tipo para React.
168 lines (164 loc) • 4.81 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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);
// index.ts
var index_exports = {};
__export(index_exports, {
Validadores: () => Validadores_default
});
module.exports = __toCommonJS(index_exports);
// hooks/usePropTypes/validadores/Validadores_String.tsx
function Build_Validadores_String() {
const validar = (valor) => {
if (typeof valor !== "string") {
return "Error: El valor proporcionado no es un string.";
}
return true;
};
function chainable(fn) {
return Object.assign(fn, {
required: validar.required,
min: validar.min,
max: validar.max,
exact: validar.exact,
onlyLetters: validar.onlyLetters,
onlyNumbers: validar.onlyNumbers,
alphanumeric: validar.alphanumeric,
regex: validar.regex
});
}
validar.required = (mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
if (!valor) {
if (mensaje) {
return mensaje;
}
return "Error: Este campo es requerido.";
}
return true;
});
};
validar.min = (longitud, mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (str.length < longitud) {
if (mensaje) {
return mensaje;
}
return `Error: Debe tener al menos ${longitud} caracteres.`;
}
return true;
});
};
validar.max = (longitud, mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (str.length > longitud) {
if (mensaje) {
return mensaje;
}
return `Error: No puede superar los ${longitud} caracteres.`;
}
return true;
});
};
validar.exact = (longitud, mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (str.length !== longitud) {
if (mensaje) {
return mensaje;
}
return `Error: Debe tener exactamente ${longitud} caracteres.`;
}
return true;
});
};
validar.onlyLetters = (mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (!/^[a-zA-Z]+$/.test(str)) {
if (mensaje) {
return mensaje;
}
return "Error: Solo se permiten letras.";
}
return true;
});
};
validar.onlyNumbers = (mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (!/^[0-9]+$/.test(str)) {
if (mensaje) {
return mensaje;
}
return "Error: Solo se permiten n\xFAmeros.";
}
return true;
});
};
validar.alphanumeric = (mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (!/^[a-zA-Z0-9]+$/.test(str)) {
if (mensaje) {
return mensaje;
}
return "Error: Solo se permiten letras y n\xFAmeros.";
}
return true;
});
};
validar.regex = (expresion, mensaje) => {
return chainable((valor) => {
const result = validar(valor);
if (typeof result === "string") return result;
const str = valor;
if (!expresion.test(str)) {
return mensaje;
}
return true;
});
};
return validar;
}
var Validadores_String = Build_Validadores_String();
// hooks/usePropTypes/validadores/Validadores.tsx
var Validadores = {
string: Validadores_String
};
var Validadores_default = Validadores;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Validadores
});