joi icon indicating copy to clipboard operation
joi copied to clipboard

how don't use custom error message ?

Open syymo opened this issue 2 years ago • 2 comments

Context

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

How can we help?

//Insert your joi schema here 
Joi.object({
  age: Joi.number().required().messages({'age': test age required'}),
})
//Insert data to validate here 
{ 
}

result:

Validation Error: "age" is required

why error message is not test age required ?

image

syymo avatar Apr 26 '22 09:04 syymo

@syymo you can do it this way:

Joi.object({ age: Joi.number().required().error(new Error('test age required')), })

image

martin-petersen avatar Aug 03 '22 18:08 martin-petersen

Another way you could display the same message is by doing:

Joi.object({ age: Joi.number().required().messages({'any.required': 'test age required'}), })

martin-petersen avatar Aug 03 '22 18:08 martin-petersen

Joi.object({
  age: Joi.number().required().messages({'any.required': 'test age required'}),
})

{ "age" : " " }

can i change the message to age is required ??

Sreesanth46 avatar Mar 17 '23 11:03 Sreesanth46

Your failing rule in this case is number.base, as shown in the details of the error, so you need to change that message.

Marsup avatar Mar 18 '23 11:03 Marsup