UNPKG

validation-box

Version:

The only validation library - with flexible regex - you need.

176 lines (173 loc) 6.51 kB
"use strict"; 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); // src/validators/generics.ts var generics_exports = {}; __export(generics_exports, { validateAge: () => validateAge, validateBirthDate: () => validateBirthDate, validateEmail: () => validateEmail, validatePassword: () => validatePassword, validateUser: () => validateUser, validateUsername: () => validateUsername }); module.exports = __toCommonJS(generics_exports); // src/helpers/index.ts var containsBannedWords = (value, bannedWords) => { if (!bannedWords || bannedWords.length === 0) return false; return bannedWords.some( (word) => value.toLowerCase().includes(word.toLowerCase()) ); }; // src/validators/generics.ts var validateUsername = (username, options = {}) => { const errors = []; const min = options.min ?? 3; const max = options.max ?? 20; const specialChars = options.allowSpecialChars ?? "_"; if (username.length < min) { errors.push(options.messages?.min || `Username must be at least ${min} characters`); } if (username.length > max) { errors.push(options.messages?.max || `Username must be at most ${max} characters`); } if (containsBannedWords(username, options.bannedWords)) { errors.push(options.messages?.bannedWords || "Username contains banned words"); } if (/^\d+$/.test(username)) { errors.push(options.messages?.onlyNumbers || "Username cannot contain only numbers"); } const regex = new RegExp(`^[a-zA-Z0-9${specialChars}]+$`); if (!regex.test(username)) { errors.push(options.messages?.invalidFormat || `Username can only contain letters, numbers and ${specialChars}`); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; var validateUser = (user, options = {}) => { const errors = []; const min = options.min ?? 3; const max = options.max ?? 30; const specialChars = options.allowSpecialChars ?? "''\\s"; if (user.length < min) { errors.push(options.messages?.min || `Name must be at least ${min} characters`); } if (user.length > max) { errors.push(options.messages?.max || `Name must be at most ${max} characters`); } if (containsBannedWords(user, options.bannedWords)) { errors.push(options.messages?.bannedWords || "Name contains banned words"); } if (/^\s*$/.test(user)) { errors.push(options.messages?.emptySpace || "Name cannot be empty or contain only spaces"); } const regex = new RegExp(`^[a-zA-Z\xC0-\xD6\xD8-\xF6\xF8-\xFF${specialChars}]+$`); if (!regex.test(user)) { errors.push(options.messages?.invalidFormat || `Name can only contain letters and ${specialChars}`); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; var validateEmail = (email, options = {}) => { const errors = []; const emailRegex = /^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/; const match = email.match(emailRegex); if (!match) { errors.push(options.messages?.invalidFormat || "Invalid email format"); } else if (options.allowedDomains && !options.allowedDomains.includes(match[1])) { errors.push(options.messages?.allowedDomains || `Email domain must be one of: ${options.allowedDomains.join(", ")}`); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; var validatePassword = (password, options = {}) => { const errors = []; const min = options.min ?? 8; const max = options.max ?? 100; const specialChars = options.allowSpecialChars ?? "!@#$%^&*()_+"; if (password.length < min) { errors.push(options.messages?.min || `Password must be at least ${min} characters`); } if (password.length > max) { errors.push(options.messages?.max || `Password must be at most ${max} characters`); } if (containsBannedWords(password, options.bannedWords)) { errors.push(options.messages?.bannedWords || "Password contains banned words"); } const regex = new RegExp( `^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[${specialChars}])[A-Za-z\\d${specialChars}]+$` ); if (!regex.test(password)) { errors.push(options.messages?.invalidFormat || "Password must contain at least one uppercase letter, one lowercase letter, one number and one special character"); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; var validateBirthDate = (date, options = {}) => { const errors = []; const dateRegex = /^\d{4}-\d{2}-\d{2}$/; if (!dateRegex.test(date)) { errors.push(options.messages?.invalidFormat || "Date must be in YYYY-MM-DD format"); } const birthDate = new Date(date); if (isNaN(birthDate.getTime())) { errors.push(options.messages?.invalidFormat || "Invalid date"); } else if (birthDate >= /* @__PURE__ */ new Date()) { errors.push(options.messages?.invalidFormat || "Birth date must be in the past"); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; var validateAge = (age, options = {}) => { const errors = []; const min = options.min ?? 18; const max = options.max ?? 120; if (!Number.isInteger(age)) { errors.push(options.messages?.invalidFormat || "Age must be an integer"); } if (age < min) { errors.push(options.messages?.min || `Age must be at least ${min} years`); } if (age > max) { errors.push(options.messages?.max || `Age must be at most ${max} years`); } return { valid: errors.length === 0, errors: errors.length > 0 ? errors : void 0 }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { validateAge, validateBirthDate, validateEmail, validatePassword, validateUser, validateUsername }); //# sourceMappingURL=generics.js.map