Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

863
Vistas
How to make custom error message using Joi?

How to make a custom message using joi? i saw many answered question related on this but i dont know why it didnt work on my end, the error message always appeared is "Student" does not contain 1 required value(s) what i want is "Student" This field is required.

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.')
    ),
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can return a custom error object using Error constructor like this:

 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); 
        }
   });
about 4 years ago · Juan Pablo Isaza Denunciar

0

The easiest way in my opinion would be this.

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();
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda