joi how to display a custom error messages 3f

Solutions on MaxInterview for joi how to display a custom error messages 3f by the best coders in the world

showing results for - "joi how to display a custom error messages 3f"
Elaine
31 Feb 2016
1const Joi = require('joi');
2
3const joiSchema = Joi.object({
4  a: Joi.string()
5    .min(2)
6    .max(10)
7    .required()
8    .messages({
9      'string.base': `"a" should be a type of 'text'`,
10      'string.empty': `"a" cannot be an empty field`,
11      'string.min': `"a" should have a minimum length of {#limit}`,
12      'any.required': `"a" is a required field`
13    })
14});
15
16const validationResult = joiSchema.validate({ a: 2 }, { abortEarly: false });
17console.log(validationResult.error); // expecting ValidationError: "a" should be a type of 'text'
18