Whole Body Validation
Sometimes you need to validate a request whose body is a string, an array, or even a number!
That's why you can omit the field to validate, and check req.body
directly:
const bodyParser = require('body-parser');
const express = require('express');
const { body } = require('express-validator');
const app = express();
// Will handle text/plain requests
app.use(bodyParser.text());
app.post('/recover-password', body().isEmail(), (req, res) => {
// Assume the validity of the request was already checked
User.recoverPassword(req.body).then(() => {
res.send('Password recovered!');
});
});
This setup should be able to handle the following request:
POST /recover-password HTTP/1.1
Host: localhost:3000
Content-Type: text/plain
my@email.com