Approved Starters
Express Frameworks
Express Validation
Package Install
npm i express-validator
Example -inline
index.js
//...const { validationResult, body } = require("express-validator")//...const app = express()//...//... validator exampleapp.post("/users", [body("email").isLength({ min: 1 }).withMessage("email must not be empty").isEmail().withMessage("must be a valid email address"),body("name").isLength({ min: 1 }).withMessage("name must not be empty"),]),async (req, res) => {const errors = validationResult(req)if (!errors.isEmpty) {return res.status(400).json(errors.mapped())}}
Example - abstracted
index.js
//...const { validationResult, body } = require("express-validator")//...const app = express()const userValidation = [body("email").isLength({ min: 1 }).withMessage("email must not be empty").isEmail().withMessage("must be a valid email address"),body("name").isLength({ min: 1 }).withMessage("name must not be empty"),]//... validation middlewareconst checkForErrors = (req, res, next) => {const errors = validationResult(req)if (!errors.isEmpty) {return res.status(400).json(errors.mapped())} else {next()}}const simpleValiationResult = validationResult.withDefaults({formatter: (err) => err.msg,})//...//... validator exampleapp.post("/users", userValidation, checkForErrors),async (req, res) => {try {return res.status(200).json({ message: "valid input" })} catch (err) {return res.status(500).json(err)}}