UNPKG

validlyjs

Version:

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

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