Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifier for strict schema validation #224

Merged
merged 4 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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