UNPKG

yh-validator

Version:

A lightweight form validation library with modular functions

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