Joi is a schema description and validation library, popular for defining an object's exact shape once and validating request data against it in a single call. This lesson covers integrating Joi into an Express app.
Defining a Joi Schema
Instead of chaining individual field validators like express-validator, Joi describes an entire object's shape as one schema, then validates a value against it in one call, returning either the validated (and optionally sanitized) value, or a detailed error.
This schema-first approach is popular because the same schema definition doubles as clear documentation of exactly what a valid request body looks like.
Forgetting { abortEarly: false } when you want every validation error returned at once.
Not using the sanitized value returned by .validate(), and instead re-reading the raw, unvalidated req.body.
Defining the same schema shape inline in multiple route files instead of exporting shared schemas.
Using Joi for both request validation and response shaping, mixing two different concerns.
Key Takeaways
Joi describes an object's entire shape as one schema, validated in a single call.
.validate() returns both an error and a sanitized/defaulted value.
A small reusable middleware factory keeps the wiring consistent across routes.
The same pattern validates req.body, req.params, or req.query by targeting different properties.
Pro Tip
Always assign req.body = value after a successful Joi validation, this ensures your route handler works with the sanitized and defaulted data, not the original raw input.
You now know two solid approaches to validation. Next, learn how to centralize error handling in the Error Handling lesson.