Skip to content

Commit

Permalink
Merge pull request #1 from dhm116/ft-add-support-for-nested-objects
Browse files Browse the repository at this point in the history
Updates for handling labels for nested peers
  • Loading branch information
BolajiOlajide committed Aug 5, 2018
2 parents e01a087 + d6c0aa9 commit 888d24f
Showing 1 changed file with 181 additions and 0 deletions.
181 changes: 181 additions & 0 deletions test/types/object.js
Expand Up @@ -1990,6 +1990,29 @@ describe('object', () => {
});
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).with('a', ['b.c']);
const error = schema.validate({ a: 1 , b: { d: 2 } }).error;
expect(error).to.be.an.error('"first" missing required peer "second"');
expect(error.details).to.equal([{
message: '"first" missing required peer "second"',
path: ['a'],
type: 'object.with',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'second',
label: 'a',
key: 'a'
}
}]);
});

describe('without()', () => {

it('should throw an error when a parameter is not a string', () => {
Expand Down Expand Up @@ -2099,6 +2122,29 @@ describe('object', () => {
}
}]);
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).without('a', ['b.c']);
const error = schema.validate({ a: 1, b: { c: 'c' } }).error;
expect(error).to.be.an.error('"first" conflict with forbidden peer "second"');
expect(error.details).to.equal([{
message: '"first" conflict with forbidden peer "second"',
path: ['a'],
type: 'object.without',
context: {
main: 'a',
mainWithLabel: 'first',
peer: 'b.c',
peerWithLabel: 'second',
label: 'a',
key: 'a'
}
}]);
});
});

describe('xor()', () => {
Expand Down Expand Up @@ -2195,6 +2241,48 @@ describe('object', () => {
}
}]);
});

it('should apply labels without any nested peers', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).xor('a', 'b.c');
const error = schema.validate({}).error;
expect(error).to.be.an.error('"value" must contain at least one of [first, second]');
expect(error.details).to.equal([{
message: '"value" must contain at least one of [first, second]',
path: [],
type: 'object.missing',
context: {
peers: ['a', 'b.c'],
peersWithLabels: ['first', 'second'],
label: 'value',
key: undefined
}
}] );
});

it('should apply labels with too many nested peers', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).xor('a', 'b.c');
const error = schema.validate({ a: 1, b: { c: 'c' } }).error;
expect(error).to.be.an.error('"value" contains a conflict between exclusive peers [first, second]');
expect(error.details).to.equal([{
message: '"value" contains a conflict between exclusive peers [first, second]',
path: [],
type: 'object.xor',
context: {
peers: ['a', 'b.c'],
peersWithLabels: ['first', 'second'],
label: 'value',
key: undefined
}
}]);
});
});

describe('or()', () => {
Expand Down Expand Up @@ -2293,6 +2381,28 @@ describe('object', () => {
}
}]);
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).or('a', 'b.c');
const error = schema.validate({}).error;
expect(error).to.be.an.error('"value" must contain at least one of [first, second]');
expect(error.details).to.equal([{
message: '"value" must contain at least one of [first, second]',
path: [],
type: 'object.missing',
context:
{
peers: ['a', 'b.c'],
peersWithLabels: ['first', 'second'],
label: 'value',
key: undefined
}
}]);
});
});

describe('and()', () => {
Expand Down Expand Up @@ -2351,6 +2461,54 @@ describe('object', () => {
}
}]);
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).and('a', 'b.c');
const error = schema.validate({ a: 1 }).error;
expect(error).to.be.an.error('"value" contains [first] without its required peers [second]');
expect(error.details).to.equal([{
message: '"value" contains [first] without its required peers [second]',
path: [],
type: 'object.and',
context:
{
present: ['a'],
presentWithLabels: ['first'],
missing: ['b.c'],
missingWithLabels: ['second'],
label: 'value',
key: undefined
}
}]);
});

it('should apply labels with invalid nested peers', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).and('a', 'c.d');
const error = schema.validate({ a: 1, b: { d: 1 } }).error;
expect(error).to.be.an.error('"value" contains [first] without its required peers [c.d]');
expect(error.details).to.equal([{
message: '"value" contains [first] without its required peers [c.d]',
path: [],
type: 'object.and',
context:
{
present: ['a'],
presentWithLabels: ['first'],
missing: ['c.d'],
missingWithLabels: ['c.d'],
label: 'value',
key: undefined
}
}]);
});
});

describe('nand()', () => {
Expand Down Expand Up @@ -2408,6 +2566,29 @@ describe('object', () => {
}
}]);
});

it('should apply labels with nested objects', () => {

const schema = Joi.object({
a: Joi.number().label('first'),
b: Joi.object({ c: Joi.string().label('second'), d: Joi.number() })
}).nand('a', 'b.c');
const error = schema.validate({ a: 1, b: { c: 'c' } }).error;
expect(error).to.be.an.error('"first" must not exist simultaneously with [second]');
expect(error.details).to.equal([{
message: '"first" must not exist simultaneously with [second]',
path: [],
type: 'object.nand',
context: {
main: 'a',
mainWithLabel: 'first',
peers: ['b.c'],
peersWithLabels: ['second'],
label: 'value',
key: undefined
}
}]);
});
});

describe('assert()', () => {
Expand Down

0 comments on commit 888d24f

Please sign in to comment.