Skip to content

Commit

Permalink
fix: label false should also hide explicit labels (#3034)
Browse files Browse the repository at this point in the history
Fixes #3033
  • Loading branch information
Marsup authored May 2, 2024
1 parent f02df4c commit 2d26030
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ exports.template = function (value, messages, code, state, prefs) {

exports.label = function (flags, state, prefs, messages) {

if (flags.label) {
return flags.label;
}

if (!prefs.errors.label) {
return '';
}

if (flags.label) {
return flags.label;
}

let path = state.path;
if (prefs.errors.label === 'key' &&
state.path.length > 1) {
Expand Down
15 changes: 15 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,21 @@ describe('errors', () => {
expect(schema.validate({ x: { y: { a: [1] } } }, { errors: { label: 'key' } }).error).to.be.an.error('"[0]" must be a string');
});

it('removes labels when label is false', () => {

const schema = Joi.object({
x: Joi.object({
y: Joi.object({
z: Joi.valid('z').label('z'),
a: Joi.array().items(Joi.string().label('item'))
})
})
}).options({ errors: { label: false } });

expect(schema.validate({ x: { y: { z: 'o' } } }).error).to.be.an.error('must be [z]');
expect(schema.validate({ x: { y: { a: [1] } } }).error).to.be.an.error('must be a string');
});

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

it('annotates error', () => {
Expand Down

0 comments on commit 2d26030

Please sign in to comment.