From f4a99cad4064a493d73ab8a8261665e204d3863c Mon Sep 17 00:00:00 2001 From: Sebastian Barfurth Date: Tue, 22 Feb 2022 16:36:19 +0100 Subject: [PATCH 1/3] feat: add modifier for strict schema validation --- CHANGELOG.md | 1 + dist/v8n.amd.js | 53 ++++++++++++++++++++++----- dist/v8n.browser.js | 53 ++++++++++++++++++++++----- dist/v8n.browser.min.js | 2 +- dist/v8n.browser.min.js.map | 2 +- dist/v8n.cjs.js | 53 ++++++++++++++++++++++----- dist/v8n.esm.browser.js | 52 +++++++++++++++++++++----- dist/v8n.esm.browser.min.js | 2 +- dist/v8n.esm.browser.min.js.map | 2 +- dist/v8n.esm.js | 53 ++++++++++++++++++++++----- dist/v8n.min.js | 2 +- dist/v8n.min.js.map | 2 +- dist/v8n.system.js | 53 ++++++++++++++++++++++----- dist/v8n.umd.js | 53 ++++++++++++++++++++++----- docs/api/README.md | 47 +++++++++++++++++++++--- src/Rule.js | 19 +++++----- src/v8n.js | 33 +++++++++++++++++ src/v8n.test.js | 65 +++++++++++++++++++++++++++++++++ types/modifier.d.ts | 4 +- types/test/test-v8n.ts | 2 +- types/v8n.d.ts | 1 + 21 files changed, 462 insertions(+), 92 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6028e6..63106ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - New rule `optionalAsync` for asynchronous, optional validation ([#206](https://github.com/imbrn/v8n/issues/206)) +- New modifier `strict` for schema validation ([#191](https://github.com/imbrn/v8n/issues/191), [#164](https://github.com/imbrn/v8n/issues/164)) ## [1.4.0] - 2022-02-17 diff --git a/dist/v8n.amd.js b/dist/v8n.amd.js index b0f5bc3..af8b209 100644 --- a/dist/v8n.amd.js +++ b/dist/v8n.amd.js @@ -11,13 +11,13 @@ define(function () { 'use strict'; var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -25,14 +25,14 @@ define(function () { 'use strict'; Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -43,7 +43,8 @@ define(function () { 'use strict'; return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -62,21 +63,21 @@ define(function () { 'use strict'; return typeof fn === 'object' ? fn[variant] : fn; } - function testAux(modifiers, fn) { + function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } - function testAsyncAux(modifiers, fn) { + function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -304,8 +305,40 @@ define(function () { 'use strict'; simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; + function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); + } + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.browser.js b/dist/v8n.browser.js index 43a96db..377ffc4 100644 --- a/dist/v8n.browser.js +++ b/dist/v8n.browser.js @@ -12,13 +12,13 @@ var v8n = (function () { var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -26,14 +26,14 @@ var v8n = (function () { Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -44,7 +44,8 @@ var v8n = (function () { return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -63,21 +64,21 @@ var v8n = (function () { return typeof fn === 'object' ? fn[variant] : fn; } - function testAux(modifiers, fn) { + function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } - function testAsyncAux(modifiers, fn) { + function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -305,8 +306,40 @@ var v8n = (function () { simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; + function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); + } + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.browser.min.js b/dist/v8n.browser.min.js index 43c29d0..c46009a 100644 --- a/dist/v8n.browser.min.js +++ b/dist/v8n.browser.min.js @@ -1,2 +1,2 @@ -var v8n=function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e){if(n.length){var u=n.shift(),i=r(n,e);return u.perform(i)}return t(e)}function e(n,r){if(n.length){var u=n.shift(),i=e(n,r);return u.performAsync(i)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}))(!1))return}if(!r(this.modifiers.slice(),this.fn)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in y?e._applyRule(y[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(y,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return p(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(p(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&p(t).every(n)}},async:function(n){return function(t){return Promise.all(p(t).map(n)).then((function(n){return n.every(Boolean)}))}}}};function p(n){return"string"==typeof n?n.split(""):n}var y={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||g)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return v("string")},boolean:function(){return v("boolean")},undefined:function(){return v("undefined")},null:function(){return v("null")},array:function(){return v("array")},object:function(){return v("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:m(!1),optionalAsync:m(!0)};function m(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function v(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function g(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f}(); +var v8n=function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return g("string")},boolean:function(){return g("boolean")},undefined:function(){return g("undefined")},null:function(){return g("null")},array:function(){return g("array")},object:function(){return g("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:v(!1),optionalAsync:v(!0)};function v(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function g(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f}(); //# sourceMappingURL=v8n.browser.min.js.map diff --git a/dist/v8n.browser.min.js.map b/dist/v8n.browser.min.js.map index 97b26c8..ff0478d 100644 --- a/dist/v8n.browser.min.js.map +++ b/dist/v8n.browser.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n};\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","rule","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"gCAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAmDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,GAC1B,GAAIE,EAAUK,OAAQ,CACpBC,IAAMC,EAAWP,EAAUQ,QACrBC,EAASL,EAAQJ,EAAWF,GAClC,OAAOS,EAASG,QAAQD,GAExB,OAAOP,EAAOJ,GAIlB,SAASa,EAAaX,EAAWF,GAC/B,GAAIE,EAAUK,OAAQ,CACpBC,IAAMC,EAAWP,EAAUQ,QACrBC,EAASE,EAAaX,EAAWF,GACvC,OAAOS,EAASK,aAAaH,GAE7B,gBAAOI,UAASC,QAAQC,QAAQb,EAAOJ,EAAI,QAAXI,CAAoBW,iBApEtDG,eAAMH,GACJI,IAAInB,EAAKG,KAAKH,GAEd,IACEM,EAAQH,KAAKD,UAAUkB,QAASpB,EAAhCM,CAAoCS,GACpC,MAAOM,GACPrB,oBAAW,GAGb,IACE,OAAOM,EAAQH,KAAKD,UAAUkB,QAASpB,EAAhCM,CAAoCS,GAC3C,MAAOM,GACP,OAAO,gBAIXC,gBAAOP,GACL,IACET,EAAQH,KAAKD,UAAUkB,QAASjB,KAAKH,GAArCM,CAAyCS,GACzC,MAAOM,GACP,GAAIf,EAAQH,KAAKD,UAAUkB,kBAASG,UAAMA,IAAtCjB,EAA0C,GAC5C,OAIJ,IAAKA,EAAQH,KAAKD,UAAUkB,QAASjB,KAAKH,GAArCM,CAAyCS,GA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SAASlF,IAAU8I,KAAKC,MAAM/I,KAAWA"} \ No newline at end of file +{"version":3,"file":"v8n.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"gCAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,GAClC,OAAOU,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,GACvC,OAAOU,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAI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,EAAmBH,EAAOE,GAChC,IACEC,EAAiBpF,OAAO7C,GAAS,IAAIgI,IACrC,MAAO1H,GACPA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,OAGZyH,EAAOvI,OAAS,EAClB,MAAMuI,EAER,OAAO,GAETzF,eAAOtC,GACLP,IAAMsI,EAAS,GACTG,EAASxE,OAAOC,KAAKmE,GAAQpD,cAAIsD,GAErC,OADyBF,EAAOE,GACRlF,WAAW9C,GAAS,IAAIgI,IAAMnH,gBAAMP,GAC1DA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,SAGhB,OAAOL,QAAQwE,IAAIyD,GAAQvH,iBACzB,GAAIoH,EAAOvI,OAAS,EAClB,MAAMuI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.cjs.js b/dist/v8n.cjs.js index b40d7db..3a5899a 100644 --- a/dist/v8n.cjs.js +++ b/dist/v8n.cjs.js @@ -11,13 +11,13 @@ Rule.prototype._test = function _test (value) { var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -25,14 +25,14 @@ Rule.prototype._test = function _test (value) { Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -43,7 +43,8 @@ Rule.prototype._testAsync = function _testAsync (value) { return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -62,21 +63,21 @@ function pickFn(fn, variant) { return typeof fn === 'object' ? fn[variant] : fn; } -function testAux(modifiers, fn) { +function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } -function testAsyncAux(modifiers, fn) { +function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -304,8 +305,40 @@ var availableModifiers = { simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; +function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); +} + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.esm.browser.js b/dist/v8n.esm.browser.js index ac6f6ee..28ec925 100644 --- a/dist/v8n.esm.browser.js +++ b/dist/v8n.esm.browser.js @@ -10,13 +10,13 @@ class Rule { let fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = () => false; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { return false; } @@ -24,14 +24,14 @@ class Rule { _check(value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), it => it)(false)) { + if (testAux(this.modifiers.slice(), it => it, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } } @@ -41,6 +41,7 @@ class Rule { testAsyncAux( this.modifiers.slice(), this.fn, + this, )(value) .then(valid => { if (valid) { @@ -58,21 +59,21 @@ function pickFn(fn, variant = 'simple') { return typeof fn === 'object' ? fn[variant] : fn; } -function testAux(modifiers, fn) { +function testAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); const nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } -function testAsyncAux(modifiers, fn) { +function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); const nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return value => Promise.resolve(pickFn(fn, 'async')(value)); } @@ -284,8 +285,41 @@ const availableModifiers = { async: fn => value => Promise.all(split(value).map(fn)).then(result => result.every(Boolean)), }, + + strict: { + simple: (fn, rule) => value => { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }, + async: (fn, rule) => value => + Promise.resolve(fn(value)) + .then(result => { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(() => false), + }, }; +function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); +} + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.esm.browser.min.js b/dist/v8n.esm.browser.min.js index 441b7bd..0a74386 100644 --- a/dist/v8n.esm.browser.min.js +++ b/dist/v8n.esm.browser.min.js @@ -1,2 +1,2 @@ -class e{constructor(e,t,n,r){this.name=e,this.fn=t,this.args=n,this.modifiers=r}_test(e){let t=this.fn;try{n(this.modifiers.slice(),t)(e)}catch(e){t=()=>!1}try{return n(this.modifiers.slice(),t)(e)}catch(e){return!1}}_check(e){try{n(this.modifiers.slice(),this.fn)(e)}catch(e){if(n(this.modifiers.slice(),(e=>e))(!1))return}if(!n(this.modifiers.slice(),this.fn)(e))throw null}_testAsync(e){return new Promise(((t,n)=>{r(this.modifiers.slice(),this.fn)(e).then((r=>{r?t(e):n(null)})).catch((e=>n(e)))}))}}function t(e,t="simple"){return"object"==typeof e?e[t]:e}function n(e,r){if(e.length){const t=e.shift(),s=n(e,r);return t.perform(s)}return t(r)}function r(e,n){if(e.length){const t=e.shift(),s=r(e,n);return t.performAsync(s)}return e=>Promise.resolve(t(n,"async")(e))}class s{constructor(e,t,n){this.name=e,this.perform=t,this.performAsync=n}}class i extends Error{constructor(e,t,n,r,...s){super(s),Error.captureStackTrace&&Error.captureStackTrace(this,i),this.rule=e,this.value=t,this.cause=n,this.target=r}}class c{constructor(e=[],t=[]){this.chain=e,this.nextRuleModifiers=t}_applyRule(t,n){return(...r)=>(this.chain.push(new e(n,t.apply(this,r),r,this.nextRuleModifiers)),this.nextRuleModifiers=[],this)}_applyModifier(e,t){return this.nextRuleModifiers.push(new s(t,e.simple,e.async)),this}_clone(){return new c(this.chain.slice(),this.nextRuleModifiers.slice())}test(e){return this.chain.every((t=>t._test(e)))}testAll(e){const t=[];return this.chain.forEach((n=>{try{n._check(e)}catch(r){t.push(new i(n,e,r))}})),t}check(e){this.chain.forEach((t=>{try{t._check(e)}catch(n){throw new i(t,e,n)}}))}testAsync(e){return new Promise(((t,n)=>{o(e,this.chain.slice(),t,n)}))}}function o(e,t,n,r){if(t.length){const s=t.shift();s._testAsync(e).then((()=>{o(e,t,n,r)}),(t=>{r(new i(s,e,t))}))}else n(e)}function a(){return void 0!==typeof Proxy?l(new c):u(new c)}let h={};function l(e){return new Proxy(e,{get(t,n){if(n in t)return t[n];const r=l(e._clone());return n in f?r._applyModifier(f[n],n):n in h?r._applyRule(h[n],n):n in y?r._applyRule(y[n],n):void 0}})}function u(e){const t=(e,t)=>(Object.keys(e).forEach((n=>{t[n]=(...r)=>u(t._clone())._applyRule(e[n],n)(...r)})),t),n=t(y,e),r=t(h,n);return Object.keys(f).forEach((e=>{Object.defineProperty(r,e,{get:()=>u(r._clone())._applyModifier(f[e],e)})})),r}a.extend=function(e){Object.assign(h,e)},a.clearCustomRules=function(){h={}};const f={not:{simple:e=>t=>!e(t),async:e=>t=>Promise.resolve(e(t)).then((e=>!e)).catch((()=>!0))},some:{simple:e=>t=>p(t).some((t=>{try{return e(t)}catch(e){return!1}})),async:e=>t=>Promise.all(p(t).map((t=>{try{return e(t).catch((()=>!1))}catch(e){return!1}}))).then((e=>e.some(Boolean)))},every:{simple:e=>t=>!1!==t&&p(t).every(e),async:e=>t=>Promise.all(p(t).map(e)).then((e=>e.every(Boolean)))}};function p(e){return"string"==typeof e?e.split(""):e}const y={equal:e=>t=>t==e,exact:e=>t=>t===e,number:(e=!0)=>t=>"number"==typeof t&&(e||isFinite(t)),integer:()=>e=>(Number.isInteger||d)(e),numeric:()=>e=>!isNaN(parseFloat(e))&&isFinite(e),string:()=>g("string"),boolean:()=>g("boolean"),undefined:()=>g("undefined"),null:()=>g("null"),array:()=>g("array"),object:()=>g("object"),instanceOf:e=>t=>t instanceof e,pattern:e=>t=>e.test(t),lowercase:()=>e=>/^([a-z]+\s*)+$/.test(e),uppercase:()=>e=>/^([A-Z]+\s*)+$/.test(e),vowel:()=>e=>/^[aeiou]+$/i.test(e),consonant:()=>e=>/^(?=[^aeiou])([a-z]+)$/i.test(e),first:e=>t=>t[0]==e,last:e=>t=>t[t.length-1]==e,empty:()=>e=>0===e.length,length:(e,t)=>n=>n.length>=e&&n.length<=(t||e),minLength:e=>t=>t.length>=e,maxLength:e=>t=>t.length<=e,negative:()=>e=>e<0,positive:()=>e=>e>=0,between:(e,t)=>n=>n>=e&&n<=t,range:(e,t)=>n=>n>=e&&n<=t,lessThan:e=>t=>tt=>t<=e,greaterThan:e=>t=>t>e,greaterThanOrEqual:e=>t=>t>=e,even:()=>e=>e%2==0,odd:()=>e=>e%2!=0,includes:e=>t=>~t.indexOf(e),schema:e=>function(e){return{simple:t=>{const n=[];if(Object.keys(e).forEach((r=>{const s=e[r];try{s.check((t||{})[r])}catch(e){e.target=r,n.push(e)}})),n.length>0)throw n;return!0},async:t=>{const n=[],r=Object.keys(e).map((r=>e[r].testAsync((t||{})[r]).catch((e=>{e.target=r,n.push(e)}))));return Promise.all(r).then((()=>{if(n.length>0)throw n;return!0}))}}}(e),passesAnyOf:(...e)=>t=>e.some((e=>e.test(t))),optional:m(!1),optionalAsync:m(!0)};function m(e){return(t,n=!1)=>r=>{if(n&&"string"==typeof r&&""===r.trim())return!0;if(null!=r){if(e)return t.testAsync(r);t.check(r)}return!0}}function g(e){return t=>Array.isArray(t)&&"array"===e||null===t&&"null"===e||typeof t===e}function d(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}export default a; +class e{constructor(e,t,n,s){this.name=e,this.fn=t,this.args=n,this.modifiers=s}_test(e){let t=this.fn;try{n(this.modifiers.slice(),t,this)(e)}catch(e){t=()=>!1}try{return n(this.modifiers.slice(),t,this)(e)}catch(e){return!1}}_check(e){try{n(this.modifiers.slice(),this.fn,this)(e)}catch(e){if(n(this.modifiers.slice(),(e=>e),this)(!1))return}if(!n(this.modifiers.slice(),this.fn,this)(e))throw null}_testAsync(e){return new Promise(((t,n)=>{s(this.modifiers.slice(),this.fn,this)(e).then((s=>{s?t(e):n(null)})).catch((e=>n(e)))}))}}function t(e,t="simple"){return"object"==typeof e?e[t]:e}function n(e,s,r){if(e.length){const t=e.shift(),i=n(e,s);return t.perform(i,r)}return t(s)}function s(e,n,r){if(e.length){const t=e.shift(),i=s(e,n);return t.performAsync(i,r)}return e=>Promise.resolve(t(n,"async")(e))}class r{constructor(e,t,n){this.name=e,this.perform=t,this.performAsync=n}}class i extends Error{constructor(e,t,n,s,...r){super(r),Error.captureStackTrace&&Error.captureStackTrace(this,i),this.rule=e,this.value=t,this.cause=n,this.target=s}}class c{constructor(e=[],t=[]){this.chain=e,this.nextRuleModifiers=t}_applyRule(t,n){return(...s)=>(this.chain.push(new e(n,t.apply(this,s),s,this.nextRuleModifiers)),this.nextRuleModifiers=[],this)}_applyModifier(e,t){return this.nextRuleModifiers.push(new r(t,e.simple,e.async)),this}_clone(){return new c(this.chain.slice(),this.nextRuleModifiers.slice())}test(e){return this.chain.every((t=>t._test(e)))}testAll(e){const t=[];return this.chain.forEach((n=>{try{n._check(e)}catch(s){t.push(new i(n,e,s))}})),t}check(e){this.chain.forEach((t=>{try{t._check(e)}catch(n){throw new i(t,e,n)}}))}testAsync(e){return new Promise(((t,n)=>{o(e,this.chain.slice(),t,n)}))}}function o(e,t,n,s){if(t.length){const r=t.shift();r._testAsync(e).then((()=>{o(e,t,n,s)}),(t=>{s(new i(r,e,t))}))}else n(e)}function h(){return void 0!==typeof Proxy?l(new c):u(new c)}let a={};function l(e){return new Proxy(e,{get(t,n){if(n in t)return t[n];const s=l(e._clone());return n in f?s._applyModifier(f[n],n):n in a?s._applyRule(a[n],n):n in m?s._applyRule(m[n],n):void 0}})}function u(e){const t=(e,t)=>(Object.keys(e).forEach((n=>{t[n]=(...s)=>u(t._clone())._applyRule(e[n],n)(...s)})),t),n=t(m,e),s=t(a,n);return Object.keys(f).forEach((e=>{Object.defineProperty(s,e,{get:()=>u(s._clone())._applyModifier(f[e],e)})})),s}h.extend=function(e){Object.assign(a,e)},h.clearCustomRules=function(){a={}};const f={not:{simple:e=>t=>!e(t),async:e=>t=>Promise.resolve(e(t)).then((e=>!e)).catch((()=>!0))},some:{simple:e=>t=>p(t).some((t=>{try{return e(t)}catch(e){return!1}})),async:e=>t=>Promise.all(p(t).map((t=>{try{return e(t).catch((()=>!1))}catch(e){return!1}}))).then((e=>e.some(Boolean)))},every:{simple:e=>t=>!1!==t&&p(t).every(e),async:e=>t=>Promise.all(p(t).map(e)).then((e=>e.every(Boolean)))},strict:{simple:(e,t)=>n=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e(n):e(n),async:(e,t)=>n=>Promise.resolve(e(n)).then((e=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e:e)).catch((()=>!1))}};function y(e){return e&&"schema"===e.name&&e.args.length>0&&"object"==typeof e.args[0]}function p(e){return"string"==typeof e?e.split(""):e}const m={equal:e=>t=>t==e,exact:e=>t=>t===e,number:(e=!0)=>t=>"number"==typeof t&&(e||isFinite(t)),integer:()=>e=>(Number.isInteger||b)(e),numeric:()=>e=>!isNaN(parseFloat(e))&&isFinite(e),string:()=>d("string"),boolean:()=>d("boolean"),undefined:()=>d("undefined"),null:()=>d("null"),array:()=>d("array"),object:()=>d("object"),instanceOf:e=>t=>t instanceof e,pattern:e=>t=>e.test(t),lowercase:()=>e=>/^([a-z]+\s*)+$/.test(e),uppercase:()=>e=>/^([A-Z]+\s*)+$/.test(e),vowel:()=>e=>/^[aeiou]+$/i.test(e),consonant:()=>e=>/^(?=[^aeiou])([a-z]+)$/i.test(e),first:e=>t=>t[0]==e,last:e=>t=>t[t.length-1]==e,empty:()=>e=>0===e.length,length:(e,t)=>n=>n.length>=e&&n.length<=(t||e),minLength:e=>t=>t.length>=e,maxLength:e=>t=>t.length<=e,negative:()=>e=>e<0,positive:()=>e=>e>=0,between:(e,t)=>n=>n>=e&&n<=t,range:(e,t)=>n=>n>=e&&n<=t,lessThan:e=>t=>tt=>t<=e,greaterThan:e=>t=>t>e,greaterThanOrEqual:e=>t=>t>=e,even:()=>e=>e%2==0,odd:()=>e=>e%2!=0,includes:e=>t=>~t.indexOf(e),schema:e=>function(e){return{simple:t=>{const n=[];if(Object.keys(e).forEach((s=>{const r=e[s];try{r.check((t||{})[s])}catch(e){e.target=s,n.push(e)}})),n.length>0)throw n;return!0},async:t=>{const n=[],s=Object.keys(e).map((s=>e[s].testAsync((t||{})[s]).catch((e=>{e.target=s,n.push(e)}))));return Promise.all(s).then((()=>{if(n.length>0)throw n;return!0}))}}}(e),passesAnyOf:(...e)=>t=>e.some((e=>e.test(t))),optional:g(!1),optionalAsync:g(!0)};function g(e){return(t,n=!1)=>s=>{if(n&&"string"==typeof s&&""===s.trim())return!0;if(null!=s){if(e)return t.testAsync(s);t.check(s)}return!0}}function d(e){return t=>Array.isArray(t)&&"array"===e||null===t&&"null"===e||typeof t===e}function b(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}export default h; //# sourceMappingURL=v8n.esm.browser.min.js.map diff --git a/dist/v8n.esm.browser.min.js.map b/dist/v8n.esm.browser.min.js.map index dd9fbf0..abb5eea 100644 --- a/dist/v8n.esm.browser.min.js.map +++ b/dist/v8n.esm.browser.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.esm.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n};\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","[object Object]","name","fn","args","modifiers","this","value","testAux","slice","ex","it","Promise","resolve","reject","testAsyncAux","then","valid","catch","pickFn","variant","length","modifier","shift","nextFn","perform","performAsync","Modifier","ValidationError","Error","rule","cause","target","remaining","super","captureStackTrace","Context","chain","nextRuleModifiers","ruleFn","push","apply","simple","async","every","_test","err","forEach","_check","executeAsyncRules","rules","_testAsync","v8n","undefined","Proxy","proxyContext","proxylessContext","customRules","context","obj","prop","newContext","_clone","availableModifiers","_applyModifier","_applyRule","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithAvailableRules","contextWithAllRules","defineProperty","get","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","test","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","check","nested","testAsync","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"AAAA,MAAMA,EACJC,YAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,EAGnBJ,MAAMM,GACJ,IAAIJ,EAAKG,KAAKH,GAEd,IACEK,EAAQF,KAAKD,UAAUI,QAASN,EAAhCK,CAAoCD,GACpC,MAAOG,GACPP,EAAK,KAAM,EAGb,IACE,OAAOK,EAAQF,KAAKD,UAAUI,QAASN,EAAhCK,CAAoCD,GAC3C,MAAOG,GACP,OAAO,GAIXT,OAAOM,GACL,IACEC,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAArCK,CAAyCD,GACzC,MAAOG,GACP,GAAIF,EAAQF,KAAKD,UAAUI,SAASE,GAAMA,GAAtCH,EAA0C,GAC5C,OAIJ,IAAKA,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAArCK,CAAyCD,GAC5C,MAAM,KAIVN,WAAWM,GACT,OAAO,IAAIK,SAAQ,CAACC,EAASC,KAC3BC,EACET,KAAKD,UAAUI,QACfH,KAAKH,GAFPY,CAGER,GACCS,MAAKC,IACAA,EACFJ,EAAQN,GAERO,EAAO,SAGVI,OAAMR,GAAMI,EAAOJ,SAK5B,SAASS,EAAOhB,EAAIiB,EAAU,UAC5B,MAAqB,iBAAPjB,EAAkBA,EAAGiB,GAAWjB,EAGhD,SAASK,EAAQH,EAAWF,GAC1B,GAAIE,EAAUgB,OAAQ,CACpB,MAAMC,EAAWjB,EAAUkB,QACrBC,EAAShB,EAAQH,EAAWF,GAClC,OAAOmB,EAASG,QAAQD,GAExB,OAAOL,EAAOhB,GAIlB,SAASY,EAAaV,EAAWF,GAC/B,GAAIE,EAAUgB,OAAQ,CACpB,MAAMC,EAAWjB,EAAUkB,QACrBC,EAAST,EAAaV,EAAWF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js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","[object Object]","name","fn","args","modifiers","this","value","testAux","slice","ex","it","Promise","resolve","reject","testAsyncAux","then","valid","catch","pickFn","variant","rule","length","modifier","shift","nextFn","perform","performAsync","Modifier","ValidationError","Error","cause","target","remaining","super","captureStackTrace","Context","chain","nextRuleModifiers","ruleFn","push","apply","simple","async","every","_test","err","forEach","_check","executeAsyncRules","rules","_testAsync","v8n","undefined","Proxy","proxyContext","proxylessContext","customRules","context","obj","prop","newContext","_clone","availableModifiers","_applyModifier","_applyRule","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithAvailableRules","contextWithAllRules","defineProperty","get","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","test","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","check","nested","testAsync","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"AAAA,MAAMA,EACJC,YAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,EAGnBJ,MAAMM,GACJ,IAAIJ,EAAKG,KAAKH,GAEd,IACEK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GAC1C,MAAOG,GACPP,EAAK,KAAM,EAGb,IACE,OAAOK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GACjD,MAAOG,GACP,OAAO,GAIXT,OAAOM,GACL,IACEC,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAC/C,MAAOG,GACP,GAAIF,EAAQF,KAAKD,UAAUI,SAASE,GAAMA,GAAIL,KAA1CE,EAAgD,GAClD,OAIJ,IAAKA,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAClD,MAAM,KAIVN,WAAWM,GACT,OAAO,IAAIK,SAAQ,CAACC,EAASC,KAC3BC,EACET,KAAKD,UAAUI,QACfH,KAAKH,GACLG,KAHFS,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,OAAS6G,EACZD,EAAOpG,KAAK9B,OAGZkI,EAAOtH,OAAS,EAClB,MAAMsH,EAER,OAAO,GAETjG,MAAOpC,IACL,MAAMqI,EAAS,GACTI,EAAS1E,OAAOC,KAAKoE,GAAQrD,KAAIuD,GACZF,EAAOE,GACRI,WAAW1I,GAAS,IAAIsI,IAAM3H,OAAMR,IAC1DA,EAAGsB,OAAS6G,EACZD,EAAOpG,KAAK9B,QAGhB,OAAOE,QAAQyE,IAAI2D,GAAQhI,MAAK,KAC9B,GAAI4H,EAAOtH,OAAS,EAClB,MAAMsH,EAGR,OAAO,OAhFKM,CAAWP,GAI7BQ,YAAa,IAAIC,IAAgB7I,GAC/B6I,EAAYlE,MAAKmE,GAAcA,EAAWrC,KAAKzG,KAEjD+I,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,MAAO,CAACJ,EAAYK,GAA6B,IAAUnJ,IACzD,GACEmJ,GACiB,iBAAVnJ,GACU,KAAjBA,EAAMoJ,OAEN,OAAO,EAGT,GAAIpJ,MAAAA,EAAuC,CACzC,GAAKkJ,EAGH,OAAOJ,EAAWJ,UAAU1I,GAF5B8I,EAAWN,MAAMxI,GAKrB,OAAO,GAIX,SAASiG,EAASb,GAChB,OAAOpF,GAEFqJ,MAAMC,QAAQtJ,IAAuB,UAAboF,GACd,OAAVpF,GAA+B,SAAboF,UACZpF,IAAUoF,EAKvB,SAASQ,EAAkB5F,GACzB,MACmB,iBAAVA,GAAsBwF,SAASxF,IAAUuJ,KAAKC,MAAMxJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.esm.js b/dist/v8n.esm.js index e33b2e2..1c20946 100644 --- a/dist/v8n.esm.js +++ b/dist/v8n.esm.js @@ -9,13 +9,13 @@ Rule.prototype._test = function _test (value) { var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -23,14 +23,14 @@ Rule.prototype._test = function _test (value) { Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -41,7 +41,8 @@ Rule.prototype._testAsync = function _testAsync (value) { return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -60,21 +61,21 @@ function pickFn(fn, variant) { return typeof fn === 'object' ? fn[variant] : fn; } -function testAux(modifiers, fn) { +function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } -function testAsyncAux(modifiers, fn) { +function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -302,8 +303,40 @@ var availableModifiers = { simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; +function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); +} + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.min.js b/dist/v8n.min.js index b3727ed..e6ad50a 100644 --- a/dist/v8n.min.js +++ b/dist/v8n.min.js @@ -1,2 +1,2 @@ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).v8n=t()}(this,(function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e){if(n.length){var u=n.shift(),i=r(n,e);return u.perform(i)}return t(e)}function e(n,r){if(n.length){var u=n.shift(),i=e(n,r);return u.performAsync(i)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}))(!1))return}if(!r(this.modifiers.slice(),this.fn)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):l(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in h?e._applyModifier(h[r],r):r in s?e._applyRule(s[r],r):r in y?e._applyRule(y[r],r):void 0}})}function l(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=l(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(y,n),e=t(s,r);return Object.keys(h).forEach((function(n){Object.defineProperty(e,n,{get:function(){return l(e._clone())._applyModifier(h[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var h={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return p(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(p(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&p(t).every(n)}},async:function(n){return function(t){return Promise.all(p(t).map(n)).then((function(n){return n.every(Boolean)}))}}}};function p(n){return"string"==typeof n?n.split(""):n}var y={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||v)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return d("string")},boolean:function(){return d("boolean")},undefined:function(){return d("undefined")},null:function(){return d("null")},array:function(){return d("array")},object:function(){return d("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:m(!1),optionalAsync:m(!0)};function m(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function d(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function v(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f})); +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).v8n=t()}(this,(function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return v("string")},boolean:function(){return v("boolean")},undefined:function(){return v("undefined")},null:function(){return v("null")},array:function(){return v("array")},object:function(){return v("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:g(!1),optionalAsync:g(!0)};function g(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function v(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f})); //# sourceMappingURL=v8n.min.js.map diff --git a/dist/v8n.min.js.map b/dist/v8n.min.js.map index bdb7f34..427c8c4 100644 --- a/dist/v8n.min.js.map +++ b/dist/v8n.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n};\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","rule","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"oOAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAK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,IAAI+H,IACrC,MAAOzH,GACPA,EAAGY,OAAS6G,EACZD,EAAO3F,KAAK7B,OAGZwH,EAAOtI,OAAS,EAClB,MAAMsI,EAER,OAAO,GAETvF,eAAOvC,GACLP,IAAMqI,EAAS,GACTG,EAAStE,OAAOC,KAAKiE,GAAQlD,cAAIoD,GAErC,OADyBF,EAAOE,GACRhF,WAAW/C,GAAS,IAAI+H,IAAMlH,gBAAMP,GAC1DA,EAAGY,OAAS6G,EACZD,EAAO3F,KAAK7B,SAGhB,OAAOL,QAAQyE,IAAIuD,GAAQtH,iBACzB,GAAImH,EAAOtI,OAAS,EAClB,MAAMsI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCnI,UAC/BoI,EAAY7D,eAAK8D,UAAcA,EAAW5F,KAAKzC,QAEjDsI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU1I,GACzD,GACE0I,GACiB,iBAAV1I,GACU,KAAjBA,EAAM2I,OAEN,OAAO,EAGT,GAAI3I,MAAAA,EAAuC,CACzC,GAAKyI,EAGH,OAAOJ,EAAWtF,UAAU/C,GAF5BqI,EAAWvF,MAAM9C,GAKrB,OAAO,IAIX,SAAS2F,EAASb,GAChB,gBAAO9E,GACL,OACG4I,MAAMC,QAAQ7I,IAAuB,UAAb8E,GACd,OAAV9E,GAA+B,SAAb8E,UACZ9E,IAAU8E,GAKvB,SAASQ,EAAkBtF,GACzB,MACmB,iBAAVA,GAAsBkF,SAASlF,IAAU8I,KAAKC,MAAM/I,KAAWA"} \ No newline at end of file +{"version":3,"file":"v8n.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"oOAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,GAClC,OAAOU,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,GACvC,OAAOU,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAIpB,EAAKG,KAAKH,GAEd,IACEM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GAC1C,MAAOM,GACPtB,oBAAW,GAGb,IACE,OAAOM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GACjD,MAAOM,GACP,OAAO,gBAIXC,gBAAOP,GACL,IACEV,EAAQH,KAAKD,UAAUmB,QAASlB,KAAKH,GAA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F,WAAW9C,GAAS,IAAIgI,IAAMnH,gBAAMP,GAC1DA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,SAGhB,OAAOL,QAAQwE,IAAIyD,GAAQvH,iBACzB,GAAIoH,EAAOvI,OAAS,EAClB,MAAMuI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.system.js b/dist/v8n.system.js index cac9053..af6cb91 100644 --- a/dist/v8n.system.js +++ b/dist/v8n.system.js @@ -16,13 +16,13 @@ System.register('v8n', [], function (exports) { var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -30,14 +30,14 @@ System.register('v8n', [], function (exports) { Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -48,7 +48,8 @@ System.register('v8n', [], function (exports) { return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -67,21 +68,21 @@ System.register('v8n', [], function (exports) { return typeof fn === 'object' ? fn[variant] : fn; } - function testAux(modifiers, fn) { + function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } - function testAsyncAux(modifiers, fn) { + function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -309,8 +310,40 @@ System.register('v8n', [], function (exports) { simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; + function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); + } + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/dist/v8n.umd.js b/dist/v8n.umd.js index 24e98e7..f2ef1b9 100644 --- a/dist/v8n.umd.js +++ b/dist/v8n.umd.js @@ -15,13 +15,13 @@ var fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = function () { return false; }; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex$1) { return false; } @@ -29,14 +29,14 @@ Rule.prototype._check = function _check (value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), function (it) { return it; })(false)) { + if (testAux(this.modifiers.slice(), function (it) { return it; }, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } }; @@ -47,7 +47,8 @@ return new Promise(function (resolve, reject) { testAsyncAux( this$1.modifiers.slice(), - this$1.fn + this$1.fn, + this$1 )(value) .then(function (valid) { if (valid) { @@ -66,21 +67,21 @@ return typeof fn === 'object' ? fn[variant] : fn; } - function testAux(modifiers, fn) { + function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } - function testAsyncAux(modifiers, fn) { + function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); var nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; } @@ -308,8 +309,40 @@ simple: function (fn) { return function (value) { return value !== false && split(value).every(fn); }; }, async: function (fn) { return function (value) { return Promise.all(split(value).map(fn)).then(function (result) { return result.every(Boolean); }); }; }, }, + + strict: { + simple: function (fn, rule) { return function (value) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }; }, + async: function (fn, rule) { return function (value) { return Promise.resolve(fn(value)) + .then(function (result) { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(function () { return false; }); }; }, + }, }; + function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); + } + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/docs/api/README.md b/docs/api/README.md index 6cb244c..9cfe9ff 100644 --- a/docs/api/README.md +++ b/docs/api/README.md @@ -79,15 +79,16 @@ sidebar: auto - **Properties:** - - `fork: Function` - - `exec: Function` + - `name: string` + - `perform: Function` + - `performAsync: Function` - **Details:** This class represents a modifier. It is contained in an array within the - `modifiers` property of a [`Rule`](#rule). `fork` is a helper function for - mapping the value to the function correctly. `exec` performs the actual - modification of the modifier. + `modifiers` property of a [`Rule`](#rule). The functions `perform` and + `performAsync` are used to apply the modifier to a rule depending on the + validation strategy required. - **See also:** [Rule](#rule) @@ -1037,6 +1038,12 @@ sidebar: auto values of the given object are then validated based on the validation specified for it's key in the schema. + ::: info + Schemas are validated like interfaces. That means, while they enforce that the + given schema is met, it also allows additional properties on the object. This + behaviour can be changed by using the [`strict`](#strict) modifier. + ::: + ```js const validation = v8n().schema({ id: v8n() @@ -1234,3 +1241,33 @@ This rule is asynchronous and requires the [`testAsync` validation strategy](#te .every.positive() .test(1, 2, -3); // false ``` + +### strict + +- **Applicable Types:** `object` + +- **Applicable Rules:** `schema` + +- **Usage:** + + This modifier makes checks for object schemas strict. This means that + the schema must match the object exactly and no extraneous properties + may be included. For other rules this modifier will not have any effect. + + ```js + v8n() + .strict.schema({ + id: v8n() + .number() + .positive(), + }) + .test({ id: 2 }); // true + + v8n() + .strict.schema({ + id: v8n() + .number() + .positive(), + }) + .test({ id: 2, name: 'Luke' }); // false + ``` diff --git a/src/Rule.js b/src/Rule.js index 4f290a3..c4d8c2b 100644 --- a/src/Rule.js +++ b/src/Rule.js @@ -10,13 +10,13 @@ class Rule { let fn = this.fn; try { - testAux(this.modifiers.slice(), fn)(value); + testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { fn = () => false; } try { - return testAux(this.modifiers.slice(), fn)(value); + return testAux(this.modifiers.slice(), fn, this)(value); } catch (ex) { return false; } @@ -24,14 +24,14 @@ class Rule { _check(value) { try { - testAux(this.modifiers.slice(), this.fn)(value); + testAux(this.modifiers.slice(), this.fn, this)(value); } catch (ex) { - if (testAux(this.modifiers.slice(), it => it)(false)) { + if (testAux(this.modifiers.slice(), it => it, this)(false)) { return; } } - if (!testAux(this.modifiers.slice(), this.fn)(value)) { + if (!testAux(this.modifiers.slice(), this.fn, this)(value)) { throw null; } } @@ -41,6 +41,7 @@ class Rule { testAsyncAux( this.modifiers.slice(), this.fn, + this, )(value) .then(valid => { if (valid) { @@ -58,21 +59,21 @@ function pickFn(fn, variant = 'simple') { return typeof fn === 'object' ? fn[variant] : fn; } -function testAux(modifiers, fn) { +function testAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); const nextFn = testAux(modifiers, fn); - return modifier.perform(nextFn); + return modifier.perform(nextFn, rule); } else { return pickFn(fn); } } -function testAsyncAux(modifiers, fn) { +function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); const nextFn = testAsyncAux(modifiers, fn); - return modifier.performAsync(nextFn); + return modifier.performAsync(nextFn, rule); } else { return value => Promise.resolve(pickFn(fn, 'async')(value)); } diff --git a/src/v8n.js b/src/v8n.js index be23851..589d347 100644 --- a/src/v8n.js +++ b/src/v8n.js @@ -109,8 +109,41 @@ const availableModifiers = { async: fn => value => Promise.all(split(value).map(fn)).then(result => result.every(Boolean)), }, + + strict: { + simple: (fn, rule) => value => { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + fn(value) + ); + } + return fn(value); + }, + async: (fn, rule) => value => + Promise.resolve(fn(value)) + .then(result => { + if (isSchemaRule(rule) && value && typeof value === 'object') { + return ( + Object.keys(rule.args[0]).length === Object.keys(value).length && + result + ); + } + return result; + }) + .catch(() => false), + }, }; +function isSchemaRule(rule) { + return ( + rule && + rule.name === 'schema' && + rule.args.length > 0 && + typeof rule.args[0] === 'object' + ); +} + function split(value) { if (typeof value === 'string') { return value.split(''); diff --git a/src/v8n.test.js b/src/v8n.test.js index 9c6a748..81b7d52 100644 --- a/src/v8n.test.js +++ b/src/v8n.test.js @@ -431,6 +431,71 @@ describe('modifiers', () => { ).rejects.toBeDefined(); }); + describe("the 'strict' modifier", () => { + it('makes the schema rule strict', () => { + const validation = v8n().strict.schema({ + id: v8n() + .number() + .positive(), + name: v8n() + .string() + .minLength(4), + }); + + expect( + validation.test({ + id: 1, + name: 'Luke', + }), + ).toBeTruthy(); + + expect( + validation.test({ + id: 1, + }), + ).toBeFalsy(); + + expect( + validation.test({ + id: 1, + name: 'Luke', + lastname: 'Skywalker', + }), + ).toBeFalsy(); + }); + + it('makes the schema rule strict asynchronously', async () => { + v8n.extend({ asyncRule }); + + const validation = v8n().strict.schema({ + id: v8n() + .number() + .asyncRule([10, 20]), + }); + + const valid = { + id: 10, + }; + + const invalidValue = { + id: 1, + }; + + const invalidExtraProp = { + id: 1, + name: 'Luke', + }; + + await expect(validation.testAsync(valid)).resolves.toBe(valid); + await expect(validation.testAsync(invalidValue)).rejects.toBeInstanceOf( + ValidationError, + ); + await expect( + validation.testAsync(invalidExtraProp), + ).rejects.toBeInstanceOf(ValidationError); + }); + }); + it('expect that error if formatted correctly', async () => { const validation = v8n().schema({ item: v8n().every.schema({ diff --git a/types/modifier.d.ts b/types/modifier.d.ts index 3c8bf6e..2a5c385 100644 --- a/types/modifier.d.ts +++ b/types/modifier.d.ts @@ -1,6 +1,6 @@ -import { AsyncValidator, SimpleValidator } from './rule'; +import { AsyncValidator, SimpleValidator, Rule } from './rule'; -export type Performer = (fn: T) => T; +export type Performer = (fn: T, rule?: Rule) => T; export interface Modifier { name: string; diff --git a/types/test/test-v8n.ts b/types/test/test-v8n.ts index e1f0176..bc4adf3 100644 --- a/types/test/test-v8n.ts +++ b/types/test/test-v8n.ts @@ -35,7 +35,7 @@ v8n() .even() .odd() .includes('test') - .schema({ test: 2 }) + .strict.schema({ test: 2 }) .passesAnyOf() .optional(v8n().string()) .test(12); diff --git a/types/v8n.d.ts b/types/v8n.d.ts index 02070cd..1ecbfee 100644 --- a/types/v8n.d.ts +++ b/types/v8n.d.ts @@ -10,6 +10,7 @@ export interface V8nValidator { not: V8nValidator; some: V8nValidator; every: V8nValidator; + strict: V8nValidator; equal(expected: any): V8nValidator; exact(expected: any): V8nValidator; number(allowInfinite?: boolean): V8nValidator; From 4bc24fffbb9ec0265baa0a481f4aa82c1d1f48a2 Mon Sep 17 00:00:00 2001 From: Sebastian Barfurth Date: Tue, 22 Feb 2022 16:40:19 +0100 Subject: [PATCH 2/3] docs: correct linked issue for strict schema validation --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63106ee..64ebfb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - New rule `optionalAsync` for asynchronous, optional validation ([#206](https://github.com/imbrn/v8n/issues/206)) -- New modifier `strict` for schema validation ([#191](https://github.com/imbrn/v8n/issues/191), [#164](https://github.com/imbrn/v8n/issues/164)) +- New modifier `strict` for schema validation ([#191](https://github.com/imbrn/v8n/issues/191), [#179](https://github.com/imbrn/v8n/issues/179)) ## [1.4.0] - 2022-02-17 From 87c9169b295e2c037f9170e1faf963b4625f5948 Mon Sep 17 00:00:00 2001 From: brfrth Date: Sun, 13 Mar 2022 12:42:39 +0100 Subject: [PATCH 3/3] test: add modifier combination tests for strict --- dist/v8n.amd.js | 4 +- dist/v8n.browser.js | 4 +- dist/v8n.browser.min.js | 2 +- dist/v8n.browser.min.js.map | 2 +- dist/v8n.cjs.js | 4 +- dist/v8n.esm.browser.js | 4 +- dist/v8n.esm.browser.min.js | 2 +- dist/v8n.esm.browser.min.js.map | 2 +- dist/v8n.esm.js | 4 +- dist/v8n.min.js | 2 +- dist/v8n.min.js.map | 2 +- dist/v8n.system.js | 4 +- dist/v8n.umd.js | 4 +- src/Rule.js | 4 +- src/v8n.test.js | 219 ++++++++++++++++++++++---------- 15 files changed, 176 insertions(+), 87 deletions(-) diff --git a/dist/v8n.amd.js b/dist/v8n.amd.js index af8b209..8b12a4d 100644 --- a/dist/v8n.amd.js +++ b/dist/v8n.amd.js @@ -66,7 +66,7 @@ define(function () { 'use strict'; function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -76,7 +76,7 @@ define(function () { 'use strict'; function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/dist/v8n.browser.js b/dist/v8n.browser.js index 377ffc4..c40e750 100644 --- a/dist/v8n.browser.js +++ b/dist/v8n.browser.js @@ -67,7 +67,7 @@ var v8n = (function () { function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -77,7 +77,7 @@ var v8n = (function () { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/dist/v8n.browser.min.js b/dist/v8n.browser.min.js index c46009a..1d686ae 100644 --- a/dist/v8n.browser.min.js +++ b/dist/v8n.browser.min.js @@ -1,2 +1,2 @@ -var v8n=function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return g("string")},boolean:function(){return g("boolean")},undefined:function(){return g("undefined")},null:function(){return g("null")},array:function(){return g("array")},object:function(){return g("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:v(!1),optionalAsync:v(!0)};function v(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function g(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f}(); +var v8n=function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e,u);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r,u);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return g("string")},boolean:function(){return g("boolean")},undefined:function(){return g("undefined")},null:function(){return g("null")},array:function(){return g("array")},object:function(){return g("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:v(!1),optionalAsync:v(!0)};function v(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function g(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f}(); //# sourceMappingURL=v8n.browser.min.js.map diff --git a/dist/v8n.browser.min.js.map b/dist/v8n.browser.min.js.map index ff0478d..50dedea 100644 --- a/dist/v8n.browser.min.js.map +++ b/dist/v8n.browser.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"gCAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,GAClC,OAAOU,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,GACvC,OAAOU,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAIpB,EAAKG,KAAKH,GAEd,IACEM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GAC1C,MAAOM,GACPtB,oBAAW,GAGb,IACE,OAAOM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GACjD,MAAOM,GACP,OAAO,gBAIXC,gBAAOP,GACL,IACEV,EAAQH,KAAKD,UAAUmB,QAASlB,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AAOE,GACRlF,WAAW9C,GAAS,IAAIgI,IAAMnH,gBAAMP,GAC1DA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,SAGhB,OAAOL,QAAQwE,IAAIyD,GAAQvH,iBACzB,GAAIoH,EAAOvI,OAAS,EAClB,MAAMuI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file +{"version":3,"file":"v8n.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn, rule);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn, rule);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"gCAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,EAAIO,GACtC,OAAOG,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,EAAIO,GAC3C,OAAOG,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAIpB,EAAKG,KAAKH,GAEd,IACEM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GAC1C,MAAOM,GACPtB,oBAAW,GAGb,IACE,OAAOM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GACjD,MAAOM,GACP,OAAO,gBAIXC,gBAAOP,GACL,IACEV,EAAQH,KAAKD,UAAUmB,QAASlB,KAAKH,GAAIG,KAAzCG,CAA+CU,GAC/C,MAAOM,GACP,GAAIhB,EAAQH,KAAKD,UAAUmB,kBAASG,UAAMA,IAAIrB,KAA1CG,EAAgD,GAClD,OAIJ,IAAKA,EAAQH,KAAKD,UAAUmB,QAASlB,KAAKH,GAAIG,KAAzCG,C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,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.cjs.js b/dist/v8n.cjs.js index 3a5899a..ff3ea1b 100644 --- a/dist/v8n.cjs.js +++ b/dist/v8n.cjs.js @@ -66,7 +66,7 @@ function pickFn(fn, variant) { function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -76,7 +76,7 @@ function testAux(modifiers, fn, rule) { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/dist/v8n.esm.browser.js b/dist/v8n.esm.browser.js index 28ec925..0dc6270 100644 --- a/dist/v8n.esm.browser.js +++ b/dist/v8n.esm.browser.js @@ -62,7 +62,7 @@ function pickFn(fn, variant = 'simple') { function testAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); - const nextFn = testAux(modifiers, fn); + const nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -72,7 +72,7 @@ function testAux(modifiers, fn, rule) { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); - const nextFn = testAsyncAux(modifiers, fn); + const nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return value => Promise.resolve(pickFn(fn, 'async')(value)); diff --git a/dist/v8n.esm.browser.min.js b/dist/v8n.esm.browser.min.js index 0a74386..78e3502 100644 --- a/dist/v8n.esm.browser.min.js +++ b/dist/v8n.esm.browser.min.js @@ -1,2 +1,2 @@ -class e{constructor(e,t,n,s){this.name=e,this.fn=t,this.args=n,this.modifiers=s}_test(e){let t=this.fn;try{n(this.modifiers.slice(),t,this)(e)}catch(e){t=()=>!1}try{return n(this.modifiers.slice(),t,this)(e)}catch(e){return!1}}_check(e){try{n(this.modifiers.slice(),this.fn,this)(e)}catch(e){if(n(this.modifiers.slice(),(e=>e),this)(!1))return}if(!n(this.modifiers.slice(),this.fn,this)(e))throw null}_testAsync(e){return new Promise(((t,n)=>{s(this.modifiers.slice(),this.fn,this)(e).then((s=>{s?t(e):n(null)})).catch((e=>n(e)))}))}}function t(e,t="simple"){return"object"==typeof e?e[t]:e}function n(e,s,r){if(e.length){const t=e.shift(),i=n(e,s);return t.perform(i,r)}return t(s)}function s(e,n,r){if(e.length){const t=e.shift(),i=s(e,n);return t.performAsync(i,r)}return e=>Promise.resolve(t(n,"async")(e))}class r{constructor(e,t,n){this.name=e,this.perform=t,this.performAsync=n}}class i extends Error{constructor(e,t,n,s,...r){super(r),Error.captureStackTrace&&Error.captureStackTrace(this,i),this.rule=e,this.value=t,this.cause=n,this.target=s}}class c{constructor(e=[],t=[]){this.chain=e,this.nextRuleModifiers=t}_applyRule(t,n){return(...s)=>(this.chain.push(new e(n,t.apply(this,s),s,this.nextRuleModifiers)),this.nextRuleModifiers=[],this)}_applyModifier(e,t){return this.nextRuleModifiers.push(new r(t,e.simple,e.async)),this}_clone(){return new c(this.chain.slice(),this.nextRuleModifiers.slice())}test(e){return this.chain.every((t=>t._test(e)))}testAll(e){const t=[];return this.chain.forEach((n=>{try{n._check(e)}catch(s){t.push(new i(n,e,s))}})),t}check(e){this.chain.forEach((t=>{try{t._check(e)}catch(n){throw new i(t,e,n)}}))}testAsync(e){return new Promise(((t,n)=>{o(e,this.chain.slice(),t,n)}))}}function o(e,t,n,s){if(t.length){const r=t.shift();r._testAsync(e).then((()=>{o(e,t,n,s)}),(t=>{s(new i(r,e,t))}))}else n(e)}function h(){return void 0!==typeof Proxy?l(new c):u(new c)}let a={};function l(e){return new Proxy(e,{get(t,n){if(n in t)return t[n];const s=l(e._clone());return n in f?s._applyModifier(f[n],n):n in a?s._applyRule(a[n],n):n in m?s._applyRule(m[n],n):void 0}})}function u(e){const t=(e,t)=>(Object.keys(e).forEach((n=>{t[n]=(...s)=>u(t._clone())._applyRule(e[n],n)(...s)})),t),n=t(m,e),s=t(a,n);return Object.keys(f).forEach((e=>{Object.defineProperty(s,e,{get:()=>u(s._clone())._applyModifier(f[e],e)})})),s}h.extend=function(e){Object.assign(a,e)},h.clearCustomRules=function(){a={}};const f={not:{simple:e=>t=>!e(t),async:e=>t=>Promise.resolve(e(t)).then((e=>!e)).catch((()=>!0))},some:{simple:e=>t=>p(t).some((t=>{try{return e(t)}catch(e){return!1}})),async:e=>t=>Promise.all(p(t).map((t=>{try{return e(t).catch((()=>!1))}catch(e){return!1}}))).then((e=>e.some(Boolean)))},every:{simple:e=>t=>!1!==t&&p(t).every(e),async:e=>t=>Promise.all(p(t).map(e)).then((e=>e.every(Boolean)))},strict:{simple:(e,t)=>n=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e(n):e(n),async:(e,t)=>n=>Promise.resolve(e(n)).then((e=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e:e)).catch((()=>!1))}};function y(e){return e&&"schema"===e.name&&e.args.length>0&&"object"==typeof e.args[0]}function p(e){return"string"==typeof e?e.split(""):e}const m={equal:e=>t=>t==e,exact:e=>t=>t===e,number:(e=!0)=>t=>"number"==typeof t&&(e||isFinite(t)),integer:()=>e=>(Number.isInteger||b)(e),numeric:()=>e=>!isNaN(parseFloat(e))&&isFinite(e),string:()=>d("string"),boolean:()=>d("boolean"),undefined:()=>d("undefined"),null:()=>d("null"),array:()=>d("array"),object:()=>d("object"),instanceOf:e=>t=>t instanceof e,pattern:e=>t=>e.test(t),lowercase:()=>e=>/^([a-z]+\s*)+$/.test(e),uppercase:()=>e=>/^([A-Z]+\s*)+$/.test(e),vowel:()=>e=>/^[aeiou]+$/i.test(e),consonant:()=>e=>/^(?=[^aeiou])([a-z]+)$/i.test(e),first:e=>t=>t[0]==e,last:e=>t=>t[t.length-1]==e,empty:()=>e=>0===e.length,length:(e,t)=>n=>n.length>=e&&n.length<=(t||e),minLength:e=>t=>t.length>=e,maxLength:e=>t=>t.length<=e,negative:()=>e=>e<0,positive:()=>e=>e>=0,between:(e,t)=>n=>n>=e&&n<=t,range:(e,t)=>n=>n>=e&&n<=t,lessThan:e=>t=>tt=>t<=e,greaterThan:e=>t=>t>e,greaterThanOrEqual:e=>t=>t>=e,even:()=>e=>e%2==0,odd:()=>e=>e%2!=0,includes:e=>t=>~t.indexOf(e),schema:e=>function(e){return{simple:t=>{const n=[];if(Object.keys(e).forEach((s=>{const r=e[s];try{r.check((t||{})[s])}catch(e){e.target=s,n.push(e)}})),n.length>0)throw n;return!0},async:t=>{const n=[],s=Object.keys(e).map((s=>e[s].testAsync((t||{})[s]).catch((e=>{e.target=s,n.push(e)}))));return Promise.all(s).then((()=>{if(n.length>0)throw n;return!0}))}}}(e),passesAnyOf:(...e)=>t=>e.some((e=>e.test(t))),optional:g(!1),optionalAsync:g(!0)};function g(e){return(t,n=!1)=>s=>{if(n&&"string"==typeof s&&""===s.trim())return!0;if(null!=s){if(e)return t.testAsync(s);t.check(s)}return!0}}function d(e){return t=>Array.isArray(t)&&"array"===e||null===t&&"null"===e||typeof t===e}function b(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}export default h; +class e{constructor(e,t,n,s){this.name=e,this.fn=t,this.args=n,this.modifiers=s}_test(e){let t=this.fn;try{n(this.modifiers.slice(),t,this)(e)}catch(e){t=()=>!1}try{return n(this.modifiers.slice(),t,this)(e)}catch(e){return!1}}_check(e){try{n(this.modifiers.slice(),this.fn,this)(e)}catch(e){if(n(this.modifiers.slice(),(e=>e),this)(!1))return}if(!n(this.modifiers.slice(),this.fn,this)(e))throw null}_testAsync(e){return new Promise(((t,n)=>{s(this.modifiers.slice(),this.fn,this)(e).then((s=>{s?t(e):n(null)})).catch((e=>n(e)))}))}}function t(e,t="simple"){return"object"==typeof e?e[t]:e}function n(e,s,r){if(e.length){const t=e.shift(),i=n(e,s,r);return t.perform(i,r)}return t(s)}function s(e,n,r){if(e.length){const t=e.shift(),i=s(e,n,r);return t.performAsync(i,r)}return e=>Promise.resolve(t(n,"async")(e))}class r{constructor(e,t,n){this.name=e,this.perform=t,this.performAsync=n}}class i extends Error{constructor(e,t,n,s,...r){super(r),Error.captureStackTrace&&Error.captureStackTrace(this,i),this.rule=e,this.value=t,this.cause=n,this.target=s}}class c{constructor(e=[],t=[]){this.chain=e,this.nextRuleModifiers=t}_applyRule(t,n){return(...s)=>(this.chain.push(new e(n,t.apply(this,s),s,this.nextRuleModifiers)),this.nextRuleModifiers=[],this)}_applyModifier(e,t){return this.nextRuleModifiers.push(new r(t,e.simple,e.async)),this}_clone(){return new c(this.chain.slice(),this.nextRuleModifiers.slice())}test(e){return this.chain.every((t=>t._test(e)))}testAll(e){const t=[];return this.chain.forEach((n=>{try{n._check(e)}catch(s){t.push(new i(n,e,s))}})),t}check(e){this.chain.forEach((t=>{try{t._check(e)}catch(n){throw new i(t,e,n)}}))}testAsync(e){return new Promise(((t,n)=>{o(e,this.chain.slice(),t,n)}))}}function o(e,t,n,s){if(t.length){const r=t.shift();r._testAsync(e).then((()=>{o(e,t,n,s)}),(t=>{s(new i(r,e,t))}))}else n(e)}function h(){return void 0!==typeof Proxy?l(new c):u(new c)}let a={};function l(e){return new Proxy(e,{get(t,n){if(n in t)return t[n];const s=l(e._clone());return n in f?s._applyModifier(f[n],n):n in a?s._applyRule(a[n],n):n in m?s._applyRule(m[n],n):void 0}})}function u(e){const t=(e,t)=>(Object.keys(e).forEach((n=>{t[n]=(...s)=>u(t._clone())._applyRule(e[n],n)(...s)})),t),n=t(m,e),s=t(a,n);return Object.keys(f).forEach((e=>{Object.defineProperty(s,e,{get:()=>u(s._clone())._applyModifier(f[e],e)})})),s}h.extend=function(e){Object.assign(a,e)},h.clearCustomRules=function(){a={}};const f={not:{simple:e=>t=>!e(t),async:e=>t=>Promise.resolve(e(t)).then((e=>!e)).catch((()=>!0))},some:{simple:e=>t=>p(t).some((t=>{try{return e(t)}catch(e){return!1}})),async:e=>t=>Promise.all(p(t).map((t=>{try{return e(t).catch((()=>!1))}catch(e){return!1}}))).then((e=>e.some(Boolean)))},every:{simple:e=>t=>!1!==t&&p(t).every(e),async:e=>t=>Promise.all(p(t).map(e)).then((e=>e.every(Boolean)))},strict:{simple:(e,t)=>n=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e(n):e(n),async:(e,t)=>n=>Promise.resolve(e(n)).then((e=>y(t)&&n&&"object"==typeof n?Object.keys(t.args[0]).length===Object.keys(n).length&&e:e)).catch((()=>!1))}};function y(e){return e&&"schema"===e.name&&e.args.length>0&&"object"==typeof e.args[0]}function p(e){return"string"==typeof e?e.split(""):e}const m={equal:e=>t=>t==e,exact:e=>t=>t===e,number:(e=!0)=>t=>"number"==typeof t&&(e||isFinite(t)),integer:()=>e=>(Number.isInteger||b)(e),numeric:()=>e=>!isNaN(parseFloat(e))&&isFinite(e),string:()=>d("string"),boolean:()=>d("boolean"),undefined:()=>d("undefined"),null:()=>d("null"),array:()=>d("array"),object:()=>d("object"),instanceOf:e=>t=>t instanceof e,pattern:e=>t=>e.test(t),lowercase:()=>e=>/^([a-z]+\s*)+$/.test(e),uppercase:()=>e=>/^([A-Z]+\s*)+$/.test(e),vowel:()=>e=>/^[aeiou]+$/i.test(e),consonant:()=>e=>/^(?=[^aeiou])([a-z]+)$/i.test(e),first:e=>t=>t[0]==e,last:e=>t=>t[t.length-1]==e,empty:()=>e=>0===e.length,length:(e,t)=>n=>n.length>=e&&n.length<=(t||e),minLength:e=>t=>t.length>=e,maxLength:e=>t=>t.length<=e,negative:()=>e=>e<0,positive:()=>e=>e>=0,between:(e,t)=>n=>n>=e&&n<=t,range:(e,t)=>n=>n>=e&&n<=t,lessThan:e=>t=>tt=>t<=e,greaterThan:e=>t=>t>e,greaterThanOrEqual:e=>t=>t>=e,even:()=>e=>e%2==0,odd:()=>e=>e%2!=0,includes:e=>t=>~t.indexOf(e),schema:e=>function(e){return{simple:t=>{const n=[];if(Object.keys(e).forEach((s=>{const r=e[s];try{r.check((t||{})[s])}catch(e){e.target=s,n.push(e)}})),n.length>0)throw n;return!0},async:t=>{const n=[],s=Object.keys(e).map((s=>e[s].testAsync((t||{})[s]).catch((e=>{e.target=s,n.push(e)}))));return Promise.all(s).then((()=>{if(n.length>0)throw n;return!0}))}}}(e),passesAnyOf:(...e)=>t=>e.some((e=>e.test(t))),optional:g(!1),optionalAsync:g(!0)};function g(e){return(t,n=!1)=>s=>{if(n&&"string"==typeof s&&""===s.trim())return!0;if(null!=s){if(e)return t.testAsync(s);t.check(s)}return!0}}function d(e){return t=>Array.isArray(t)&&"array"===e||null===t&&"null"===e||typeof t===e}function b(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e}export default h; //# sourceMappingURL=v8n.esm.browser.min.js.map diff --git a/dist/v8n.esm.browser.min.js.map b/dist/v8n.esm.browser.min.js.map index abb5eea..25258a5 100644 --- a/dist/v8n.esm.browser.min.js.map +++ b/dist/v8n.esm.browser.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.esm.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","[object Object]","name","fn","args","modifiers","this","value","testAux","slice","ex","it","Promise","resolve","reject","testAsyncAux","then","valid","catch","pickFn","variant","rule","length","modifier","shift","nextFn","perform","performAsync","Modifier","ValidationError","Error","cause","target","remaining","super","captureStackTrace","Context","chain","nextRuleModifiers","ruleFn","push","apply","simple","async","every","_test","err","forEach","_check","executeAsyncRules","rules","_testAsync","v8n","undefined","Proxy","proxyContext","proxylessContext","customRules","context","obj","prop","newContext","_clone","availableModifiers","_applyModifier","_applyRule","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithAvailableRules","contextWithAllRules","defineProperty","get","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","test","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","check","nested","testAsync","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"AAAA,MAAMA,EACJC,YAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,EAGnBJ,MAAMM,GACJ,IAAIJ,EAAKG,KAAKH,GAEd,IACEK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GAC1C,MAAOG,GACPP,EAAK,KAAM,EAGb,IACE,OAAOK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GACjD,MAAOG,GACP,OAAO,GAIXT,OAAOM,GACL,IACEC,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAC/C,MAAOG,GACP,GAAIF,EAAQF,KAAKD,UAAUI,SAASE,GAAMA,GAAIL,KAA1CE,EAAgD,GAClD,OAIJ,IAAKA,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAClD,MAAM,KAIVN,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C1B,MAAMC,EAAmBH,EAAOE,GAChC,IACEC,EAAiBC,OAAOxI,GAAS,IAAIsI,IACrC,MAAOnI,GACPA,EAAGsB,OAAS6G,EACZD,EAAOpG,KAAK9B,OAGZkI,EAAOtH,OAAS,EAClB,MAAMsH,EAER,OAAO,GAETjG,MAAOpC,IACL,MAAMqI,EAAS,GACTI,EAAS1E,OAAOC,KAAKoE,GAAQrD,KAAIuD,GACZF,EAAOE,GACRI,WAAW1I,GAAS,IAAIsI,IAAM3H,OAAMR,IAC1DA,EAAGsB,OAAS6G,EACZD,EAAOpG,KAAK9B,QAGhB,OAAOE,QAAQyE,IAAI2D,GAAQhI,MAAK,KAC9B,GAAI4H,EAAOtH,OAAS,EAClB,MAAMsH,EAGR,OAAO,OAhFKM,CAAWP,GAI7BQ,YAAa,IAAIC,IAAgB7I,GAC/B6I,EAAYlE,MAAKmE,GAAcA,EAAWrC,KAAKzG,KAEjD+I,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,MAAO,CAACJ,EAAYK,GAA6B,IAAUnJ,IACzD,GACEmJ,GACiB,iBAAVnJ,GACU,KAAjBA,EAAMoJ,OAEN,OAAO,EAGT,GAAIpJ,MAAAA,EAAuC,CACzC,GAAKkJ,EAGH,OAAOJ,EAAWJ,UAAU1I,GAF5B8I,EAAWN,MAAMxI,GAKrB,OAAO,GAIX,SAASiG,EAASb,GAChB,OAAOpF,GAEFqJ,MAAMC,QAAQtJ,IAAuB,UAAboF,GACd,OAAVpF,GAA+B,SAAboF,UACZpF,IAAUoF,EAKvB,SAASQ,EAAkB5F,GACzB,MACmB,iBAAVA,GAAsBwF,SAASxF,IAAUuJ,KAAKC,MAAMxJ,KAAWA"} \ No newline at end of file +{"version":3,"file":"v8n.esm.browser.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn, rule);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn, rule);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","[object Object]","name","fn","args","modifiers","this","value","testAux","slice","ex","it","Promise","resolve","reject","testAsyncAux","then","valid","catch","pickFn","variant","rule","length","modifier","shift","nextFn","perform","performAsync","Modifier","ValidationError","Error","cause","target","remaining","super","captureStackTrace","Context","chain","nextRuleModifiers","ruleFn","push","apply","simple","async","every","_test","err","forEach","_check","executeAsyncRules","rules","_testAsync","v8n","undefined","Proxy","proxyContext","proxylessContext","customRules","context","obj","prop","newContext","_clone","availableModifiers","_applyModifier","_applyRule","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithAvailableRules","contextWithAllRules","defineProperty","get","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","test","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","check","nested","testAsync","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"AAAA,MAAMA,EACJC,YAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,EAGnBJ,MAAMM,GACJ,IAAIJ,EAAKG,KAAKH,GAEd,IACEK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GAC1C,MAAOG,GACPP,EAAK,KAAM,EAGb,IACE,OAAOK,EAAQF,KAAKD,UAAUI,QAASN,EAAIG,KAApCE,CAA0CD,GACjD,MAAOG,GACP,OAAO,GAIXT,OAAOM,GACL,IACEC,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAC/C,MAAOG,GACP,GAAIF,EAAQF,KAAKD,UAAUI,SAASE,GAAMA,GAAIL,KAA1CE,EAAgD,GAClD,OAIJ,IAAKA,EAAQF,KAAKD,UAAUI,QAASH,KAAKH,GAAIG,KAAzCE,CAA+CD,GAClD,MAAM,KAIVN,WAAWM,GACT,OAAO,IAAIK,SAAQ,CAACC,EAASC,KAC3BC,EACET,KAAKD,UAAUI,QACfH,KAAKH,GACLG,KAHFS,CAIER,GACCS,MAAKC,IACAA,EACFJ,EAAQN,GAERO,EAAO,SAGVI,OAAMR,GAAMI,EAAOJ,SAK5B,SAASS,EAAOhB,EAAIi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C,IACL,MAAMqI,EAAS,GACTI,EAAS1E,OAAOC,KAAKoE,GAAQrD,KAAIuD,GACZF,EAAOE,GACRI,WAAW1I,GAAS,IAAIsI,IAAM3H,OAAMR,IAC1DA,EAAGsB,OAAS6G,EACZD,EAAOpG,KAAK9B,QAGhB,OAAOE,QAAQyE,IAAI2D,GAAQhI,MAAK,KAC9B,GAAI4H,EAAOtH,OAAS,EAClB,MAAMsH,EAGR,OAAO,OAhFKM,CAAWP,GAI7BQ,YAAa,IAAIC,IAAgB7I,GAC/B6I,EAAYlE,MAAKmE,GAAcA,EAAWrC,KAAKzG,KAEjD+I,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,MAAO,CAACJ,EAAYK,GAA6B,IAAUnJ,IACzD,GACEmJ,GACiB,iBAAVnJ,GACU,KAAjBA,EAAMoJ,OAEN,OAAO,EAGT,GAAIpJ,MAAAA,EAAuC,CACzC,GAAKkJ,EAGH,OAAOJ,EAAWJ,UAAU1I,GAF5B8I,EAAWN,MAAMxI,GAKrB,OAAO,GAIX,SAASiG,EAASb,GAChB,OAAOpF,GAEFqJ,MAAMC,QAAQtJ,IAAuB,UAAboF,GACd,OAAVpF,GAA+B,SAAboF,UACZpF,IAAUoF,EAKvB,SAASQ,EAAkB5F,GACzB,MACmB,iBAAVA,GAAsBwF,SAASxF,IAAUuJ,KAAKC,MAAMxJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.esm.js b/dist/v8n.esm.js index 1c20946..4ebcbad 100644 --- a/dist/v8n.esm.js +++ b/dist/v8n.esm.js @@ -64,7 +64,7 @@ function pickFn(fn, variant) { function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -74,7 +74,7 @@ function testAux(modifiers, fn, rule) { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/dist/v8n.min.js b/dist/v8n.min.js index e6ad50a..224bf79 100644 --- a/dist/v8n.min.js +++ b/dist/v8n.min.js @@ -1,2 +1,2 @@ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).v8n=t()}(this,(function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return v("string")},boolean:function(){return v("boolean")},undefined:function(){return v("undefined")},null:function(){return v("null")},array:function(){return v("array")},object:function(){return v("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:g(!1),optionalAsync:g(!0)};function g(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function v(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f})); +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).v8n=t()}(this,(function(){"use strict";var n=function(n,t,r,e){this.name=n,this.fn=t,this.args=r,this.modifiers=e};function t(n,t){return void 0===t&&(t="simple"),"object"==typeof n?n[t]:n}function r(n,e,u){if(n.length){var i=n.shift(),o=r(n,e,u);return i.perform(o,u)}return t(e)}function e(n,r,u){if(n.length){var i=n.shift(),o=e(n,r,u);return i.performAsync(o,u)}return function(n){return Promise.resolve(t(r,"async")(n))}}n.prototype._test=function(n){var t=this.fn;try{r(this.modifiers.slice(),t,this)(n)}catch(n){t=function(){return!1}}try{return r(this.modifiers.slice(),t,this)(n)}catch(n){return!1}},n.prototype._check=function(n){try{r(this.modifiers.slice(),this.fn,this)(n)}catch(n){if(r(this.modifiers.slice(),(function(n){return n}),this)(!1))return}if(!r(this.modifiers.slice(),this.fn,this)(n))throw null},n.prototype._testAsync=function(n){var t=this;return new Promise((function(r,u){e(t.modifiers.slice(),t.fn,t)(n).then((function(t){t?r(n):u(null)})).catch((function(n){return u(n)}))}))};var u=function(n,t,r){this.name=n,this.perform=t,this.performAsync=r},i=function(n){function t(r,e,u,i){for(var o=[],c=arguments.length-4;c-- >0;)o[c]=arguments[c+4];n.call(this,o),n.captureStackTrace&&n.captureStackTrace(this,t),this.rule=r,this.value=e,this.cause=u,this.target=i}return n&&(t.__proto__=n),t.prototype=Object.create(n&&n.prototype),t.prototype.constructor=t,t}(Error),o=function(n,t){void 0===n&&(n=[]),void 0===t&&(t=[]),this.chain=n,this.nextRuleModifiers=t};function c(n,t,r,e){if(t.length){var u=t.shift();u._testAsync(n).then((function(){c(n,t,r,e)}),(function(t){e(new i(u,n,t))}))}else r(n)}function f(){return void 0!==typeof Proxy?a(new o):h(new o)}o.prototype._applyRule=function(t,r){var e=this;return function(){for(var u=[],i=arguments.length;i--;)u[i]=arguments[i];return e.chain.push(new n(r,t.apply(e,u),u,e.nextRuleModifiers)),e.nextRuleModifiers=[],e}},o.prototype._applyModifier=function(n,t){return this.nextRuleModifiers.push(new u(t,n.simple,n.async)),this},o.prototype._clone=function(){return new o(this.chain.slice(),this.nextRuleModifiers.slice())},o.prototype.test=function(n){return this.chain.every((function(t){return t._test(n)}))},o.prototype.testAll=function(n){var t=[];return this.chain.forEach((function(r){try{r._check(n)}catch(e){t.push(new i(r,n,e))}})),t},o.prototype.check=function(n){this.chain.forEach((function(t){try{t._check(n)}catch(r){throw new i(t,n,r)}}))},o.prototype.testAsync=function(n){var t=this;return new Promise((function(r,e){c(n,t.chain.slice(),r,e)}))};var s={};function a(n){return new Proxy(n,{get:function(t,r){if(r in t)return t[r];var e=a(n._clone());return r in l?e._applyModifier(l[r],r):r in s?e._applyRule(s[r],r):r in m?e._applyRule(m[r],r):void 0}})}function h(n){var t=function(n,t){return Object.keys(n).forEach((function(r){t[r]=function(){for(var e=[],u=arguments.length;u--;)e[u]=arguments[u];var i=h(t._clone()),o=i._applyRule(n[r],r).apply(void 0,e);return o}})),t},r=t(m,n),e=t(s,r);return Object.keys(l).forEach((function(n){Object.defineProperty(e,n,{get:function(){return h(e._clone())._applyModifier(l[n],n)}})})),e}f.extend=function(n){Object.assign(s,n)},f.clearCustomRules=function(){s={}};var l={not:{simple:function(n){return function(t){return!n(t)}},async:function(n){return function(t){return Promise.resolve(n(t)).then((function(n){return!n})).catch((function(){return!0}))}}},some:{simple:function(n){return function(t){return y(t).some((function(t){try{return n(t)}catch(n){return!1}}))}},async:function(n){return function(t){return Promise.all(y(t).map((function(t){try{return n(t).catch((function(){return!1}))}catch(n){return!1}}))).then((function(n){return n.some(Boolean)}))}}},every:{simple:function(n){return function(t){return!1!==t&&y(t).every(n)}},async:function(n){return function(t){return Promise.all(y(t).map(n)).then((function(n){return n.every(Boolean)}))}}},strict:{simple:function(n,t){return function(r){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n(r):n(r)}},async:function(n,t){return function(r){return Promise.resolve(n(r)).then((function(n){return p(t)&&r&&"object"==typeof r?Object.keys(t.args[0]).length===Object.keys(r).length&&n:n})).catch((function(){return!1}))}}}};function p(n){return n&&"schema"===n.name&&n.args.length>0&&"object"==typeof n.args[0]}function y(n){return"string"==typeof n?n.split(""):n}var m={equal:function(n){return function(t){return t==n}},exact:function(n){return function(t){return t===n}},number:function(n){return void 0===n&&(n=!0),function(t){return"number"==typeof t&&(n||isFinite(t))}},integer:function(){return function(n){return(Number.isInteger||d)(n)}},numeric:function(){return function(n){return!isNaN(parseFloat(n))&&isFinite(n)}},string:function(){return v("string")},boolean:function(){return v("boolean")},undefined:function(){return v("undefined")},null:function(){return v("null")},array:function(){return v("array")},object:function(){return v("object")},instanceOf:function(n){return function(t){return t instanceof n}},pattern:function(n){return function(t){return n.test(t)}},lowercase:function(){return function(n){return/^([a-z]+\s*)+$/.test(n)}},uppercase:function(){return function(n){return/^([A-Z]+\s*)+$/.test(n)}},vowel:function(){return function(n){return/^[aeiou]+$/i.test(n)}},consonant:function(){return function(n){return/^(?=[^aeiou])([a-z]+)$/i.test(n)}},first:function(n){return function(t){return t[0]==n}},last:function(n){return function(t){return t[t.length-1]==n}},empty:function(){return function(n){return 0===n.length}},length:function(n,t){return function(r){return r.length>=n&&r.length<=(t||n)}},minLength:function(n){return function(t){return t.length>=n}},maxLength:function(n){return function(t){return t.length<=n}},negative:function(){return function(n){return n<0}},positive:function(){return function(n){return n>=0}},between:function(n,t){return function(r){return r>=n&&r<=t}},range:function(n,t){return function(r){return r>=n&&r<=t}},lessThan:function(n){return function(t){return tn}},greaterThanOrEqual:function(n){return function(t){return t>=n}},even:function(){return function(n){return n%2==0}},odd:function(){return function(n){return n%2!=0}},includes:function(n){return function(t){return~t.indexOf(n)}},schema:function(n){return function(n){return{simple:function(t){var r=[];if(Object.keys(n).forEach((function(e){var u=n[e];try{u.check((t||{})[e])}catch(n){n.target=e,r.push(n)}})),r.length>0)throw r;return!0},async:function(t){var r=[],e=Object.keys(n).map((function(e){return n[e].testAsync((t||{})[e]).catch((function(n){n.target=e,r.push(n)}))}));return Promise.all(e).then((function(){if(r.length>0)throw r;return!0}))}}}(n)},passesAnyOf:function(){for(var n=[],t=arguments.length;t--;)n[t]=arguments[t];return function(t){return n.some((function(n){return n.test(t)}))}},optional:g(!1),optionalAsync:g(!0)};function g(n){return function(t,r){return void 0===r&&(r=!1),function(e){if(r&&"string"==typeof e&&""===e.trim())return!0;if(null!=e){if(n)return t.testAsync(e);t.check(e)}return!0}}}function v(n){return function(t){return Array.isArray(t)&&"array"===n||null===t&&"null"===n||typeof t===n}}function d(n){return"number"==typeof n&&isFinite(n)&&Math.floor(n)===n}return f})); //# sourceMappingURL=v8n.min.js.map diff --git a/dist/v8n.min.js.map b/dist/v8n.min.js.map index 427c8c4..10989d9 100644 --- a/dist/v8n.min.js.map +++ b/dist/v8n.min.js.map @@ -1 +1 @@ -{"version":3,"file":"v8n.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"oOAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,GAClC,OAAOU,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,GACvC,OAAOU,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAIpB,EAAKG,KAAKH,GAEd,IACE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,EAAiBpF,OAAO7C,GAAS,IAAIgI,IACrC,MAAO1H,GACPA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,OAGZyH,EAAOvI,OAAS,EAClB,MAAMuI,EAER,OAAO,GAETzF,eAAOtC,GACLP,IAAMsI,EAAS,GACTG,EAASxE,OAAOC,KAAKmE,GAAQpD,cAAIsD,GAErC,OADyBF,EAAOE,GACRlF,WAAW9C,GAAS,IAAIgI,IAAMnH,gBAAMP,GAC1DA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,SAGhB,OAAOL,QAAQwE,IAAIyD,GAAQvH,iBACzB,GAAIoH,EAAOvI,OAAS,EAClB,MAAMuI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file +{"version":3,"file":"v8n.min.js","sources":["../src/Rule.js","../src/Modifier.js","../src/ValidationError.js","../src/Context.js","../src/v8n.js"],"sourcesContent":["class Rule {\n constructor(name, fn, args, modifiers) {\n this.name = name;\n this.fn = fn;\n this.args = args;\n this.modifiers = modifiers;\n }\n\n _test(value) {\n let fn = this.fn;\n\n try {\n testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n fn = () => false;\n }\n\n try {\n return testAux(this.modifiers.slice(), fn, this)(value);\n } catch (ex) {\n return false;\n }\n }\n\n _check(value) {\n try {\n testAux(this.modifiers.slice(), this.fn, this)(value);\n } catch (ex) {\n if (testAux(this.modifiers.slice(), it => it, this)(false)) {\n return;\n }\n }\n\n if (!testAux(this.modifiers.slice(), this.fn, this)(value)) {\n throw null;\n }\n }\n\n _testAsync(value) {\n return new Promise((resolve, reject) => {\n testAsyncAux(\n this.modifiers.slice(),\n this.fn,\n this,\n )(value)\n .then(valid => {\n if (valid) {\n resolve(value);\n } else {\n reject(null);\n }\n })\n .catch(ex => reject(ex));\n });\n }\n}\n\nfunction pickFn(fn, variant = 'simple') {\n return typeof fn === 'object' ? fn[variant] : fn;\n}\n\nfunction testAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAux(modifiers, fn, rule);\n return modifier.perform(nextFn, rule);\n } else {\n return pickFn(fn);\n }\n}\n\nfunction testAsyncAux(modifiers, fn, rule) {\n if (modifiers.length) {\n const modifier = modifiers.shift();\n const nextFn = testAsyncAux(modifiers, fn, rule);\n return modifier.performAsync(nextFn, rule);\n } else {\n return value => Promise.resolve(pickFn(fn, 'async')(value));\n }\n}\n\nexport default Rule;\n","class Modifier {\n constructor(name, perform, performAsync) {\n this.name = name;\n this.perform = perform;\n this.performAsync = performAsync;\n }\n}\n\nexport default Modifier;\n","class ValidationError extends Error {\n constructor(rule, value, cause, target, ...remaining) {\n super(remaining);\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, ValidationError);\n }\n this.rule = rule;\n this.value = value;\n this.cause = cause;\n this.target = target;\n }\n}\n\nexport default ValidationError;\n","import Rule from './Rule';\nimport Modifier from './Modifier';\nimport ValidationError from './ValidationError';\n\nclass Context {\n constructor(chain = [], nextRuleModifiers = []) {\n this.chain = chain;\n this.nextRuleModifiers = nextRuleModifiers;\n }\n\n _applyRule(ruleFn, name) {\n return (...args) => {\n this.chain.push(\n new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),\n );\n this.nextRuleModifiers = [];\n return this;\n };\n }\n\n _applyModifier(modifier, name) {\n this.nextRuleModifiers.push(\n new Modifier(name, modifier.simple, modifier.async),\n );\n return this;\n }\n\n _clone() {\n return new Context(this.chain.slice(), this.nextRuleModifiers.slice());\n }\n\n test(value) {\n return this.chain.every(rule => rule._test(value));\n }\n\n testAll(value) {\n const err = [];\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n err.push(new ValidationError(rule, value, ex));\n }\n });\n return err;\n }\n\n check(value) {\n this.chain.forEach(rule => {\n try {\n rule._check(value);\n } catch (ex) {\n throw new ValidationError(rule, value, ex);\n }\n });\n }\n\n testAsync(value) {\n return new Promise((resolve, reject) => {\n executeAsyncRules(value, this.chain.slice(), resolve, reject);\n });\n }\n}\n\nfunction executeAsyncRules(value, rules, resolve, reject) {\n if (rules.length) {\n const rule = rules.shift();\n rule._testAsync(value).then(\n () => {\n executeAsyncRules(value, rules, resolve, reject);\n },\n cause => {\n reject(new ValidationError(rule, value, cause));\n },\n );\n } else {\n resolve(value);\n }\n}\n\nexport default Context;\n","import Context from './Context';\n\nfunction v8n() {\n return typeof Proxy !== undefined\n ? proxyContext(new Context())\n : proxylessContext(new Context());\n}\n\n// Custom rules\nlet customRules = {};\n\nv8n.extend = function(newRules) {\n Object.assign(customRules, newRules);\n};\n\nv8n.clearCustomRules = function() {\n customRules = {};\n};\n\nfunction proxyContext(context) {\n return new Proxy(context, {\n get(obj, prop) {\n if (prop in obj) {\n return obj[prop];\n }\n\n const newContext = proxyContext(context._clone());\n\n if (prop in availableModifiers) {\n return newContext._applyModifier(availableModifiers[prop], prop);\n }\n if (prop in customRules) {\n return newContext._applyRule(customRules[prop], prop);\n }\n if (prop in availableRules) {\n return newContext._applyRule(availableRules[prop], prop);\n }\n },\n });\n}\n\nfunction proxylessContext(context) {\n const addRuleSet = (ruleSet, targetContext) => {\n Object.keys(ruleSet).forEach(prop => {\n targetContext[prop] = (...args) => {\n const newContext = proxylessContext(targetContext._clone());\n const contextWithRuleApplied = newContext._applyRule(\n ruleSet[prop],\n prop,\n )(...args);\n return contextWithRuleApplied;\n };\n });\n return targetContext;\n };\n\n const contextWithAvailableRules = addRuleSet(availableRules, context);\n const contextWithAllRules = addRuleSet(\n customRules,\n contextWithAvailableRules,\n );\n\n Object.keys(availableModifiers).forEach(prop => {\n Object.defineProperty(contextWithAllRules, prop, {\n get: () => {\n const newContext = proxylessContext(contextWithAllRules._clone());\n return newContext._applyModifier(availableModifiers[prop], prop);\n },\n });\n });\n\n return contextWithAllRules;\n}\n\nconst availableModifiers = {\n not: {\n simple: fn => value => !fn(value),\n async: fn => value =>\n Promise.resolve(fn(value))\n .then(result => !result)\n .catch(() => true),\n },\n\n some: {\n simple: fn => value => {\n return split(value).some(item => {\n try {\n return fn(item);\n } catch (ex) {\n return false;\n }\n });\n },\n async: fn => value => {\n return Promise.all(\n split(value).map(item => {\n try {\n return fn(item).catch(() => false);\n } catch (ex) {\n return false;\n }\n }),\n ).then(result => result.some(Boolean));\n },\n },\n\n every: {\n simple: fn => value => value !== false && split(value).every(fn),\n async: fn => value =>\n Promise.all(split(value).map(fn)).then(result => result.every(Boolean)),\n },\n\n strict: {\n simple: (fn, rule) => value => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n fn(value)\n );\n }\n return fn(value);\n },\n async: (fn, rule) => value =>\n Promise.resolve(fn(value))\n .then(result => {\n if (isSchemaRule(rule) && value && typeof value === 'object') {\n return (\n Object.keys(rule.args[0]).length === Object.keys(value).length &&\n result\n );\n }\n return result;\n })\n .catch(() => false),\n },\n};\n\nfunction isSchemaRule(rule) {\n return (\n rule &&\n rule.name === 'schema' &&\n rule.args.length > 0 &&\n typeof rule.args[0] === 'object'\n );\n}\n\nfunction split(value) {\n if (typeof value === 'string') {\n return value.split('');\n }\n return value;\n}\n\nconst availableRules = {\n // Value\n\n equal: expected => value => value == expected,\n\n exact: expected => value => value === expected,\n\n // Types\n\n number: (allowInfinite = true) => value =>\n typeof value === 'number' && (allowInfinite || isFinite(value)),\n\n integer: () => value => {\n const isInteger = Number.isInteger || isIntegerPolyfill;\n return isInteger(value);\n },\n\n numeric: () => value => !isNaN(parseFloat(value)) && isFinite(value),\n\n string: () => testType('string'),\n\n boolean: () => testType('boolean'),\n\n undefined: () => testType('undefined'),\n\n null: () => testType('null'),\n\n array: () => testType('array'),\n\n object: () => testType('object'),\n\n instanceOf: instance => value => value instanceof instance,\n\n // Pattern\n\n pattern: expected => value => expected.test(value),\n\n lowercase: () => value => /^([a-z]+\\s*)+$/.test(value),\n\n uppercase: () => value => /^([A-Z]+\\s*)+$/.test(value),\n\n vowel: () => value => /^[aeiou]+$/i.test(value),\n\n consonant: () => value => /^(?=[^aeiou])([a-z]+)$/i.test(value),\n\n // Value at\n\n first: expected => value => value[0] == expected,\n\n last: expected => value => value[value.length - 1] == expected,\n\n // Length\n\n empty: () => value => value.length === 0,\n\n length: (min, max) => value =>\n value.length >= min && value.length <= (max || min),\n\n minLength: min => value => value.length >= min,\n\n maxLength: max => value => value.length <= max,\n\n // Range\n\n negative: () => value => value < 0,\n\n positive: () => value => value >= 0,\n\n between: (a, b) => value => value >= a && value <= b,\n\n range: (a, b) => value => value >= a && value <= b,\n\n lessThan: n => value => value < n,\n\n lessThanOrEqual: n => value => value <= n,\n\n greaterThan: n => value => value > n,\n\n greaterThanOrEqual: n => value => value >= n,\n\n // Divisible\n\n even: () => value => value % 2 === 0,\n\n odd: () => value => value % 2 !== 0,\n\n includes: expected => value => ~value.indexOf(expected),\n\n schema: schema => testSchema(schema),\n\n // branching\n\n passesAnyOf: (...validations) => value =>\n validations.some(validation => validation.test(value)),\n\n optional: createOptionalRule(false),\n optionalAsync: createOptionalRule(true),\n};\n\nfunction createOptionalRule(asynchronous) {\n return (validation, considerTrimmedEmptyString = false) => value => {\n if (\n considerTrimmedEmptyString &&\n typeof value === 'string' &&\n value.trim() === ''\n ) {\n return true;\n }\n\n if (value !== undefined && value !== null) {\n if (!asynchronous) {\n validation.check(value);\n } else {\n return validation.testAsync(value);\n }\n }\n return true;\n };\n}\n\nfunction testType(expected) {\n return value => {\n return (\n (Array.isArray(value) && expected === 'array') ||\n (value === null && expected === 'null') ||\n typeof value === expected\n );\n };\n}\n\nfunction isIntegerPolyfill(value) {\n return (\n typeof value === 'number' && isFinite(value) && Math.floor(value) === value\n );\n}\n\nfunction testSchema(schema) {\n return {\n simple: value => {\n const causes = [];\n Object.keys(schema).forEach(key => {\n const nestedValidation = schema[key];\n try {\n nestedValidation.check((value || {})[key]);\n } catch (ex) {\n ex.target = key;\n causes.push(ex);\n }\n });\n if (causes.length > 0) {\n throw causes;\n }\n return true;\n },\n async: value => {\n const causes = [];\n const nested = Object.keys(schema).map(key => {\n const nestedValidation = schema[key];\n return nestedValidation.testAsync((value || {})[key]).catch(ex => {\n ex.target = key;\n causes.push(ex);\n });\n });\n return Promise.all(nested).then(() => {\n if (causes.length > 0) {\n throw causes;\n }\n\n return true;\n });\n },\n };\n}\n\nexport default v8n;\n"],"names":["Rule","name","fn","args","modifiers","this","pickFn","variant","testAux","rule","length","const","modifier","shift","nextFn","perform","testAsyncAux","performAsync","value","Promise","resolve","_test","let","slice","ex","_check","it","_testAsync","reject","then","valid","catch","Modifier","ValidationError","cause","target","super","remaining","Error","captureStackTrace","Context","chain","nextRuleModifiers","executeAsyncRules","rules","v8n","undefined","Proxy","proxyContext","proxylessContext","_applyRule","ruleFn","push","apply","_applyModifier","simple","async","_clone","test","every","testAll","err","forEach","check","testAsync","customRules","context","get","obj","prop","newContext","availableModifiers","availableRules","addRuleSet","ruleSet","targetContext","Object","keys","contextWithRuleApplied","contextWithAvailableRules","contextWithAllRules","defineProperty","extend","newRules","assign","clearCustomRules","not","result","some","split","item","all","map","Boolean","strict","isSchemaRule","equal","expected","exact","number","allowInfinite","isFinite","integer","Number","isInteger","isIntegerPolyfill","numeric","isNaN","parseFloat","string","testType","boolean","null","array","object","instanceOf","instance","pattern","lowercase","uppercase","vowel","consonant","first","last","empty","min","max","minLength","maxLength","negative","positive","between","a","b","range","lessThan","n","lessThanOrEqual","greaterThan","greaterThanOrEqual","even","odd","includes","indexOf","schema","causes","key","nestedValidation","nested","testSchema","passesAnyOf","validations","validation","optional","createOptionalRule","optionalAsync","asynchronous","considerTrimmedEmptyString","trim","Array","isArray","Math","floor"],"mappings":"oOAAA,IAAMA,EACJ,SAAYC,EAAMC,EAAIC,EAAMC,GAC1BC,KAAKJ,KAAOA,EACZI,KAAKH,GAAKA,EACVG,KAAKF,KAAOA,EACZE,KAAKD,UAAYA,GAoDrB,SAASE,EAAOJ,EAAIK,GAClB,sBAD4B,UACP,iBAAPL,EAAkBA,EAAGK,GAAWL,EAGhD,SAASM,EAAQJ,EAAWF,EAAIO,GAC9B,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASN,EAAQJ,EAAWF,EAAIO,GACtC,OAAOG,EAASG,QAAQD,EAAQL,GAEhC,OAAOH,EAAOJ,GAIlB,SAASc,EAAaZ,EAAWF,EAAIO,GACnC,GAAIL,EAAUM,OAAQ,CACpBC,IAAMC,EAAWR,EAAUS,QACrBC,EAASE,EAAaZ,EAAWF,EAAIO,GAC3C,OAAOG,EAASK,aAAaH,EAAQL,GAErC,gBAAOS,UAASC,QAAQC,QAAQd,EAAOJ,EAAI,QAAXI,CAAoBY,iBArEtDG,eAAMH,GACJI,IAAIpB,EAAKG,KAAKH,GAEd,IACEM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GAC1C,MAAOM,GACPtB,oBAAW,GAGb,IACE,OAAOM,EAAQH,KAAKD,UAAUmB,QAASrB,EAAIG,KAApCG,CAA0CU,GACjD,MAAOM,GACP,OAAO,gBAIXC,gBAAOP,GAC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AAKmE,GAAQpD,cAAIsD,GAErC,OADyBF,EAAOE,GACRlF,WAAW9C,GAAS,IAAIgI,IAAMnH,gBAAMP,GAC1DA,EAAGW,OAAS+G,EACZD,EAAO7F,KAAK5B,SAGhB,OAAOL,QAAQwE,IAAIyD,GAAQvH,iBACzB,GAAIoH,EAAOvI,OAAS,EAClB,MAAMuI,EAGR,OAAO,OAhFKI,CAAWL,IAI7BM,8FAAiCpI,UAC/BqI,EAAY/D,eAAKgE,UAAcA,EAAW9F,KAAKxC,QAEjDuI,SAAUC,GAAmB,GAC7BC,cAAeD,GAAmB,IAGpC,SAASA,EAAmBE,GAC1B,gBAAQJ,EAAYK,0BAA6B,YAAU3I,GACzD,GACE2I,GACiB,iBAAV3I,GACU,KAAjBA,EAAM4I,OAEN,OAAO,EAGT,GAAI5I,MAAAA,EAAuC,CACzC,GAAK0I,EAGH,OAAOJ,EAAWxF,UAAU9C,GAF5BsI,EAAWzF,MAAM7C,GAKrB,OAAO,IAIX,SAAS4F,EAASb,GAChB,gBAAO/E,GACL,OACG6I,MAAMC,QAAQ9I,IAAuB,UAAb+E,GACd,OAAV/E,GAA+B,SAAb+E,UACZ/E,IAAU+E,GAKvB,SAASQ,EAAkBvF,GACzB,MACmB,iBAAVA,GAAsBmF,SAASnF,IAAU+I,KAAKC,MAAMhJ,KAAWA"} \ No newline at end of file diff --git a/dist/v8n.system.js b/dist/v8n.system.js index af6cb91..620a32e 100644 --- a/dist/v8n.system.js +++ b/dist/v8n.system.js @@ -71,7 +71,7 @@ System.register('v8n', [], function (exports) { function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -81,7 +81,7 @@ System.register('v8n', [], function (exports) { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/dist/v8n.umd.js b/dist/v8n.umd.js index f2ef1b9..1377752 100644 --- a/dist/v8n.umd.js +++ b/dist/v8n.umd.js @@ -70,7 +70,7 @@ function testAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAux(modifiers, fn); + var nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -80,7 +80,7 @@ function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { var modifier = modifiers.shift(); - var nextFn = testAsyncAux(modifiers, fn); + var nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); }; diff --git a/src/Rule.js b/src/Rule.js index c4d8c2b..47ffd3d 100644 --- a/src/Rule.js +++ b/src/Rule.js @@ -62,7 +62,7 @@ function pickFn(fn, variant = 'simple') { function testAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); - const nextFn = testAux(modifiers, fn); + const nextFn = testAux(modifiers, fn, rule); return modifier.perform(nextFn, rule); } else { return pickFn(fn); @@ -72,7 +72,7 @@ function testAux(modifiers, fn, rule) { function testAsyncAux(modifiers, fn, rule) { if (modifiers.length) { const modifier = modifiers.shift(); - const nextFn = testAsyncAux(modifiers, fn); + const nextFn = testAsyncAux(modifiers, fn, rule); return modifier.performAsync(nextFn, rule); } else { return value => Promise.resolve(pickFn(fn, 'async')(value)); diff --git a/src/v8n.test.js b/src/v8n.test.js index 81b7d52..8ddec8d 100644 --- a/src/v8n.test.js +++ b/src/v8n.test.js @@ -431,71 +431,6 @@ describe('modifiers', () => { ).rejects.toBeDefined(); }); - describe("the 'strict' modifier", () => { - it('makes the schema rule strict', () => { - const validation = v8n().strict.schema({ - id: v8n() - .number() - .positive(), - name: v8n() - .string() - .minLength(4), - }); - - expect( - validation.test({ - id: 1, - name: 'Luke', - }), - ).toBeTruthy(); - - expect( - validation.test({ - id: 1, - }), - ).toBeFalsy(); - - expect( - validation.test({ - id: 1, - name: 'Luke', - lastname: 'Skywalker', - }), - ).toBeFalsy(); - }); - - it('makes the schema rule strict asynchronously', async () => { - v8n.extend({ asyncRule }); - - const validation = v8n().strict.schema({ - id: v8n() - .number() - .asyncRule([10, 20]), - }); - - const valid = { - id: 10, - }; - - const invalidValue = { - id: 1, - }; - - const invalidExtraProp = { - id: 1, - name: 'Luke', - }; - - await expect(validation.testAsync(valid)).resolves.toBe(valid); - await expect(validation.testAsync(invalidValue)).rejects.toBeInstanceOf( - ValidationError, - ); - await expect( - validation.testAsync(invalidExtraProp), - ).rejects.toBeInstanceOf(ValidationError); - }); - }); - it('expect that error if formatted correctly', async () => { const validation = v8n().schema({ item: v8n().every.schema({ @@ -516,6 +451,160 @@ describe('modifiers', () => { }); }); + describe("the 'strict' modifier", () => { + it('makes the schema rule strict', () => { + const validation = v8n().strict.schema({ + id: v8n() + .number() + .positive(), + name: v8n() + .string() + .minLength(4), + }); + + expect( + validation.test({ + id: 1, + name: 'Luke', + }), + ).toBeTruthy(); + + expect( + validation.test({ + id: 1, + }), + ).toBeFalsy(); + + expect( + validation.test({ + id: 1, + name: 'Luke', + lastname: 'Skywalker', + }), + ).toBeFalsy(); + }); + + it('makes the schema rule strict asynchronously', async () => { + v8n.extend({ asyncRule }); + + const validation = v8n().strict.schema({ + id: v8n() + .number() + .asyncRule([10, 20]), + }); + + const valid = { + id: 10, + }; + + const invalidValue = { + id: 1, + }; + + const invalidExtraProp = { + id: 1, + name: 'Luke', + }; + + await expect(validation.testAsync(valid)).resolves.toBe(valid); + await expect(validation.testAsync(invalidValue)).rejects.toBeInstanceOf( + ValidationError, + ); + await expect( + validation.testAsync(invalidExtraProp), + ).rejects.toBeInstanceOf(ValidationError); + }); + + it('does not affect non-schema validations', () => { + expect( + v8n() + .strict.number() + .test(1), + ).toEqual( + v8n() + .number() + .test(1), + ); + + expect( + v8n() + .strict.lowercase() + .test('abc'), + ).toEqual( + v8n() + .lowercase() + .test('abc'), + ); + }); + + it('can be chained with other modifiers', () => { + const schema = { + id: v8n() + .number() + .positive(), + }; + + expect( + v8n() + .not.strict.schema(schema) + .test({ + id: 1, + }), + ).toBeFalsy(); + + expect( + v8n() + .not.strict.schema(schema) + .test({ + id: 1, + name: 'abc', + }), + ).toBeTruthy(); + + expect( + v8n() + .some.strict.schema(schema) + .test([ + { + id: 1, + name: 'abc', + }, + { + id: 1, + }, + ]), + ).toBeTruthy(); + + expect( + v8n() + .some.strict.schema(schema) + .test([ + { + id: 1, + name: 'abc', + }, + { + id: -1, + }, + ]), + ).toBeFalsy(); + + expect( + v8n() + .not.some.strict.schema(schema) + .test([ + { + id: 1, + name: 'abc', + }, + { + id: 1, + }, + ]), + ).toBeFalsy(); + }); + }); + test('should be able to mix modifiers', () => { const validation = v8n().not.some.positive(); expect(validation.test([-1, -2, 1])).toBeFalsy();