n4s
Version:
Assertion library for form validations
3 lines (2 loc) • 4.48 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("vest-utils"),require("context")):"function"==typeof define&&define.amd?define(["exports","vest-utils","context"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).n4s={},n["vest-utils"],n.context)}(this,(function(n,t,e){"use strict";const i=e.createCascade(((n,e)=>{const i={value:n.value,meta:n.meta||{}};return e?n.set?t.assign(i,{parent:()=>function(n){return{value:n.value,meta:n.meta,parent:n.parent}}(e)}):e:t.assign(i,{parent:s})}));function s(){return null}function r(n,e){return t.isStringValue(n)&&t.isStringValue(e)&&n.endsWith(e)}const u=t.bindNot(r);function o(n,t){return n===t}const a=t.bindNot(o);function l(n,e){return t.numberEquals(n,e)||t.greaterThan(n,e)}function c(n,e){return(t.isArray(e)||!(!t.isStringValue(e)||!t.isStringValue(n)))&&-1!==e.indexOf(n)}const N=t.bindNot(c);function f(n,e){return t.isNumeric(n)&&t.isNumeric(e)&&Number(n)<Number(e)}function g(n,e){return t.numberEquals(n,e)||f(n,e)}function d(n,t,e){return l(n,t)&&g(n,e)}const h=t.bindNot(d);function m(n){return t.isNullish(n)||t.isStringValue(n)&&!n.trim()}const p=t.bindNot(m),b=t.bindNot(t.isBoolean),y=n=>!!t.isNumeric(n)&&n%2==0;function E(n,t){return n in t}const v=t.bindNot(E);function q(n){return Number.isNaN(n)}const S=t.bindNot(q);function T(n){return f(n,0)}function x(n){return Boolean("number"==typeof n)}const O=t.bindNot(x),V=n=>!!t.isNumeric(n)&&n%2!=0,B=t.bindNot(t.isStringValue);function w(n){return!!n}const W=t.bindNot(w);function A(n,e){if(t.isNullish(e))return!1;for(const t in e)if(e[t]===n)return!0;return!1}const P=t.bindNot(A);function F(n,t){return l(n.length,t)}function U(n,e){return e instanceof RegExp?e.test(n):!!t.isStringValue(e)&&new RegExp(e).test(n)}const j=t.bindNot(U);function k(n,t){try{return t(n)}catch(n){return!1}}function I(n,t){return f(n.length,t)}function J(n,t){return g(n.length,t)}function K(n,e){return t.isStringValue(n)&&t.isStringValue(e)&&n.startsWith(e)}const R=t.bindNot(K);const $={condition:k,doesNotEndWith:u,doesNotStartWith:R,endsWith:r,equals:o,greaterThan:t.greaterThan,greaterThanOrEquals:l,gt:t.greaterThan,gte:l,inside:c,isArray:t.isArray,isBetween:d,isBlank:m,isBoolean:t.isBoolean,isEmpty:t.isEmpty,isEven:y,isFalsy:W,isKeyOf:E,isNaN:q,isNegative:T,isNotArray:t.isNotArray,isNotBetween:h,isNotBlank:p,isNotBoolean:b,isNotEmpty:t.isNotEmpty,isNotKeyOf:v,isNotNaN:S,isNotNull:t.isNotNull,isNotNullish:t.isNotNullish,isNotNumber:O,isNotNumeric:t.isNotNumeric,isNotString:B,isNotUndefined:t.isNotUndefined,isNotValueOf:P,isNull:t.isNull,isNullish:t.isNullish,isNumber:x,isNumeric:t.isNumeric,isOdd:V,isPositive:t.isPositive,isString:t.isStringValue,isTruthy:w,isUndefined:t.isUndefined,isValueOf:A,lengthEquals:t.lengthEquals,lengthNotEquals:t.lengthNotEquals,lessThan:f,lessThanOrEquals:g,longerThan:t.longerThan,longerThanOrEquals:F,lt:f,lte:g,matches:U,notEquals:a,notInside:N,notMatches:j,numberEquals:t.numberEquals,numberNotEquals:t.numberNotEquals,shorterThan:I,shorterThanOrEquals:J,startsWith:K};function C(n){return $[n]}function M(n,t){const e={pass:n};return t&&(e.message=t),e}function X(n){return t.defaultTo(n,M(!0))}function z(n,e,i,...s){return function(n){t.invariant(t.isBoolean(n)||n&&t.isBoolean(n.pass),"Incorrect return value for rule: "+JSON.stringify(n))}(n),t.isBoolean(n)?M(n):M(n.pass,t.optionalFunctionValue(n.message,e,i,...s))}function D(n){const e={message:function(n){return s=n,r},pass:!1};let s;const r=new Proxy(e,{get:(u,o)=>{const a=C(o);return a?function(e,r,u){return function(...o){const a=i.run({value:n},(()=>z(r(n,...o),u,n,...o)));function l(){return t.isNullish(s)?t.isNullish(a.message)?`enforce/${u} failed with ${JSON.stringify(n)}`:t.StringObject(a.message):t.StringObject(s)}return t.invariant(a.pass,l()),e.pass=a.pass,e}}(r,a,o):e[o]}});return r}const G=function(){const n={context:()=>i.useX(),extend:n=>{t.assign($,n)}};return new Proxy(t.assign(D,n),{get:(n,e)=>e in n?n[e]:C(e)?function(n){const e=[];let s;return function n(r){return(...u)=>{const o=C(r);e.push((n=>z(o(n,...u),r,n,...u)));let a={run:n=>X(t.mapFirst(e,((e,r)=>{var u;const o=i.run({value:n},(()=>e(n)));r(!o.pass,M(!!o.pass,null!==(u=t.optionalFunctionValue(s,n,o.message))&&void 0!==u?u:o.message))}))),test:n=>a.run(n).pass,message:n=>(n&&(s=n),a)};return a=new Proxy(a,{get:(t,e)=>C(e)?n(e):t[e]}),a}}(n)}(e):void 0})}();n.ctx=i,n.enforce=G}));
//# sourceMappingURL=n4s.production.js.map