Home > OS >  How to get customised message for Joi label
How to get customised message for Joi label

Time:12-02

What currently I have is this:


example: Joi.boolean()
        .required()
        .invalid(false)
        .label('Disclaimer Checkbox'),

and the output which I am getting is "Disclaimer checkbox contains invalid value".

What I need is label which contain only message which I wrote not "contains invalid value"

CodePudding user response:

  a: Joi.string()
    .min(2)
    .max(10)
    .required()
    .messages({
      'string.base': `"a" should be a type of 'text'`,
      'string.empty': `"a" cannot be an empty field`,
      'string.min': `"a" should have a minimum length of {#limit}`,
      'any.required': `"a" is a required field`
    })
});

CodePudding user response:

When defining joi validations , you need to assert messages along with their validation string to define an error message against it , like the example below.

bodyValidation: Joi.integer()
    .min(1997)
    .max(2022)
    .regex(/[0-9],{4}/)
    .required()
    .messages({
      'integer.base': `Entered data should be an integer`,
      'integer.regex': `Entered field is not an year`,
      'integer.min': `Entered Year is less than 1997`,
      'any.required': `Entry is required`
    })
});
  • Related