Answers for "Joi how to display a custom error messages?"

0

Joi how to display a custom error messages?

const Joi = require('joi');

const joiSchema = Joi.object({
  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`
    })
});

const validationResult = joiSchema.validate({ a: 2 }, { abortEarly: false });
console.log(validationResult.error); // expecting ValidationError: "a" should be a type of 'text'
Posted by: Guest on August-10-2021

Code answers related to "Joi how to display a custom error messages?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language