-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
featureNew functionality or improvementNew functionality or improvement
Milestone
Description
Context
- node version:8.x
- joi version:latest
- environment (node, browser):node
- used with (hapi, standalone, ...): No
- any other relevant information:
I am using Joi Library with NodeJs/Typescript for validations. Validating a request body for a Post Operation.
I am using the language option to provide custom messages for respective field validations.
Below is the code
const bodySchema = Joi.object().keys({
// had to change from string to any so as to avoid multiple messages when empty
field1: Joi.any().required().valid('Dummy').options({
language: {
any: {
// wrt empty: specific error message not displaying with any but empty error is handled by allowOnly custom message. Anways there is no custom message for empty in requirements
// empty: '!!The parameter \'field1\' cannot be empty. It must be \'Dummy\'',
required: '!!The parameter \'field1\' is mandatory and must be the value \'Dummy\'',
allowOnly: '!!Invalid value for parameter \'field1\'. It must be \'Dummy\''
// how to capture value passed for field1 in the message?
}
}
}),
How to Capture the wrong field value passed as requestBody in the custom error message
if I pass request body for the POST endpoint
{
"field1": "wrongvalue",
}
Expected custom message Invalid value 'wrongvalue' for parameter 'field1'. It must be 'Dummy''
I have gone through JOI API but could not find any reference to doing this. While regex option has the facility to capture the value passed. {{value}} works in regex but not in other options.
Please let me know if there is a way to capture the value.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
featureNew functionality or improvementNew functionality or improvement