A tiny module to provide Joi validation middleware within a Koa server.
npm install koa-joi-validate
const validate =
To use the module, call
validate with an object containing Joi validation objects for the request headers, URL query, URL path params, and post body.
The following basic example will verify that any request to the server contains a properly formatted request ID header and user ID url query parameter.
const Koa =const joi =const validate =const app =appapp;app
Here is another basic example, mounting a validator on a specific route using koa-router.
const router =const loginValidator =router
For more examples of the (very powerful) validation capabilities of Joi, view the official documentation - https://github.com/hapijs/joi
If the validation fails, an HTTP 400 response will be returned to the client, along with a short human-readable error message explaining why the request was rejected.