How do you validate input in an Express API?
Model answer
Never trust req.body, req.query or req.params. Validate types, required fields, lengths and formats with a schema library such as Zod, Joi or express-validator, and return 400 with clear messages. Also validate in the database layer (Mongoose schema rules or constraints), and sanitise values used in queries.
Learn this topic
Build a REST API: Create, Read, Update and Delete Tasks in the course Node.js and Express: Build a Back End API from Scratch.
Sign in to add your own answer.