Skip to content

Commit

Permalink
feat: add modifier for strict schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Barfurth authored Mar 13, 2022
1 parent c7e4b2b commit fa865ee
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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), [#179](https://github.com/imbrn/v8n/issues/179))

## [1.4.0] - 2022-02-17

Expand Down
57 changes: 45 additions & 12 deletions dist/v8n.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ 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;
}
};

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;
}
};
Expand All @@ -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) {
Expand All @@ -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);
var nextFn = testAux(modifiers, fn, rule);
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);
var nextFn = testAsyncAux(modifiers, fn, rule);
return modifier.performAsync(nextFn, rule);
} else {
return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); };
}
Expand Down Expand Up @@ -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('');
Expand Down
57 changes: 45 additions & 12 deletions dist/v8n.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ 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;
}
};

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;
}
};
Expand All @@ -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) {
Expand All @@ -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);
var nextFn = testAux(modifiers, fn, rule);
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);
var nextFn = testAsyncAux(modifiers, fn, rule);
return modifier.performAsync(nextFn, rule);
} else {
return function (value) { return Promise.resolve(pickFn(fn, 'async')(value)); };
}
Expand Down Expand Up @@ -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('');
Expand Down
Loading

0 comments on commit fa865ee

Please sign in to comment.