Modularizing our validation logic

Next, create a new file at src/validators/users/create.js and copy the validation blocks from our request handlers into the file, wrapping it inside its own function and exporting that function:

function validate (req) {  if (    !Object.prototype.hasOwnProperty.call(req.body, 'email')    || !Object.prototype.hasOwnProperty.call(req.body, 'password')  ) {    res.status(400);    res.set('Content-Type', 'application/json');    return res.json({ message: 'Payload must contain at least the email and password fields' });  }  ...}export default validate;

Next, import the ValidationError class from src/validators/errors/validation-error.js. Then, instead of modifying the res object (which is not in scope), return instances of 

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.