Comparing version 2.2.0 to 2.3.0
@@ -14,3 +14,7 @@ { | ||
}, | ||
"xor": "{{key}} conflict with exclusive peer {{value}}", | ||
"xor": { | ||
"parent": "cannot specify peer when key is not part of an object", | ||
"peer": "{{key}} conflict with exclusive peer {{value}}", | ||
"missing": "at least one of {{key}} {{value}} is required" | ||
}, | ||
"or": { | ||
@@ -17,0 +21,0 @@ "parent": "cannot specify peer when key is not part of an object", |
@@ -158,6 +158,11 @@ // Load modules | ||
internals.with = function (peers) { | ||
internals.Any.prototype.with = function () { | ||
return function (value, state, options) { | ||
var peers = Utils.flatten(Array.prototype.slice.call(arguments)); | ||
for (var i = 0, li = peers.length; i < li; i++) { | ||
Utils.assert(typeof peers[i] === 'string', 'peers must be a string'); | ||
} | ||
this._test('with', peers, function (value, state, options) { | ||
if (!state.parent) { | ||
@@ -178,14 +183,4 @@ return internals.Any.error('any.with.parent', null, state); | ||
return null; | ||
}; | ||
}; | ||
}); | ||
internals.Any.prototype.with = function () { | ||
var peers = Utils.flatten(Array.prototype.slice.call(arguments)); | ||
for (var i = 0, li = peers.length; i < li; i++) { | ||
Utils.assert(typeof peers[i] === 'string', 'peers must be a string'); | ||
} | ||
this._test('with', peers, internals.with(peers)); | ||
return this; | ||
@@ -227,2 +222,4 @@ }; | ||
this._valids.remove(undefined); | ||
this._invalids.remove(undefined); | ||
@@ -233,13 +230,30 @@ this._invalids.remove(null); | ||
var peers = Utils.flatten(Array.prototype.slice.call(arguments)); | ||
var withFn = internals.with(peers); | ||
this._test('xor', peers, function (value, state, options) { | ||
var present = (value !== undefined && value !== null && value !== ''); | ||
var other = !withFn(value, state, options); | ||
if (present ^ other) { | ||
if (!state.parent) { | ||
return internals.Any.error('any.xor.parent', null, state); | ||
} | ||
var present = (value !== undefined && value !== null && value !== '' ? 1 : 0); | ||
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]] !== '') { | ||
++present; | ||
} | ||
} | ||
if (present === 1) { | ||
return null; | ||
} | ||
return internals.Any.error('any.xor', { value: peers }, state); | ||
if (present === 0) { | ||
return (present === 1 ? null : internals.Any.error('any.xor.missing', { value: peers }, state)); | ||
} | ||
return internals.Any.error('any.xor.peer', { value: peers }, state); | ||
}); | ||
@@ -365,3 +379,3 @@ | ||
var self = this; | ||
// Setup state and settings | ||
@@ -446,3 +460,3 @@ | ||
}; | ||
var finish = function () { | ||
@@ -449,0 +463,0 @@ |
// Load modules | ||
var Errors = require('./errors'); | ||
var Utils = require('./utils'); | ||
@@ -59,3 +58,3 @@ var Any = require('./any'); | ||
if (schema instanceof Any) { | ||
if (typeof schema.validate === 'function') { | ||
return schema.validate(object, options); | ||
@@ -72,3 +71,3 @@ } | ||
if (schema instanceof Any) { | ||
if (typeof schema.describe === 'function') { | ||
return schema.describe(); | ||
@@ -75,0 +74,0 @@ } |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"repository": "git://github.com/spumko/joi", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -45,2 +45,11 @@ // Load modules | ||
it('fails when with set on root', function (done) { | ||
var b = Joi.any(); | ||
var result = b.with('test'); | ||
expect(result.validate('test')).to.exist; | ||
done(); | ||
}); | ||
it('returns error when related type not found', function (done) { | ||
@@ -105,2 +114,34 @@ | ||
describe('#xor', function () { | ||
it('fails when without set on root', function (done) { | ||
var b = Joi.any(); | ||
var result = b.xor('test'); | ||
expect(result.validate('test')).to.exist; | ||
done(); | ||
}); | ||
it('should throw an error when a parameter is not a string', function (done) { | ||
try { | ||
b.xor({}); | ||
var error = false; | ||
} catch (e) { | ||
error = true; | ||
} | ||
expect(error).to.equal(true); | ||
try { | ||
b.xor(123); | ||
error = false; | ||
} catch (e) { | ||
error = true; | ||
} | ||
expect(error).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
describe('#or', function () { | ||
@@ -107,0 +148,0 @@ |
@@ -51,2 +51,21 @@ // Load modules | ||
it('validated with', function (done) { | ||
var schema = Joi.object({ | ||
txt: Joi.string().with('upc'), | ||
upc: Joi.string() | ||
}); | ||
Validate(schema, [ | ||
[{ upc: 'test' }, true], | ||
[{ txt: 'test' }, false], | ||
[{ txt: 'test', upc: null }, false], | ||
[{ txt: 'test', upc: '' }, false], | ||
[{ txt: 'test', upc: undefined }, false], | ||
[{ txt: 'test', upc: 'test' }, true] | ||
]); | ||
done(); | ||
}); | ||
it('validates xor', function (done) { | ||
@@ -60,3 +79,3 @@ | ||
var err = Joi.validate({ upc: null, txt: null }, schema, { abortEarly: false }); | ||
expect(err.message).to.equal('txt conflict with exclusive peer upc. upc conflict with exclusive peer txt'); | ||
expect(err.message).to.equal('at least one of txt upc is required. at least one of upc txt is required'); | ||
@@ -84,2 +103,19 @@ Validate(schema, [ | ||
it('validates multiple peers xor', function (done) { | ||
var schema = Joi.object({ | ||
txt: Joi.string().xor('upc', 'code'), | ||
upc: Joi.string(), | ||
code: Joi.string() | ||
}); | ||
Validate(schema, [ | ||
[{ upc: 'test' }, true], | ||
[{ txt: 'test' }, true], | ||
[{ }, false] | ||
]); | ||
done(); | ||
}); | ||
it('validates or', function (done) { | ||
@@ -86,0 +122,0 @@ |
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
333499
4276