UNPKG

yh-validator

Version:

A lightweight form validation library with modular functions

1 lines 1.08 kB
import {a}from'./chunk-QJYIPWK4.mjs';function U(s,d={}){let{minLength:r=3,maxLength:t=30,allowSpaces:i=false,allowDashes:p=true,allowUnderscores:u=true,allowPeriods:g=true,noLeadingTrailingSpecialChars:f=true,noConsecutiveSpecialChars:m=true,blacklist:w=[]}=d,e=[],l=a(s,"Username");if(l)return l;let a$1=s.trim();a$1.length<r&&e.push(`Must be at least ${r} characters long.`),a$1.length>t&&e.push(`Cannot exceed ${t} characters.`),a$1.length===0&&r>0&&e.push("Cannot be empty."),!i&&s.includes(" ")&&e.push("Cannot contain spaces.");let n="a-zA-Z0-9";p&&(n+="\\-"),u&&(n+="_"),g&&(n+="\\."),i&&(n+=" "),new RegExp(`[^${n}]`).test(a$1)&&(e.includes("Cannot contain spaces.")||e.push("Contains disallowed characters.")),f&&/^[\s\-_.]|[\s\-_.]$/.test(a$1)&&e.push("Cannot start or end with a special character or space."),m&&/[\-_.]{2,}/.test(a$1)&&e.push("Cannot contain consecutive special characters.");let C=a$1.toLowerCase();w.some(o=>C===o.toLowerCase())&&e.push("Is reserved or not allowed.");let c=Array.from(new Set(e));return {valid:c.length===0,errors:c}}export{U as a};