Skip to content

Commit

Permalink
Merge pull request #1132 from WesTyler/options_describe
Browse files Browse the repository at this point in the history
Add "options" to describe output
  • Loading branch information
Marsup authored May 13, 2017
2 parents 257e65e + 00f562d commit c25856c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internals.Alternatives = class extends Any {

let errors = [];
const il = this._inner.matches.length;
const baseType = this._settings && this._settings.baseType;
const baseType = this._baseType;

for (let i = 0; i < il; ++i) {
const item = this._inner.matches[i];
Expand Down Expand Up @@ -107,9 +107,10 @@ internals.Alternatives = class extends Any {
otherwise: options.otherwise !== undefined ? Cast.schema(options.otherwise) : undefined
};

if (obj._settings && obj._settings.baseType) {
item.then = item.then && obj._settings.baseType.concat(item.then);
item.otherwise = item.otherwise && obj._settings.baseType.concat(item.otherwise);
if (obj._baseType) {

item.then = item.then && obj._baseType.concat(item.then);
item.otherwise = item.otherwise && obj._baseType.concat(item.otherwise);
}

Ref.push(obj._refs, item.ref);
Expand Down
11 changes: 10 additions & 1 deletion lib/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ module.exports = internals.Any = class {
obj.isJoi = true;
obj._type = this._type;
obj._settings = internals.concatSettings(this._settings);
obj._baseType = this._baseType;
obj._valids = Hoek.clone(this._valids);
obj._invalids = Hoek.clone(this._invalids);
obj._tests = this._tests.slice();
Expand Down Expand Up @@ -383,7 +384,7 @@ module.exports = internals.Any = class {
Alternatives = Alternatives || require('./alternatives');
const obj = Alternatives.when(ref, { is: options.is, then, otherwise });
obj._flags.presence = 'ignore';
obj._settings = internals.concatSettings(obj._settings, { baseType: this });
obj._baseType = this;

return obj;
}
Expand Down Expand Up @@ -704,6 +705,14 @@ module.exports = internals.Any = class {
}
}

if (this._settings) {
description.options = Hoek.clone(this._settings);
}

if (this._baseType) {
description.base = this._baseType.describe();
}

if (this._description) {
description.description = this._description;
}
Expand Down
62 changes: 62 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,61 @@ describe('any', () => {
}).to.not.throw();
done();
});

it('describes a schema with options', (done) => {

const schema = Joi.any().options({ abortEarly: false, convert: false });
const description = schema.describe();

expect(description).to.equal({ type: 'any', options: { abortEarly: false, convert: false } });
done();
});

it('describes an alternatives schema with options', (done) => {

const schema = Joi.number().min(10).when('a', { is: 5, then: Joi.number().max(20).required() }).options({ abortEarly: false, convert: false }).describe();
expect(schema).to.equal({
type: 'alternatives',
flags: {
presence: 'ignore'
},
options: {
abortEarly: false,
convert: false
},
base: {
type: 'number',
invalids: [
Infinity,
-Infinity
],
rules: [
{ arg: 10, name: 'min' }
]
},
alternatives: [{
ref: 'ref:a',
is: {
type: 'number',
flags: {
allowOnly: true,
presence: 'required'
},
valids: [5],
invalids: [Infinity, -Infinity]
},
then: {
type: 'number',
flags: {
presence: 'required'
},
invalids: [Infinity, -Infinity],
rules: [{ name: 'min', arg: 10 }, { name: 'max', arg: 20 }]
}
}]
});
done();
});
});

describe('label()', () => {
Expand Down Expand Up @@ -1560,6 +1615,13 @@ describe('any', () => {
flags: {
presence: 'ignore'
},
base: {
type: 'number',
invalids: [Infinity, -Infinity],
rules: [
{ arg: 10, name: 'min' }
]
},
alternatives: [{
ref: 'ref:a',
is: {
Expand Down
8 changes: 6 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ describe('Joi', () => {
defaultRef: Joi.string().default(defaultRef, 'not here'),
defaultFn: Joi.string().default(defaultFn, 'not here'),
defaultDescribedFn: Joi.string().default(defaultDescribedFn, 'described test')
}).rename('renamed', 'required').without('required', 'xor').without('xor', 'required');
}).options({ abortEarly: false, convert: false }).rename('renamed', 'required').without('required', 'xor').without('xor', 'required');

const result = {
type: 'object',
Expand Down Expand Up @@ -1660,7 +1660,11 @@ describe('Joi', () => {
override: false
}
}
]
],
options: {
abortEarly: false,
convert: false
}
};

it('describes schema (direct)', (done) => {
Expand Down
9 changes: 9 additions & 0 deletions test/ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ describe('ref', () => {
expect(desc).to.equal({
type: 'alternatives',
flags: { presence: 'ignore' },
base: {
type: 'any',
flags: {
allowOnly: true,
default: 'ref:a.b'
},
invalids: ['context:b.c'],
valids: ['ref:a.b']
},
alternatives: [{
ref: 'ref:a.b',
is: {
Expand Down

0 comments on commit c25856c

Please sign in to comment.