Skip to content

Setting errors.label to false does not remove labels from error messages for keys with labels #3033

@ZachHaber

Description

@ZachHaber

Runtime

node.js, browser

Runtime version

v18.17.0, chrome 122

Module version

17.13.0

Last module version without issue

No response

Used with

Hapi, and standalone

Any other relevant information

No response

What are you trying to achieve or the steps to reproduce?

I'm using Joi validation within a form, in which I'd like to not show the name of the field in the error messages as the errors will be displayed underneath the fields, so they don't need the additional context. I'm also using the same schema for validation within my Hapi backend, which I'd like to have the labels for.

The issue is that if you specify a label for a key, then the error messages don't respect the errors.label=false option, but keys without a label do respect it. If this is working as designed, then the docs should be adjusted for clarity:

false - remove any label prefix from error message, including the "".

Reduced example (from the schema tester):

Schema:

Joi.object({
  username: Joi.string().alphanum().min(3).max(30).required(),
  birth_year: Joi.number().integer().min(1900).max(2013).label('Birth Year'),
}).options({abortEarly:false, errors: {label: false}})

Data:

{ birth_year: 2014 }

What was the result you got?

{
  "birth_year" /* "Birth Year" must be less than or equal to 2013 */: 2014,
  "username": undefined /* is required */
}

What result did you expect?

{
  "birth_year" /* must be less than or equal to 2013 */: 2014,
  "username": undefined /* is required */
}

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions