Abhay S
Abhay S

Reputation: 103

How to get all validation error in once with Joi?

I am facing issue related to Joi validation, when ever i send the request to joi it throws only single error.

var CreateValidationSchema = Joi.object().keys({
      name: Joi.string().required().max(255).label("Name"),
      branch_name: Joi.string().required().max(255).label("Branch Name"),
      ifsc_code: Joi.string().required().max(15).label("IFSC Code"),
      micr_code: Joi.string().required().max(15).label("MICR Code"),
      swift_code: Joi.string().required().max(15).label("Swift Code"),
      address: Joi.string().optional().allow(null).allow("").label("Address"),
      description: Joi.string().optional().allow(null).allow("").label("Description"),
      is_approved: Joi.boolean().required().default(0).label("Approved")
});

Joi.validate(req.body, CreateValidationSchema).then(() => {
      next();
}).catch((error) => {
      _Response.ErrorResponse(res, req.lang, _Response.MESSAGES.VALIDATION_ERROR, error)
})

My schema is like above, please help.

Upvotes: 10

Views: 4319

Answers (1)

Parshav Shah
Parshav Shah

Reputation: 211

Just apply { abortEarly: false } in Joi.validate() function

Like :

Joi.validate(req.body, CreateValidationSchema, { abortEarly: false } ).then(() => {
      next();
}).catch((error) => {
      _Response.ErrorResponse(res, req.lang, _Response.MESSAGES.VALIDATION_ERROR, error)
})

May be it will helps

Upvotes: 15

Related Questions