hapijs / joi

The most powerful data validation library for JS
Other
20.89k stars 1.51k forks source link

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

Closed ZachHaber closed 5 months ago

ZachHaber commented 5 months ago

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 */
}
Marsup commented 5 months ago

Thanks for the report, it has been fixed in 17.13.1.