Comparing version 2.1.0 to 2.2.0
@@ -15,2 +15,6 @@ { | ||
"xor": "{{key}} conflict with exclusive peer {{value}}", | ||
"or": { | ||
"parent": "cannot specify peer when key is not part of an object", | ||
"peer": "missing alternative peers {{value}}" | ||
}, | ||
"rename": { | ||
@@ -17,0 +21,0 @@ "parent": "cannot rename when key is not part of an object", |
@@ -247,2 +247,46 @@ // Load modules | ||
internals.Any.prototype.or = function () { | ||
this._valids.remove(undefined); | ||
this._invalids.remove(undefined); | ||
this._invalids.remove(null); | ||
this._invalids.remove(''); | ||
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('or', peers, function (value, state, options) { | ||
if (!state.parent) { | ||
return internals.Any.error('any.or.parent', null, state); | ||
} | ||
if (value !== undefined && | ||
value !== null && | ||
value !== '') { | ||
return null; | ||
} | ||
for (var i = 0, il = peers.length; i < il; ++i) { | ||
var peer = peers[i]; | ||
if (state.parent.hasOwnProperty(peer) && | ||
state.parent[peer] !== undefined && | ||
state.parent[peer] !== null && | ||
state.parent[peer] !== '') { | ||
return null; | ||
} | ||
} | ||
return internals.Any.error('any.or.peer', { value: peers }, state); | ||
}); | ||
return this; | ||
}; | ||
internals.Any.prototype.rename = function (to, renameOptions) { | ||
@@ -249,0 +293,0 @@ |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"repository": "git://github.com/spumko/joi", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -26,2 +26,3 @@ <a href="https://github.com/spumko"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a> | ||
- [`any.xor(peer)`](#anyxorpeer) | ||
- [`any.or(peer)`](#anyorpeer) | ||
- [`description(desc)`](#descriptiondesc) | ||
@@ -273,2 +274,15 @@ - [`any.notes(notes)`](#anynotesnotes) | ||
#### `any.or(peer)` | ||
Defines a relationship with another key where this or one of the peers is required (and more than one is allowed) where: | ||
- `peer` - the key name that must appear if the current value is missing. `peer` can be an array of values, or multiple | ||
values can be passed as individual arguments. | ||
```javascript | ||
var schema = { | ||
a: Joi.any().or('b'), | ||
b: Joi.any() | ||
}; | ||
``` | ||
#### `description(desc)` | ||
@@ -594,3 +608,3 @@ | ||
var schema = { | ||
a: Joi.string().regex(\^[abc]+$\) | ||
a: Joi.string().regex(/^[abc]+$/) | ||
}; | ||
@@ -597,0 +611,0 @@ ``` |
@@ -104,2 +104,34 @@ // Load modules | ||
describe('#or', function () { | ||
it('fails when without set on root', function (done) { | ||
var b = Joi.any(); | ||
var result = b.or('test'); | ||
expect(result.validate('test')).to.exist; | ||
done(); | ||
}); | ||
it('should throw an error when a parameter is not a string', function (done) { | ||
try { | ||
b.or({}); | ||
var error = false; | ||
} catch (e) { | ||
error = true; | ||
} | ||
expect(error).to.equal(true); | ||
try { | ||
b.or(123); | ||
error = false; | ||
} catch (e) { | ||
error = true; | ||
} | ||
expect(error).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
describe('#rename', function () { | ||
@@ -106,0 +138,0 @@ |
@@ -82,2 +82,40 @@ // Load modules | ||
it('validates or', function (done) { | ||
var schema = Joi.object({ | ||
txt: Joi.string().or('upc', 'code'), | ||
upc: Joi.string().allow(null, ''), | ||
code: Joi.number() | ||
}); | ||
var err = Joi.validate({}, schema, { abortEarly: false }); | ||
expect(err.message).to.equal('missing alternative peers upc,code'); | ||
Validate(schema, [ | ||
[{ upc: null }, false], | ||
[{ upc: 'test' }, true], | ||
[{ txt: null }, false], | ||
[{ txt: 'test' }, true], | ||
[{ code: null }, false], | ||
[{ code: 123 }, true], | ||
[{ txt: 'test', upc: null }, true], | ||
[{ txt: 'test', upc: '' }, true], | ||
[{ txt: '', upc: 'test' }, true], | ||
[{ txt: null, upc: 'test' }, true], | ||
[{ txt: undefined, upc: 'test' }, true], | ||
[{ txt: 'test', upc: undefined }, true], | ||
[{ txt: 'test', upc: '' }, true], | ||
[{ txt: 'test', upc: null }, true], | ||
[{ txt: '', upc: undefined }, false], | ||
[{ txt: '', upc: undefined, code: 999 }, true], | ||
[{ txt: '', upc: undefined, code: undefined }, false], | ||
[{ txt: '', upc: '' }, false], | ||
[{ txt: 'test', upc: 'test' }, true], | ||
[{ txt: 'test', upc: 'test', code: 322 }, true] | ||
]); | ||
done(); | ||
}); | ||
it('validates an array of valid types', function (done) { | ||
@@ -84,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
330848
4204
692