Skip to content

Commit

Permalink
Merge pull request #3001 from hajekjiri/do-not-override-existing-labe…
Browse files Browse the repository at this point in the history
…ls-of-underlying-schemas-in-alternatives

fix: do not override existing labels of underlying schemas in alternatives
  • Loading branch information
Marsup authored Jan 15, 2024
2 parents 1a4b45f + 3113b72 commit 3fc290a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
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

0 comments on commit 3fc290a

Please sign in to comment.