API Reference

express-validator

Express middleware for validating and sanitizing request bodies, params, queries, and headers with a structured error result and typed matched data.

Official Documentation ↗
CompatibilityThis documentation covers version express-validator 7.x with Express 4/5; sanitizer behavior depends on the selected chain. Check the official docs for the latest API surface.

Installation

npmnpm install express-validator
pnpmpnpm add express-validator
yarnyarn add express-validator

Methods

express-validator wraps validator.js in Express middleware. Each chain validates or sanitizes one location, and validationResult collects the outcome. The library does not make input safe for every context; it validates shape and format, while output encoding and authorization remain separate concerns.

Required means explicit

Many validators skip undefined. Add .exists() or .notEmpty() when a field is required, and test a missing field, an empty string, and a wrong type. Coerce query strings deliberately; every query parameter arrives as a string.

Return stable errors

Use a final middleware to check validationResult, map errors to a stable JSON shape, and return 422. Use matchedData to pass only validated fields to the service. Keep authorization and resource ownership checks outside the validator; they depend on the current user, not only the input shape.

Security notes

Quoted from the official documentation (https://express-validator.github.io/docs/); this is not a complete threat model.

  • It allows you to combine them in many ways so that you can validate and sanitize your express requests, and offers tools to determine if the request is valid or not, which data was matched according to your validators, and so on. (https://express-validator.github.io/docs/)

FAQ

Is express-validator a sanitizer for HTML?
It offers some sanitizers, but it is not a complete XSS defense. Escape output in the rendering context and apply a Content-Security-Policy; validation is not enough.
Why does validation pass for missing fields?
Validators other than exists and notEmpty may skip undefined values. Add .exists() or .notEmpty() explicitly for required fields.
Can I use the raw req.body after validation?
Use matchedData to select only validated fields. Passing the raw body can include unknown keys and bypass the schema you intended to enforce.