UNPKG

@re-flex/form-validator

Version:

Re-flex UTILS _> form-validator

3 lines (2 loc) 3.02 kB
const t=new Date(""),e=Array.isArray,i=t=>new Date(t).getTime();class s{constructor(){this.options={nullable:!0,required:!1},this.matcher=[],this.nullable=(t=!0)=>(this.options.nullable=!!t,{validate:this.validate}),this.required=(t=!0)=>(this.options.required=!!t,{validate:this.validate}),this.defaultChain=()=>({required:this.required,nullable:this.nullable,validate:this.validate})}validate(t){if(e(this.matcher))for(const{message:e,checker:i}of this.matcher){let s=i(t);if(console.log("isValid",s,e),console.log("this.options.required",this.options),this.options.required&&void 0===t&&null!==t)return e||"Not Valid";if(!1===this.options.nullable&&null===t)return e||"Not Valid";if(!s)return e||"Not Valid"}return null}}const h=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,o=/^[A-Za-z]\w{7,14}$/,a=/^\d{10}$/,n=/^(?:3[47][0-9]{13})$/,r=/[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gim;class l extends s{constructor(t){super(),this.email=this.BindRegexpHook(h),this.password=this.BindRegexpHook(o),this.phone=this.BindRegexpHook(a),this.creditCard=this.BindRegexpHook(n),this.url=this.BindRegexpHook(r),this.match=this.BindRegexpHook(/.*/gim),this.customRegexp=t}BindRegexpHook(t){return(e="Not Valid")=>(this.matcher.push({message:e,type:"string",checker:e=>(this.customRegexp||t).test(e)}),this.defaultChain())}}class u extends s{constructor(t){super(),this.BindNumberHook=t=>(e="Not Valid")=>(this.matcher.push({message:e,type:"number",checker:e=>!Number.isNaN(e)&&t(e,this.maxMin)}),this.defaultChain()),this.positive=this.BindNumberHook(t=>t>0),this.negative=this.BindNumberHook(t=>t<0),this.max=this.BindNumberHook((t,e=0)=>t<=e),this.min=this.BindNumberHook((t,e=0)=>t>=e),this.float=this.BindNumberHook(t=>!String(t).includes(".")),this.integer=this.BindNumberHook(t=>!Number.isInteger(t)),this.safeInteger=this.BindNumberHook(t=>!Number.isSafeInteger(t)),this.bigInteger=this.BindNumberHook(t=>!BigInt(t)===t),this.maxMin=t}}class d extends s{constructor(e){super(),this.BindDateHook=e=>(i="Not Valid")=>(this.matcher.push({message:i,type:"date",checker:i=>new Date(i)!==t&&e(i,this.maxMin)}),this.defaultChain()),this.max=this.BindDateHook((t,e=new Date)=>i(t)<=i(e)),this.min=this.BindDateHook((t,e=new Date)=>i(t)>=i(e)),this.maxMin=e}}class c extends s{constructor(t="Not Valid"){super(),this.matcher.push({message:t,type:"boolean",checker:t=>["true","false"].includes(String(t))})}}class m extends s{constructor(t,e,i="Not Valid"){super(),this.message="Not Valid",this.init=()=>(this.matcher.push({message:this.message,type:"object",checker:t=>{let e={};for(const i in this.rules)if(Object.prototype.hasOwnProperty.call(t,i)){const s=this.rules[i].validate(t[i]);e[i]=s}return e}}),this),this.rules=t,this.each=e,this.message=i}}const p=()=>new l,g=t=>new u(t),k=()=>new d,b=t=>new c(t),N=(t,e="Not Valid",i)=>new m(t,i,e).init();export{b as boolean,k as date,g as number,N as object,p as string}; //# sourceMappingURL=form-validator.modern.js.map