Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to capture the value of field being validated in custom error messages using the options - language #1479

Closed
saikumarr opened this issue Apr 19, 2018 · 2 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@saikumarr
Copy link

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.

@saikumarr saikumarr changed the title Is it possible to capture the value of field being validated in cutom error messages in the options - language Is it possible to capture the value of field being validated in custom error messages in the options - language Apr 19, 2018
@saikumarr saikumarr changed the title Is it possible to capture the value of field being validated in custom error messages in the options - language Is it possible to capture the value of field being validated in custom error messages using the options - language Apr 19, 2018
@Marsup Marsup self-assigned this Apr 27, 2018
@Marsup Marsup added the request label Apr 28, 2018
@Marsup Marsup added this to the 13.3.0 milestone Apr 28, 2018
@Marsup Marsup closed this as completed in 6e8fd72 May 8, 2018
@FabricioMatteMassive
Copy link

@Marsup I'm still experiencing this issue, interpolating {{value}} on number.base gives undefined. Joi version 13.4.0.

Is it not supported? Or is this a bug?

@Marsup
Copy link
Collaborator

Marsup commented Aug 10, 2018

@FabricioMatteMassive this issue was about any.invalid, not number.base. I'm adding those as they come. Now released.

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants