Joi: User-friendly validation error messages.

Created on 10 Dec 2016  路  6Comments  路  Source: sideway/joi

It would be really nice if Joi presented user-friendly validation messages instead of things like

child "rfid" fails because ["rfid" must be a number]

Which is not appropriate for presentation for a user.

For example, bookshelf-modelbase includes a really convenient mechanism for mapping Joi validators to model fields. Except the entire thing is essentially useless because in order to present messages to the user, you still have to reimplement all of the validation yourself, one field at a time, to construct user friendly error messages.

The same holds true for any API that provides any sort of Joi-based validation. All such efforts are rendered entirely useless because of the error messages and the fact that you must do validation manually in all cases to translate errors.

It also makes maintenance more difficult, because you must specify validation information in two places instead of one: Instead of specifying e.g. a min/max value only to Joi, you must also specify it in your own error messages so that if you change the min and max, you must also maintain the string "Value must be between 2 and 12" (e.g.) to match.

It really limits the usefulness of Joi. If you have to make all your own error messages anyways, there's literally no point in using Joi over just using if (value < 2 || value > 12).

non issue

Most helpful comment

All 6 comments

For anybody who has found this, more user-friendly ones are in err.details as an array for Joi errors. Not ideal but not awful.

@JC3 you can also experiment with overriding errors: https://github.com/hapijs/joi/blob/master/API.md#anyerrorerr

@WesTyler That looks useful, too. Thanks.

I wrote a post going over how you can do custom error messages with localisation all with Joi here: https://medium.com/@Yuschick/building-custom-localised-error-messages-with-joi-4a348d8cc2ba

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

REBELinBLUE picture REBELinBLUE  路  3Comments

sergibondarenko picture sergibondarenko  路  3Comments

farwayer picture farwayer  路  3Comments

mohamadresaaa picture mohamadresaaa  路  3Comments

normancarcamo picture normancarcamo  路  3Comments