nope-validator
Version:
Fast and simple JS validator
1 lines • 10.2 kB
JavaScript
;Object.defineProperty(exports,'__esModule',{value:true});var f=class{constructor(n){this.key=n;}};function O(u,n){let e=u.split("../"),r=e.length-1,t=e[e.length-1],i=n;for(let s=0;s<r;s++)i=i?.___parent;return i&&t!==void 0&&t!==null?i[t]:t}function A(u,n){return u.map(r=>O(r,n))}function g(u,n){return u instanceof f?O(u.key,n):u}function d(u,n){if(typeof u=="object"&&u!=null&&typeof n=="object"&&n!=null){if(u===n)return true;let e=0,r=0;for(let t in u)e++;for(let t in n)r++;if(e-r!==0)return false;for(let t in u)if(!(t in n)||!d(u[t],n[t]))return false;for(let t in n)if(!(t in u)||!d(n[t],u[t]))return false;return true}return u===n}function j(u){return u.split(/[,[\].]/g).filter(Boolean)}function x(u,n,e=false){if(!u)return;let r=j(u);r=e?r.slice(0,-1):r;let t=n;for(let i of r)t=t[i];return t}function b(u,n,e){let r=false;return u.reduce(function(t,i){return r?t:t.then(function(s){return s?(r=true,s):i(n,e)}).catch(function(s){return s?(r=true,s):i(n,e)})},Promise.resolve())}function l(u){return u==null}var y=class{constructor(n){this.validationRules=[];this._type="object";this.objectShape=n||{};}getType(){return this._type}shape(n){return Object.assign(this.objectShape,n),this}extend(n){return Object.assign(this.objectShape,n.objectShape),this}noUnknown(n="Input contains invalid keys"){let e=r=>{let t=false;for(let s in this.objectShape){t=true;break}if(!t)throw Error("noUnknown must be used with a schema");let i=false;for(let s in r)i=i||!(s in this.objectShape);if(i)return n};return this.validationRules.push(e),this}validate(n,e,r){for(let a of this.validationRules){let p=a(n);if(p)return p}let t=false,i=r?.abortEarly,s={},o=Object.assign({___parent:e},n);for(let a in this.objectShape){let h=this.objectShape[a].validate(n[a],o,r);if(h&&(t=true,s[a]=h,i))return s}if(t)return s}validateAsync(n,e,r){return b(this.validationRules,n,e).then(t=>{if(t)return t;let i=[],s=[],o=Object.assign({___parent:e},n);for(let a in this.objectShape){let h=this.objectShape[a].validateAsync(n[a],o,r);i.push(a),s.push(h);}return Promise.all(s).then(a=>{let p={},h=false;for(let m=0;m<i.length;m++){let E=a[m];E&&(h=true,p[i[m]]=E);}if(h)return p})})}validateAt(n,e){let r=j(n),t=this.objectShape;for(let o of r)isNaN(parseInt(o,10))&&(t[o]?.objectShape?t=t[o].objectShape:t[o]?.ofShape?t=t[o].ofShape.objectShape||t[o].ofShape:t=t[o]);let i=x(n,e,true),s=x(n,e);return t.validate(s,i)}};var I=/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i,k=/^([a-z][a-z0-9\*\-\.]*):\/\/(?:(?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)*(?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@)?(?:(?:[a-z0-9\-\.]|%[0-9a-f]{2})+|(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]))(?::[0-9]+)?(?:[\/|\?](?:[\w#!:\.\?\+=&@!$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})*)?$/i;var c=class u{constructor(){this.validationRules=[];this._type="undefined";}getType(){return this._type}isEmpty(n){return l(n)}required(n="This field is required"){let e=r=>{if(this.isEmpty(r))return n};return this.test(e)}notAllowed(n="Field is not allowed"){let e=r=>{if(!this.isEmpty(r))return n};return this.test(e)}when(n,e){let r=Array.isArray(n)?n:[n],t=(i,s)=>{let o=A(r,s),a=[...o],p=e.is;return (typeof p=="function"?p(...a):o.every(m=>m===p))?e.then:e.otherwise};return this.test(t)}oneOf(n,e="Invalid option"){let r=(t,i)=>{if(t===void 0)return;let s;if(n instanceof f?s=g(n,i):s=n.map(o=>g(o,i)),s.indexOf(t)===-1)return e};return this.test(r)}notOneOf(n,e="Invalid Option"){let r=(t,i)=>{if(n.map(o=>g(o,i)).indexOf(t)!==-1)return e};return this.test(r)}test(n){return this.validationRules.push(n),this}validate(n,e){this._entry=n;for(let r of this.validationRules){let t=r(this._entry,e);if(t instanceof u)return t.validate(this._entry,e);if(t)return t}}validateAsync(n,e){return b(this.validationRules,this._entry??n,e).then(r=>{if(r instanceof u)return r.validateAsync(this._entry??n,e);if(r)return r})}};var v=class extends c{constructor(){super(...arguments);this._type="string";}validate(e,r){let t=e&&String(e);return super.validate(t,r)}validateAsync(e,r){let t=e&&String(e);return super.validateAsync(t,r)}isEmpty(e){return l(e)||e.trim().length===0}regex(e,r="Doesn't satisfy the rule"){let t=i=>{if(!this.isEmpty(i)&&!e.test(i))return r};return this.test(t)}url(e="Input is not a valid url"){return this.regex(k,e),this}email(e="Input is not a valid email"){return this.regex(I,e),this}min(e,r){return this.atLeast(e,r),this}max(e,r){return this.atMost(e,r),this}greaterThan(e,r="Input is too short"){let t=i=>{if(this.isEmpty(i))return;if(i.length<=e)return r};return this.test(t)}lessThan(e,r="Input is too long"){let t=i=>{if(this.isEmpty(i))return;if(i.length>=e)return r};return this.test(t)}atLeast(e,r="Input is too short"){let t=i=>{if(this.isEmpty(i))return;if(i.length<e)return r};return this.test(t)}atMost(e,r="Input is too long"){let t=i=>{if(this.isEmpty(i))return;if(i.length>e)return r};return this.test(t)}between(e,r,t="Input is too short",i="Input is too long"){if(e&&r&&e>r){let s=()=>{throw Error("between must receive an initial length (startLength) smaller than the final length (endLength) parameter")};return this.test(s)}return this.atLeast(e,t),this.atMost(r,i),this}exactLength(e,r=`Must be at exactly of length ${e}`){let t=i=>{if(this.isEmpty(i))return;if(i.length!==e)return r};return this.test(t)}trim(){let e=r=>{this._entry=r.trim();};return this.test(e)}};var R=class extends c{constructor(e="The field is not a valid number"){super();this.message="The field is not a number";this._type="number";this.message=e;}integer(e="Input must be an integer"){let r=t=>{if(!this.isEmpty(t)&&t!==Math.floor(t))return e};return this.test(r)}min(e,r){return this.atLeast(e,r),this}max(e,r){return this.atMost(e,r),this}greaterThan(e,r="Input is too small"){let t=i=>{if(!this.isEmpty(i)&&i<=e)return r};return this.test(t)}lessThan(e,r="Input is too large"){let t=i=>{if(!this.isEmpty(i)&&i>=e)return r};return this.test(t)}atLeast(e,r="Input is too small"){let t=i=>{if(!this.isEmpty(i)&&i<e)return r};return this.test(t)}atMost(e,r="Input is too large"){let t=i=>{if(!this.isEmpty(i)&&i>e)return r};return this.test(t)}between(e,r,t="Input is too small",i="Input is too large"){if(e&&r&&e>r){let s=()=>{throw Error("between must receive an initial size (sizeStart) smaller than the final size (sizeEnd) parameter")};return this.test(s)}return this.atLeast(e,t),this.atMost(r,i),this}positive(e="Input must be positive"){return this.greaterThan(0,e),this}negative(e="Input must be negative"){return this.lessThan(0,e),this}validate(e,r){let t=e&&Number(e);return !this.isEmpty(t)&&Number.isNaN(t)?this.message:super.validate(t,r)}validateAsync(e,r){let t=e&&Number(e);return !this.isEmpty(t)&&Number.isNaN(t)?Promise.resolve(this.message):super.validateAsync(t,r)}};var T=class extends c{constructor(){super(...arguments);this._type="boolean";}true(e="Input must be true"){let r=t=>{if(!this.isEmpty(t)&&t!==true)return e};return this.test(r)}false(e="Input must be false"){let r=t=>{if(!this.isEmpty(t)&&t!==false)return e};return this.test(r)}validate(e,r){let t=l(e)?e:!!e;return super.validate(t,r)}validateAsync(e,r){let t=l(e)?e:!!e;return super.validateAsync(t,r)}};function S(u,n){let e=false;return u.reduce(function(r,t){return e?r:r.then(function(i){return i?(e=true,i):n.validateAsync(t)})},Promise.resolve())}var N=class{constructor(){this._type="object";this.validationRules=[];this.ofShape=null;}getType(){return this._type}required(n="This field is required"){let e=r=>{if(l(r))return n};return this.test(e)}of(n,e="One or more elements are of invalid type"){this.ofShape=n;let r=t=>{if(l(t))return;if(t.some(s=>n.getType()!==typeof s)||t.find(s=>n.validate(s)))return e};return this.test(r)}ofAsync(n,e="One or more elements are of invalid type"){this.ofShape=n;let r=t=>{if(!l(t))return t.some(i=>n.getType()!==typeof i)?e:S(t,n).then(i=>i&&e||void 0)};return this.test(r)}minLength(n,e="Input is too short"){let r=t=>{if(!l(t)&&t.length<=n)return e};return this.test(r)}maxLength(n,e="Input is too long"){let r=t=>{if(!l(t)&&t.length>=n)return e};return this.test(r)}mustContain(n,e="Input does not contain required value"){let r=t=>{if(!l(t)&&t.indexOf(n)===-1)return e};return this.test(r)}hasOnly(n,e="Input elements must correspond to value values"){let r=t=>{if(!l(t)&&t.some(i=>typeof i=="object"?!n.find(s=>d(i,s)):n.indexOf(i)===-1))return e};return this.test(r)}every(n,e="Input does not satisfy condition"){let r=t=>{if(!l(t)&&t.some(i=>!n(i)))return e};return this.test(r)}some(n,e="Input does not satisfy condition"){let r=t=>{if(!(l(t)||t.length===0)&&!t.some(i=>n(i)))return e};return this.test(r)}test(n){return this.validationRules.push(n),this}validate(n,e){for(let r of this.validationRules){let t=r(n,e);if(t instanceof c)return t.validate(n,e);if(t)return `${t}`}}validateAsync(n,e){return b(this.validationRules,n,e).then(r=>{if(r instanceof c)return r.validateAsync(n,e);if(r)return r})}};var w=class extends c{constructor(e="The field is not a valid date"){super();this._type="object";this.message=e;}before(e,r=`Date must be before ${e.toString()}`){let t=(i,s)=>{if(this.isEmpty(i))return;let o=e instanceof f&&s?s[e.key]:e;if(new Date(i)>=new Date(o))return r};return this.test(t)}after(e,r=`Date must be after ${e}`){let t=(i,s)=>{if(this.isEmpty(i))return;let o=e instanceof f&&s?s[e.key]:e;if(new Date(i)<=new Date(o))return r};return this.test(t)}parseDate(e){let r=e;if(this.isEmpty(e)||e instanceof Date)r=e;else if(!isNaN(+new Date(e)))r=new Date(e);else {let t=new Date(e);if(isNaN(+t))throw this.message;r=new Date(t);}return r}validate(e,r){let t;try{t=this.parseDate(e);}catch(i){return i}return super.validate(t,r)}validateAsync(e,r){let t;try{t=this.parseDate(e);}catch(i){return Promise.resolve(i)}return super.validateAsync(t,r)}};var _=()=>new y,P=()=>new v,V=u=>new R(u),D=()=>new T,C=u=>new w(u),$=()=>new N,M=u=>new f(u),q={object:_,string:P,number:V,boolean:D,date:C,array:$,ref:M};var ge=q;exports.Nope=q;exports.array=$;exports.boolean=D;exports.date=C;exports.default=ge;exports.number=V;exports.object=_;exports.ref=M;exports.string=P;