UNPKG

validlyjs

Version:

A high-performance Laravel-inspired validation library for TypeScript/JavaScript

33 lines (32 loc) 5.4 kB
const e={name:"string.min",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0] return a>=0&&e.length>=a},message:"The {field} must be at least {0} characters.",priority:2},t={name:"string.max",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0] return a>=0&&a>=e.length},message:"The {field} may not be greater than {0} characters.",priority:2},a={name:"string.length",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0] let i if("number"==typeof a)i=a else{if("string"!=typeof a)return!1 if(i=parseInt(a,10),i!=i)return!1}return i>=0&&e.length===i},message:"The {field} must be exactly {0} characters.",priority:2},i={name:"string.size",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0] let i if("number"==typeof a)i=a else{if("string"!=typeof a)return!1 if(i=parseInt(a,10),i!=i)return!1}return i>=0&&e.length===i},message:"The {field} must be {0} characters.",priority:2},r={name:"string.between",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0],i=t[1],r=e.length return!(0>i||0>a||a>r||r>i)},message:"The {field} must be between {0} and {1} characters.",priority:2},s=/^https?:\/\/([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\/.*)?$/,n=/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,o=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,l=/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,f=/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::1$|^::$/,d=/^(\d{1,3}\.){3}\d{1,3}$/,m=/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,g=/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$|^([0-9A-Fa-f]{4}\.){2}([0-9A-Fa-f]{4})$/,p=/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,y=/^\d+$/,u={name:"string.email",validate:e=>"string"==typeof e&&n.test(e),message:"The {field} must be a valid email address.",priority:2},h={name:"string.url",validate:e=>"string"==typeof e&&s.test(e),message:"The {field} format is invalid.",priority:2},c={name:"string.uuid",validate:e=>"string"==typeof e&&o.test(e),message:"The {field} must be a valid UUID.",priority:2},v={name:"string.json",validate:e=>{if("string"!=typeof e)return!1 try{return JSON.parse(e),!0}catch(e){return!1}},message:"The {field} must be a valid JSON string.",priority:2},b={name:"string.ipv4",validate:e=>"string"==typeof e&&l.test(e),message:"The {field} must be a valid IPv4 address.",priority:2},T={name:"string.ipv6",validate:e=>"string"==typeof e&&f.test(e),message:"The {field} must be a valid IPv6 address.",priority:2},A={name:"string.ip",validate:e=>"string"==typeof e&&(d.test(e)||m.test(e)),message:"The {field} must be a valid IP address.",priority:2},$={name:"string.mac_address",validate:e=>"string"==typeof e&&g.test(e),message:"The {field} must be a valid MAC address.",priority:2},z={name:"string.hex_color",validate:e=>"string"==typeof e&&p.test(e),message:"The {field} must be a valid hex color.",priority:2},Z={name:"string.credit_card",validate:e=>{if("string"!=typeof e)return!1 const t=e.length if(13>t||t>23)return!1 let a="" for(let i=0;t>i;i++){const t=e[i] " "!==t&&"-"!==t&&(a+=t)}const i=a.length if(13>i||i>19)return!1 if(!y.test(a))return!1 let r=0,s=!1 for(let e=i-1;e>=0;e--){let t=a.charCodeAt(e)-48 s&&(t<<=1,t>9&&(t-=9)),r+=t,s=!s}return r%10==0},message:"The {field} must be a valid credit card number.",priority:2},_=/^[a-zA-Z]+$/,F=/^[a-zA-Z0-9]+$/,w=/^[a-zA-Z0-9_-]+$/,x=/^[a-zA-Z0-9\s_]+$/,I={name:"string.regex",validate:(e,t)=>{if("string"!=typeof e)return!1 const a=t[0] if(!a)return!1 try{return RegExp(a,t[1]).test(e)}catch(e){return!1}},message:"The {field} format is invalid.",priority:2},P={name:"string.alpha",validate:e=>"string"==typeof e&&_.test(e),message:"The {field} may only contain letters.",priority:2},J={name:"string.alpha_num",validate:e=>"string"==typeof e&&F.test(e),message:"The {field} may only contain letters and numbers.",priority:2},N={name:"string.alpha_dash",validate:e=>"string"==typeof e&&w.test(e),message:"The {field} may only contain letters, numbers, dashes, and underscores.",priority:2},O={name:"string.alpha_space",validate:e=>"string"==typeof e&&x.test(e),message:"The {field} may only contain letters, numbers, spaces, and underscores.",priority:2},S={name:"string.starts_with",validate:(e,t)=>"string"==typeof e&&e.startsWith(t[0]),message:"The {field} must start with {0}.",priority:2},U={name:"string.ends_with",validate:(e,t)=>"string"==typeof e&&e.endsWith(t[0]),message:"The {field} must end with {0}.",priority:2},j={name:"string.contains",validate:(e,t)=>"string"==typeof e&&e.includes(t[0]),message:"The {field} must contain {0}.",priority:2},C={name:"string.in",validate:(e,t)=>"string"==typeof e&&t.includes(e),message:"The {field} must be one of the following values: {0}.",priority:2},D={name:"string.not_in",validate:(e,t)=>"string"==typeof e&&!t.includes(e),message:"The {field} must not be one of the following values: {0}.",priority:2} export{N as alphaDashRule,J as alphaNumericRule,P as alphaRule,O as alphaSpaceRule,j as containsRule,Z as creditCardRule,u as emailRule,U as endsWithRule,z as hexColorRule,C as inRule,A as ipRule,b as ipv4Rule,T as ipv6Rule,v as jsonRule,$ as macAddressRule,D as notInRule,I as regexRule,S as startsWithRule,r as stringBetweenRule,a as stringLengthRule,t as stringMaxRule,e as stringMinRule,i as stringSizeRule,h as urlRule,c as uuidRule}