UNPKG

abolish

Version:

A javascript object validator.

2 lines (1 loc) 6.44 kB
"use strict";var AbolishStringValidators=(()=>{var rr=Object.create;var l=Object.defineProperty;var er=Object.getOwnPropertyDescriptor;var tr=Object.getOwnPropertyNames;var ar=Object.getPrototypeOf,or=Object.prototype.hasOwnProperty;var n=(e,r)=>()=>(e&&(r=e(e=0)),r);var o=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports);var ir=(e,r,t,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of tr(r))!or.call(e,s)&&s!==t&&l(e,s,{get:()=>r[s],enumerable:!(a=er(r,s))||a.enumerable});return e};var i=(e,r,t)=>(t=e!=null?rr(ar(e)):{},ir(r||!e||!e.__esModule?l(t,"default",{value:e,enumerable:!0}):t,e));var f=o((dr,d)=>{"use strict";d.exports={name:"alphaNumeric",error:":param is not AlphaNumeric.",validator:e=>new RegExp(/^[a-z0-9]+$/i).test(e)}});var c=o((fr,p)=>{"use strict";p.exports={name:"boolean",error:":param is not a valid boolean",validator:(e,r,{modifier:t})=>{if(typeof e=="boolean")return!0;if(typeof e=="string"){if(e=e.toLowerCase(),e==="true")return t.setThis(!0),!0;if(e==="false")return t.setThis(!1),!0}else if(typeof e=="number"){if(e===1)return t.setThis(!0),!0;if(e===0)return t.setThis(!1),!0}else return!1}}});function u(e,r){return typeof r=="object"&&r instanceof e}var m=n(()=>{"use strict"});var A=o((ur,b)=>{"use strict";m();b.exports={name:"date",validator:(e,r,{modifier:t})=>{if(u(Date,e))return!0;if(typeof e=="string"){let a=new Date(e),s=!isNaN(a.getTime());return s&&(typeof r=="string"&&r==="cast"||typeof r=="object"&&(r!=null&&r.cast))&&t.setThis(a),s}else return!1},error:":param is not a valid Date!"}});var y=o((mr,h)=>{"use strict";h.exports={name:"email",error:":param is not a valid email.",validator:(e,r,{modifier:t})=>{if(r===!1)return!0;let a=/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,6})+$/.test(e);return a&&r==="lowercase"&&t&&t.setThis(e.toLowerCase()),a}}});var v=o((br,g)=>{"use strict";g.exports={name:"ipAddress",error:":param is not a valid IP address.",validator:e=>new RegExp(/((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/gm).test(e)}});var x=o((Ar,V)=>{"use strict";V.exports={name:"json",error:":param is not a valid JSON string",validator:e=>{if(typeof e!="string")return!1;try{return JSON.parse(e),!0}catch(r){return!1}}}});var O=o((hr,w)=>{"use strict";w.exports={name:"jsonDecode",error:":param is not a valid JSON string",validator:(e,r,{modifier:t})=>{t.setThis(JSON.parse(e))}}});var F=o((yr,T)=>{"use strict";T.exports={name:"jsonEncode",error:":param is not a valid JSON string",validator:(e,r,{modifier:t})=>{t.setThis(JSON.stringify(e))}}});var R=o((gr,N)=>{"use strict";N.exports={name:"md5",error:":param is not a valid MD5 string",validator:e=>/^[a-f0-9]{32}$/gi.test(e)}});var k=o((vr,E)=>{"use strict";E.exports={name:"number",error:":param is not a valid number",validator:(e,r,{modifier:t})=>{let a=!isNaN(e);return t&&a&&typeof e!="number"&&t.setThis(Number(e)),a}}});var C=o((Vr,j)=>{"use strict";j.exports={name:"regex",error:":param failed Regex validation.",validator:(e,r)=>{let t=r instanceof RegExp;return typeof e!="string"?!1:t?r.test(e):new RegExp(r).test(e)}}});var $=o((xr,S)=>{"use strict";function D(e,r){let t=r.getThis();if(typeof t[e]=="function")r.setThis(String(t)[e]());else throw new Error(`Validator string:${e} is not supported!`)}S.exports={name:"string",validator:(e,r,{modifier:t,error:a})=>{if(r===!0||r===void 0)return typeof e=="string"&&e.length>0?!0:a(":param is not a string");if(typeof r!="string")throw new Error("string: Validator option must be a string");if(r.indexOf(",")>0)for(let s of r.split(","))D(s,t);else D(r,t);return!0}}});function U(e,r,t="Options"){if(!sr(e,r))throw new TypeError(`${t} must be typeof [${r}], but [${typeof e}] was given.`);return!0}function sr(e,r){if(typeof r=="string")return r==="array"&&Array.isArray(e)?!0:typeof e===r;{let t=r.includes("array");return!t&&r.includes(typeof e)?!0:t&&(r.includes(typeof e)||Array.isArray(e))}}var I=n(()=>{"use strict"});var L=o((Tr,J)=>{"use strict";I();J.exports={name:"url",error:":param is not a valid URL",description:["Check that a value is a valid URL","Allow/Block hostnames"],validator:(e,r,{error:t})=>{U(r,["boolean","object"]);let a;try{a=new URL(e)}catch(s){return!1}if(a&&typeof r=="object"){if(r.hostname){if(r.hostname.allow&&!r.hostname.allow.includes(a.hostname))return t(`:param hostname is not among the allowed hostnames: [${r.hostname.allow.join(",")}]`).setCode("hostname.notAllowed");if(r.hostname.block&&r.hostname.block.includes(a.hostname))return t(":param hostname is not allowed.").setCode("hostname.blocked")}if(r.protocol){if(r.protocol.allow&&!r.protocol.allow.includes(a.protocol))return t(`:param protocol is not among the allowed protocols: [${r.protocol.allow.join(",")}]`).setCode("protocol.notAllowed");if(r.protocol.block&&r.protocol.block.includes(a.protocol))return t(":param protocol is not allowed.").setCode("protocol.blocked")}}return!0}}});var lr=o((Fr,Z)=>{var _=i(f()),M=i(c()),B=i(A()),K=i(y()),P=i(v()),z=i(x()),G=i(O()),q=i(F()),H=i(R()),Q=i(k()),W=i(C()),X=i($()),Y=i(L());Z.exports={alphaNumeric:_.default,boolean:M.default,date:B.default,email:K.default,ipAddress:P.default,json:z.default,jsonDecode:G.default,jsonEncode:q.default,md5:H.default,number:Q.default,regex:W.default,url:Y.default,string:X.default}});return lr();})();