Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not override existing labels of underlying schemas in alternatives #3001

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ module.exports = Any.extend({
label(name) {

const obj = this.$_parent('label', name);
const each = (item, source) => (source.path[0] !== 'is' ? item.label(name) : undefined);
const each = (item, source) => {

return source.path[0] !== 'is' && typeof item._flags.label !== 'string' ? item.label(name) : undefined;
};

return obj.$_modify({ each, ref: false });
}
},
Expand Down
143 changes: 143 additions & 0 deletions test/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,149 @@ describe('alternatives', () => {
}
});
});

it('does not override existing labels in nested alternatives', () => {

const schema = Joi.alternatives().try(
Joi.boolean().label('boolean'),
Joi.alternatives().try(
Joi.string().label('string'),
Joi.number().label('number')
).label('string or number')
);

expect(schema.describe()).to.equal({
type: 'alternatives',
matches: [
{
schema: {
type: 'boolean',
flags: {
label: 'boolean'
}
}
},
{
schema: {
type: 'alternatives',
flags: {
label: 'string or number'
},
matches: [
{
schema: {
type: 'string',
flags: {
label: 'string'
}
}
},
{
schema: {
type: 'number',
flags: {
label: 'number'
}
}
}
]
}
}
]
});
});

it('does not override existing label of then', () => {

const schema = Joi.object({
a: Joi.boolean(),
b: Joi.alternatives()
.conditional('a', { is: true, then: Joi.string().label('not x') })
.label('x')
});

expect(schema.describe()).to.equal({
type: 'object',
keys: {
a: {
type: 'boolean'
},
b: {
type: 'alternatives',
flags: {
label: 'x'
},
matches: [
{
is: {
type: 'any',
allow: [{ override: true }, true],
flags: {
only: true,
presence: 'required'
}
},
ref: {
path: ['a']
},
then: {
type: 'string',
flags: {
label: 'not x'
}
}
}
]
}
}
});
});

it('does not override existing label of otherwise', () => {

const schema = Joi.object({
a: Joi.boolean(),
b: Joi.alternatives()
.conditional('a', { is: true, otherwise: Joi.string().label('not x') })
.label('x')
});

expect(schema.describe()).to.equal({
type: 'object',
keys: {
a: {
type: 'boolean'
},
b: {
type: 'alternatives',
flags: {
label: 'x'
},
matches: [
{
is: {
type: 'any',
allow: [{ override: true }, true],
flags: {
only: true,
presence: 'required'
}
},
ref: {
path: ['a']
},
otherwise: {
type: 'string',
flags: {
label: 'not x'
}
}
}
]
}
}
});
});
});

describe('match()', () => {
Expand Down
Loading