valid-body-joi
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

valid-body-joi

Express middleware for validating the request body with Joi.

Install

npm install valid-body-joi

Usage

Import the middleware and Joi as below:

const validBody = require('valid-body-joi');
const Joi = require('joi');

Define a schema:

const loginSchema = Joi.object({
  email: Joi.string().trim().lowercase().email().required(),
  password: Joi.string().trim().required(),
});

Install the middleware on a route:

app.post('/api/auth/login', validBody(loginSchema), (req, res, next) => {
  const login = req.body;
  // ...
});

Implementation

  1. The middleware validates the request body against the provided schema.

  2. If the request body is valid, the body is replaced by the sanitized data, and control passes to the next middleware.

  3. Otherwise, a 400 response is sent.

Package Sidebar

Install

npm i valid-body-joi

Weekly Downloads

1

Version

2.0.4

License

MIT

Unpacked Size

4.5 kB

Total Files

6

Last publish

Collaborators

  • merlin4