¿Cómo hacer un mensaje personalizado usando joi? vi muchas preguntas respondidas relacionadas con esto, pero no sé por qué no funcionó en mi extremo, el mensaje de error siempre apareció es "Estudiante" no contiene 1 valor (es) requerido (s) lo que quiero es "Estudiante" Este campo es obligatorio.
export const VALIDATION_SCHEMA = { students: Joi.array() .label('Student Name(s)') .items( Joi.object({ name: Joi.string(), value: Joi.string() }).required().messages('"Student" This field is required.') ), }Puede devolver un objeto de error personalizado utilizando el constructor de errores como este:
var schema = Joi.object().keys({ firstName: Joi.string().min(4).max(8).required().error(new Error('error message here for first name')), lastName: Joi.string().min(5).max(1).required().error(new Error('error message here for last name')) }); Joi.validate(req.body, schema, function(err, value) { if (err) { console.log(err.message) return catched(err.message); } });La forma más fácil en mi opinión sería esta.
const Joi = require("@hapi/joi"); export const categorySchema = Joi.object({ mobile: Joi.string().trim().regex(/^[6-9]\d{9}$/).required().messages({ "string.base": `"" should be a type of string`, "string.empty": `"" must contain value`, "string.pattern.base": `"" must be 10 digit number`, "any.required": `"" is a required field` }), password: Joi.string().trim().required().messages({ "string.base": `"" should be a type of 'text'`, "string.pattern.base": `"" must be 10 digit number`, "any.required": `"" is a required field` }), }).required();