Skip to content

Commit

Permalink
Move description, unit, label to flags. For #1867
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Jul 12, 2019
1 parent dfda451 commit 5e17771
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 54 deletions.
33 changes: 2 additions & 31 deletions lib/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,8 @@ exports.describe = function (schema) {
// Flags

for (const flag in schema._flags) {
if (flag[0] === '_') {
continue;
}

const value = schema._flags[flag];
switch (flag) {

case 'description':
description.description = value;
break;

case 'empty':
description.flags[flag] = value.describe();
break;

case 'default':
case 'failover':
description.flags[flag] = internals.describe(value);
break;

case 'label':
description.label = value;
break;

case 'unit':
description.unit = value;
break;

default:
description.flags[flag] = value;
break;
if (flag[0] !== '_') {
description.flags[flag] = internals.describe(schema._flags[flag]);
}
}

Expand Down
24 changes: 12 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2580,8 +2580,8 @@ describe('Joi', () => {
const schema = defaultJoi.optional();
expect(schema.describe()).to.equal({
type: 'any',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'optional'
}
});
Expand All @@ -2594,8 +2594,8 @@ describe('Joi', () => {
expect(schema.describe()).to.equal({
type: 'string',
invalids: [''],
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required'
}
});
Expand All @@ -2607,15 +2607,15 @@ describe('Joi', () => {
const schema = defaultJoi.object({ foo: 'bar' });
expect(schema.describe()).to.equal({
type: 'object',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required'
},
children: {
foo: {
type: 'string',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required',
allowOnly: true
},
Expand All @@ -2638,16 +2638,16 @@ describe('Joi', () => {
});
expect(schema.describe()).to.equal({
type: 'object',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required'
},
children: {
foo: {
type: 'string',
description: 'defaulted',
flags: {
presence: 'required',
description: 'defaulted',
allowOnly: true
},
invalids: [''],
Expand All @@ -2656,18 +2656,18 @@ describe('Joi', () => {
baz: {
children: {
qux: {
description: 'defaulted2',
flags: {
allowOnly: true,
description: 'defaulted2',
presence: 'required'
},
invalids: [''],
type: 'string',
valids: ['zorg']
}
},
description: 'defaulted2',
flags: {
description: 'defaulted2',
presence: 'required'
},
type: 'object'
Expand All @@ -2689,16 +2689,16 @@ describe('Joi', () => {

expect(schema.describe()).to.equal({
type: 'object',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required',
cast: 'raw'
},
children: {
foo: {
type: 'string',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required',
allowOnly: true,
cast: 'raw'
Expand All @@ -2717,8 +2717,8 @@ describe('Joi', () => {
const schema = extendedJoi.foobar();
expect(schema.describe()).to.equal({
type: 'foobar',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required'
}
});
Expand All @@ -2731,8 +2731,8 @@ describe('Joi', () => {
const schema = defaultJoi.foobar();
expect(schema.describe()).to.equal({
type: 'foobar',
description: 'defaulted',
flags: {
description: 'defaulted',
presence: 'required'
}
});
Expand Down
2 changes: 1 addition & 1 deletion test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ describe('Manifest', () => {
notEmpty: {
type: 'string',
flags: {
description: 'a',
presence: 'required'
},
description: 'a',
notes: ['b'],
tags: ['c'],
invalids: ['']
Expand Down
16 changes: 11 additions & 5 deletions test/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,9 @@ describe('alternatives', () => {
matches: [{
peek: {
type: 'string',
label: 'foo',
flags: {
label: 'foo'
},
invalids: ['']
},
then: {
Expand Down Expand Up @@ -318,11 +320,13 @@ describe('alternatives', () => {

const outcome = {
type: 'alternatives',
description: 'd',
notes: ['f'],
tags: ['g'],
meta: ['b', 'c'],
examples: ['a'],
flags: {
description: 'd'
},
matches: [{
schema: {
type: 'string',
Expand Down Expand Up @@ -469,8 +473,10 @@ describe('alternatives', () => {

expect(labeled.describe()).to.equal({
base: { type: 'any' },
flags: { presence: 'ignore' },
label: 'Label b',
flags: {
label: 'Label b',
presence: 'ignore'
},
type: 'alternatives',
matches: [{
is: {
Expand All @@ -482,13 +488,13 @@ describe('alternatives', () => {
then: {
type: 'string',
flags: {
label: 'Label b',
empty: {
flags: { allowOnly: true },
type: 'string',
valids: ['']
}
},
label: 'Label b',
valids: [null],
invalids: ['']
}
Expand Down
12 changes: 7 additions & 5 deletions test/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,12 @@ describe('any', () => {
const desc = a.concat(b).describe();
expect(desc).to.equal({
type: 'any',
description: 'b',
tags: ['a', 'b'],
examples: ['a', 'b'],
unit: 'b'
flags: {
description: 'b',
unit: 'b'
}
});
});

Expand Down Expand Up @@ -1228,7 +1230,7 @@ describe('any', () => {
it('sets the description', () => {

const b = Joi.description('my description');
expect(b.describe().description).to.equal('my description');
expect(b.describe().flags.description).to.equal('my description');
});

it('throws when description is missing', () => {
Expand Down Expand Up @@ -1805,7 +1807,7 @@ describe('any', () => {
it('can describe a label', () => {

const schema = Joi.object().label('lbl').describe();
expect(schema).to.equal({ type: 'object', label: 'lbl' });
expect(schema).to.equal({ type: 'object', flags: { label: 'lbl' } });
});

it('does not leak into sub objects', async () => {
Expand Down Expand Up @@ -2592,7 +2594,7 @@ describe('any', () => {
it('sets the unit', () => {

const b = Joi.unit('milliseconds');
expect(b.describe().unit).to.equal('milliseconds');
expect(b.describe().flags.unit).to.equal('milliseconds');
});

it('throws when unit is missing', () => {
Expand Down

0 comments on commit 5e17771

Please sign in to comment.