Joi: Custom validator error handling

Created on 29 Nov 2019  路  4Comments  路  Source: sideway/joi

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community?
  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 12.12.0
  • module version: 16.1.7
  • environment (e.g. node, browser, native): Node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information: no

How can we help?

I am trying to understand how to make custom errors in custom validators, and what properties I can control with it. But I can't seem to figure out the syntax of it.

This one

helpers.message({ message: 'hello world', code: 'any.custom', details: 'hi' });

gives me:

Error code "custom" is not defined, your custom type is missing the correct messages definition].

This one results in

helpers.error('string.UUID', { key: 'properties', value: 'what' });
Error code "string.UUID" is not defined, your custom type is missing the correct messages definition]

The example from docs like:

helpers.error('string.UUID')
Works, but I don't control anything here.

Is there a way to control data that will be in the details property on ValidationError, or am I missing something? Docs are lacking the more complex example about this, and I am kind of lost here.

support

Most helpful comment

@TheFedaikin helpers.message({ custom: 'hello world' }); should work

All 4 comments

const bookSchema = Joi.object({ title: Joi.string().required().error((error)=>{ return { message:'title is must' } }).label('title'), author: Joi.string().required(), });

Getting error after executing this code :
Error :
Error: error() must return an Error object

Thanks

Unfortunately, no community resources were available to help resolve this issue after two weeks, and it is being closed. We close unresolved community issues to keep the issue tracker organized and effective. Please check https://hapi.dev/support for other support options.

@TheFedaikin helpers.message({ custom: 'hello world' }); should work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

normancarcamo picture normancarcamo  路  3Comments

ashrafkm picture ashrafkm  路  3Comments

JbIPS picture JbIPS  路  4Comments

Dreamystify picture Dreamystify  路  4Comments

sergibondarenko picture sergibondarenko  路  3Comments