Comparing version 2.3.1 to 2.4.1
100
lib/any.js
@@ -31,3 +31,4 @@ // Load modules | ||
this._settings = null; | ||
this._validators = []; | ||
this._tests = []; | ||
this._dependencies = []; | ||
this._mutators = []; | ||
@@ -47,14 +48,17 @@ this._valids = new internals.Set([undefined]); | ||
internals.Any.prototype._base = function (func) { | ||
this._tests.push({ func: func }); | ||
}; | ||
internals.Any.prototype._test = function (name, arg, func) { | ||
if (arguments.length === 1) { | ||
func = arguments[0]; | ||
name = undefined; | ||
} | ||
else if (arguments.length === 2) { | ||
func = arguments[1]; | ||
arg = undefined; | ||
} | ||
this._tests.push({ func: func, name: name, arg: arg }); | ||
}; | ||
this._validators.push({ func: func, name: name, arg: arg }); | ||
internals.Any.prototype._dependency = function (name, arg, func) { | ||
this._dependencies.push({ func: func, name: name, arg: arg }); | ||
}; | ||
@@ -167,3 +171,3 @@ | ||
this._test('with', peers, function (value, state, options) { | ||
this._dependency('with', peers, function (value, state, options) { | ||
@@ -174,8 +178,8 @@ if (!state.parent) { | ||
if (value === undefined) { | ||
return null; | ||
} | ||
for (var i = 0, il = peers.length; i < il; ++i) { | ||
if (!state.parent.hasOwnProperty(peers[i]) || | ||
state.parent[peers[i]] === undefined || | ||
state.parent[peers[i]] === null || | ||
state.parent[peers[i]] === '') { | ||
if (!state.parent.hasOwnProperty(peers[i])) { | ||
return internals.Any.error('any.with.peer', { value: peers[i] }, state); | ||
@@ -199,3 +203,3 @@ } | ||
this._test('without', peers, function (value, state, options) { | ||
this._dependency('without', peers, function (value, state, options) { | ||
@@ -206,8 +210,8 @@ if (!state.parent) { | ||
if (value === undefined) { | ||
return null; | ||
} | ||
for (var i = 0, il = peers.length; i < il; ++i) { | ||
if (state.parent.hasOwnProperty(peers[i]) && | ||
state.parent[peers[i]] !== undefined && | ||
state.parent[peers[i]] !== null && | ||
state.parent[peers[i]] !== '') { | ||
if (state.parent.hasOwnProperty(peers[i])) { | ||
return internals.Any.error('any.without.peer', { value: peers[i] }, state); | ||
@@ -226,11 +230,5 @@ } | ||
this._valids.remove(undefined); | ||
this._invalids.remove(undefined); | ||
this._invalids.remove(null); | ||
this._invalids.remove(''); | ||
var peers = Utils.flatten(Array.prototype.slice.call(arguments)); | ||
this._test('xor', peers, function (value, state, options) { | ||
this._dependency('xor', peers, function (value, state, options) { | ||
@@ -244,7 +242,3 @@ if (!state.parent) { | ||
for (var i = 0, il = peers.length; i < il && present < 2; ++i) { | ||
if (state.parent.hasOwnProperty(peers[i]) && | ||
state.parent[peers[i]] !== undefined && | ||
state.parent[peers[i]] !== null && | ||
state.parent[peers[i]] !== '') { | ||
if (state.parent.hasOwnProperty(peers[i])) { | ||
++present; | ||
@@ -271,8 +265,2 @@ } | ||
this._valids.remove(undefined); | ||
this._invalids.remove(undefined); | ||
this._invalids.remove(null); | ||
this._invalids.remove(''); | ||
var peers = Utils.flatten(Array.prototype.slice.call(arguments)); | ||
@@ -283,3 +271,3 @@ for (var i = 0, li = peers.length; i < li; i++) { | ||
this._test('or', peers, function (value, state, options) { | ||
this._dependency('or', peers, function (value, state, options) { | ||
@@ -299,7 +287,3 @@ if (!state.parent) { | ||
var peer = peers[i]; | ||
if (state.parent.hasOwnProperty(peer) && | ||
state.parent[peer] !== undefined && | ||
state.parent[peer] !== null && | ||
state.parent[peer] !== '') { | ||
if (state.parent.hasOwnProperty(peer)) { | ||
return null; | ||
@@ -399,2 +383,14 @@ } | ||
// Validate dependencies | ||
for (var i = 0, il = self._dependencies.length; i < il; ++i) { | ||
var err = self._dependencies[i].func(value, state, options); | ||
if (err) { | ||
errors.push(err); | ||
if (options.abortEarly) { | ||
return errors; | ||
} | ||
} | ||
} | ||
// Check allowed and denied values using the original value | ||
@@ -446,8 +442,7 @@ | ||
// Apply validators | ||
// Validate tests | ||
for (var i = 0, il = self._validators.length; i < il; ++i) { | ||
var err = self._validators[i].func(value, state, options); | ||
for (i = 0, il = self._tests.length; i < il; ++i) { | ||
var err = self._tests[i].func(value, state, options); | ||
if (err) { | ||
Utils.assert(typeof err === 'object', 'oops'); | ||
if (Array.isArray(err)) { | ||
@@ -538,4 +533,5 @@ errors = errors.concat(err); | ||
for (var i = 0, il = this._validators.length; i < il; ++i) { | ||
var validator = this._validators[i]; | ||
var validators = [].concat(this._dependencies, this._tests); | ||
for (var i = 0, il = validators.length; i < il; ++i) { | ||
var validator = validators[i]; | ||
if (validator.name) { | ||
@@ -542,0 +538,0 @@ var item = { name: validator.name }; |
@@ -23,3 +23,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -26,0 +26,0 @@ if (Array.isArray(value)) { |
@@ -17,3 +17,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -20,0 +20,0 @@ if (value === null || typeof value === 'boolean') { |
@@ -17,3 +17,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -20,0 +20,0 @@ if (value instanceof Date) { |
@@ -17,3 +17,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -20,0 +20,0 @@ if (typeof value === 'function') { |
@@ -17,3 +17,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -81,3 +81,3 @@ if ((typeof value === 'number' || typeof value === 'string') && !isNaN(+value)) { | ||
this._test('integer', function (value, state, options) { | ||
this._test('integer', undefined, function (value, state, options) { | ||
@@ -97,3 +97,3 @@ if (!isNaN(value) && ((value | 0) === parseFloat(value))) { | ||
this._test('negative', function (value, state, options) { | ||
this._test('negative', undefined, function (value, state, options) { | ||
@@ -113,3 +113,3 @@ if (value < 0) { | ||
this._test('positive', function (value, state, options) { | ||
this._test('positive', undefined, function (value, state, options) { | ||
@@ -116,0 +116,0 @@ if (value > 0) { |
@@ -20,3 +20,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -23,0 +23,0 @@ if (typeof value !== 'object' || |
@@ -18,3 +18,3 @@ // Load modules | ||
this._test(function (value, state, options) { | ||
this._base(function (value, state, options) { | ||
@@ -129,3 +129,3 @@ if (typeof value === 'string' || | ||
this._test('alphanum', function (value, state, options) { | ||
this._test('alphanum', undefined, function (value, state, options) { | ||
@@ -148,3 +148,3 @@ if (value !== undefined && | ||
this._test('token', function (value, state, options) { | ||
this._test('token', undefined, function (value, state, options) { | ||
@@ -169,3 +169,3 @@ if (value !== undefined && | ||
this._test('email', function (value, state, options) { | ||
this._test('email', undefined, function (value, state, options) { | ||
@@ -187,3 +187,3 @@ if (value.match(regex)) { | ||
this._test(function (value, state, options) { | ||
this._test('isoDate', undefined, function (value, state, options) { | ||
@@ -206,3 +206,3 @@ if (value.match(regex)) { | ||
this._test(function (value, state, options) { | ||
this._test('guid', undefined, function (value, state, options) { | ||
@@ -209,0 +209,0 @@ if (value.match(regex) || value.match(regex2)) { |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "2.3.1", | ||
"version": "2.4.1", | ||
"repository": "git://github.com/spumko/joi", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -6,3 +6,3 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
Current version: **2.0.x** | ||
Current version: **2.4.x** | ||
@@ -9,0 +9,0 @@ [![Build Status](https://secure.travis-ci.org/spumko/joi.png)](http://travis-ci.org/spumko/joi) |
@@ -25,2 +25,5 @@ // Load modules | ||
var result = Joi.validate(config[i][0], schema); | ||
if (result !== null && config[i][1]) { | ||
console.log(result); | ||
} | ||
expect(result === null).to.equal(config[i][1]); | ||
@@ -27,0 +30,0 @@ } |
@@ -77,3 +77,3 @@ // Load modules | ||
var err = Joi.validate({ upc: null, txt: null }, schema, { abortEarly: false }); | ||
var err = Joi.validate({}, schema, { abortEarly: false }); | ||
expect(err.message).to.equal('at least one of txt upc is required. at least one of upc txt is required'); | ||
@@ -86,10 +86,10 @@ | ||
[{ txt: 'test' }, true], | ||
[{ txt: 'test', upc: null }, true], | ||
[{ txt: 'test', upc: '' }, true], | ||
[{ txt: '', upc: 'test' }, true], | ||
[{ txt: null, upc: 'test' }, true], | ||
[{ txt: 'test', upc: null }, false], | ||
[{ txt: 'test', upc: '' }, false], | ||
[{ txt: '', upc: 'test' }, false], | ||
[{ txt: null, upc: 'test' }, false], | ||
[{ txt: undefined, upc: 'test' }, true], | ||
[{ txt: 'test', upc: undefined }, true], | ||
[{ txt: 'test', upc: '' }, true], | ||
[{ txt: 'test', upc: null }, true], | ||
[{ txt: 'test', upc: '' }, false], | ||
[{ txt: 'test', upc: null }, false], | ||
[{ txt: '', upc: undefined }, false], | ||
@@ -114,3 +114,3 @@ [{ txt: '', upc: '' }, false], | ||
[{ txt: 'test' }, true], | ||
[{ }, false] | ||
[{}, false] | ||
]); | ||
@@ -121,2 +121,37 @@ | ||
it('validates xor with number types', function (done) { | ||
var schema = Joi.object({ | ||
code: Joi.number().xor('upc'), | ||
upc: Joi.number() | ||
}); | ||
Validate(schema, [ | ||
[{ upc: 123 }, true], | ||
[{ code: 456 }, true], | ||
[{ code: 456, upc: 123 }, false], | ||
[{}, false] | ||
]); | ||
done(); | ||
}); | ||
it('validates xor when empty value of peer allowed', function (done) { | ||
var schema = Joi.object({ | ||
code: Joi.string().xor('upc'), | ||
upc: Joi.string().allow('') | ||
}); | ||
Validate(schema, [ | ||
[{ upc: '' }, true], | ||
[{ upc: '123' }, true], | ||
[{ code: '456' }, true], | ||
[{ code: '456', upc: '' }, false], | ||
[{}, false] | ||
]); | ||
done(); | ||
}); | ||
it('validates or', function (done) { | ||
@@ -142,4 +177,4 @@ | ||
[{ txt: 'test', upc: '' }, true], | ||
[{ txt: '', upc: 'test' }, true], | ||
[{ txt: null, upc: 'test' }, true], | ||
[{ txt: '', upc: 'test' }, false], | ||
[{ txt: null, upc: 'test' }, false], | ||
[{ txt: undefined, upc: 'test' }, true], | ||
@@ -150,3 +185,3 @@ [{ txt: 'test', upc: undefined }, true], | ||
[{ txt: '', upc: undefined }, false], | ||
[{ txt: '', upc: undefined, code: 999 }, true], | ||
[{ txt: '', upc: undefined, code: 999 }, false], | ||
[{ txt: '', upc: undefined, code: undefined }, false], | ||
@@ -153,0 +188,0 @@ [{ txt: '', upc: '' }, false], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
334205
4301
0