Skip to content

Commit

Permalink
Cleanup for #1183
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed May 13, 2017
1 parent dcc6f4c commit 78843fa
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 110 deletions.
12 changes: 12 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,18 @@ const optionalSchema = schema.optionalKeys('a.b', 'c.d');

The behavior is exactly the same as `requiredKeys`.

#### `object.forbiddenKeys(children)`

Sets the specified children to forbidden.
- `children` - can be a single string value, an array of string values, or each child provided as an argument.

```js
const schema = Joi.object().keys({ a: { b: Joi.number().required() }, c: { d: Joi.string().required() } });
const optionalSchema = schema.forbiddenKeys('a.b', 'c.d');
```

The behavior is exactly the same as `requiredKeys`.

### `string` - inherits from `Any`

Generates a schema object that matches a string data type. Note that empty strings are not allowed by default and must
Expand Down
110 changes: 0 additions & 110 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,116 +1647,6 @@ describe('any', () => {
});
});

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

it('should set keys as required', (done) => {

const schema = Joi.object({ a: 0, b: 0, c: { d: 0, e: { f: 0 } }, g: { h: 0 } })
.requiredKeys('a', 'b', 'c.d', 'c.e.f', 'g');
Helper.validate(schema, [
[{}, false, null, 'child "a" fails because ["a" is required]'],
[{ a: 0 }, false, null, 'child "b" fails because ["b" is required]'],
[{ a: 0, b: 0 }, false, null, 'child "g" fails because ["g" is required]'],
[{ a: 0, b: 0, g: {} }, true],
[{ a: 0, b: 0, c: {}, g: {} }, false, null, 'child "c" fails because [child "d" fails because ["d" is required]]'],
[{ a: 0, b: 0, c: { d: 0 }, g: {} }, true],
[{ a: 0, b: 0, c: { d: 0, e: {} }, g: {} }, false, null, 'child "c" fails because [child "e" fails because [child "f" fails because ["f" is required]]]'],
[{ a: 0, b: 0, c: { d: 0, e: { f: 0 } }, g: {} }, true]
], done);
});

it('should work on types other than objects', (done) => {

const schemas = [Joi.array(), Joi.binary(), Joi.boolean(), Joi.date(), Joi.func(), Joi.number(), Joi.string()];
schemas.forEach((schema) => {

expect(() => {

schema.applyFunctionToChildren([''], 'required');
}).to.not.throw();

expect(() => {

schema.applyFunctionToChildren(['', 'a'], 'required');
}).to.throw();

expect(() => {

schema.applyFunctionToChildren(['a'], 'required');
}).to.throw();
});

done();
});

it('should throw on unknown key', (done) => {

expect(() => {

Joi.object({ a: 0, b: 0 }).requiredKeys('a', 'c', 'b', 'd', 'd.e.f');
}).to.throw(Error, 'unknown key(s) c, d');

expect(() => {

Joi.object({ a: 0, b: 0 }).requiredKeys('a', 'b', 'a.c.d');
}).to.throw(Error, 'unknown key(s) a.c.d');

done();
});

it('should throw on empty object', (done) => {

expect(() => {

Joi.object().requiredKeys('a', 'c', 'b', 'd');
}).to.throw(Error, 'unknown key(s) a, b, c, d');
done();
});

it('should not modify original object', (done) => {

const schema = Joi.object({ a: 0 });
const requiredSchema = schema.requiredKeys('a');
schema.validate({}, (err) => {

expect(err).to.not.exist();

requiredSchema.validate({}, (err) => {

expect(err).to.exist();
done();
});
});
});
});

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

it('should set keys as optional', (done) => {

const schema = Joi.object({ a: Joi.number().required(), b: Joi.number().required() }).optionalKeys('a', 'b');
Helper.validate(schema, [
[{}, true],
[{ a: 0 }, true],
[{ a: 0, b: 0 }, true]
], done);
});
});
describe('forbiddenKeys()', () => {

it('should set keys as forbidden', (done) => {

const schema = Joi.object({ a: Joi.number().required(), b: Joi.number().required() }).forbiddenKeys('a', 'b');
Helper.validate(schema, [
[{}, true],
[{ a: undefined }, true],
[{ a: undefined, b: undefined }, true],
[{ a: 0 }, false, null, 'child "a" fails because ["a" is not allowed]'],
[{ b: 0 }, false, null, 'child "b" fails because ["b" is not allowed]']
], done);
});
});

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

it('should void values when considered empty', (done) => {
Expand Down
111 changes: 111 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -1531,4 +1531,115 @@ describe('object', () => {
});

});

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

it('should set keys as required', (done) => {

const schema = Joi.object({ a: 0, b: 0, c: { d: 0, e: { f: 0 } }, g: { h: 0 } })
.requiredKeys('a', 'b', 'c.d', 'c.e.f', 'g');
Helper.validate(schema, [
[{}, false, null, 'child "a" fails because ["a" is required]'],
[{ a: 0 }, false, null, 'child "b" fails because ["b" is required]'],
[{ a: 0, b: 0 }, false, null, 'child "g" fails because ["g" is required]'],
[{ a: 0, b: 0, g: {} }, true],
[{ a: 0, b: 0, c: {}, g: {} }, false, null, 'child "c" fails because [child "d" fails because ["d" is required]]'],
[{ a: 0, b: 0, c: { d: 0 }, g: {} }, true],
[{ a: 0, b: 0, c: { d: 0, e: {} }, g: {} }, false, null, 'child "c" fails because [child "e" fails because [child "f" fails because ["f" is required]]]'],
[{ a: 0, b: 0, c: { d: 0, e: { f: 0 } }, g: {} }, true]
], done);
});

it('should work on types other than objects', (done) => {

const schemas = [Joi.array(), Joi.binary(), Joi.boolean(), Joi.date(), Joi.func(), Joi.number(), Joi.string()];
schemas.forEach((schema) => {

expect(() => {

schema.applyFunctionToChildren([''], 'required');
}).to.not.throw();

expect(() => {

schema.applyFunctionToChildren(['', 'a'], 'required');
}).to.throw();

expect(() => {

schema.applyFunctionToChildren(['a'], 'required');
}).to.throw();
});

done();
});

it('should throw on unknown key', (done) => {

expect(() => {

Joi.object({ a: 0, b: 0 }).requiredKeys('a', 'c', 'b', 'd', 'd.e.f');
}).to.throw(Error, 'unknown key(s) c, d');

expect(() => {

Joi.object({ a: 0, b: 0 }).requiredKeys('a', 'b', 'a.c.d');
}).to.throw(Error, 'unknown key(s) a.c.d');

done();
});

it('should throw on empty object', (done) => {

expect(() => {

Joi.object().requiredKeys('a', 'c', 'b', 'd');
}).to.throw(Error, 'unknown key(s) a, b, c, d');
done();
});

it('should not modify original object', (done) => {

const schema = Joi.object({ a: 0 });
const requiredSchema = schema.requiredKeys('a');
schema.validate({}, (err) => {

expect(err).to.not.exist();

requiredSchema.validate({}, (err) => {

expect(err).to.exist();
done();
});
});
});
});

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

it('should set keys as optional', (done) => {

const schema = Joi.object({ a: Joi.number().required(), b: Joi.number().required() }).optionalKeys('a', 'b');
Helper.validate(schema, [
[{}, true],
[{ a: 0 }, true],
[{ a: 0, b: 0 }, true]
], done);
});
});

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

it('should set keys as forbidden', (done) => {

const schema = Joi.object({ a: Joi.number().required(), b: Joi.number().required() }).forbiddenKeys('a', 'b');
Helper.validate(schema, [
[{}, true],
[{ a: undefined }, true],
[{ a: undefined, b: undefined }, true],
[{ a: 0 }, false, null, 'child "a" fails because ["a" is not allowed]'],
[{ b: 0 }, false, null, 'child "b" fails because ["b" is not allowed]']
], done);
});
});
});

0 comments on commit 78843fa

Please sign in to comment.