Validation middleware using Joi.


  • joi: Pass in a Joi instance for the middleware to use. Default: require('Joi').
  • params, query, body and headers: these are the validators and respectively validate the request’s params, query, body and headers. Explained below.
  • onValidationError: Optional callback function that receives the ValidationError error as an argument. Can be used to adjust the error to your liking. You can throw any error to make the validation fail or return nothing to ignore the validation failure and continue with the request anyway.
  • Everything else you set in options is passed along to Joi as options while validating. (See Joi documentation.)


This middleware automatically adds express’ request and response objects to the Joi context as req and res, allowing for example: Joi.ref('$req.params.orgId').


Validators can be:

  • empty or omitted: no validation is done in this case
  • a Joi schema: used to validate
  • a function that accepts Joi and returns a Joi schema


When validation fails, a ValidationError will be thrown. The main error’s path property will simply be the top level path that failed (params, query, body or headers.) For every Joi error, another ValidationError will be added as cause to the main error, these sub-errors will have the full path array to the exact validation problem as well as other properties returned in the Joi error’s details.