Are you overusing express-validator’s custom validator?

1 min read

express-validator is a popular express middleware library used to validate and sanitize user input. It offers a set of express middlewares that warp over a vast collection of validators and sanitizers provided by validator.js. It also provides a way to define custom validators to handle specific validation requirements that are not covered by validator.js.

While custom validators should be reserved for special cases that involve complex validation rules, it’s very common to see them being used in places where a built-in/standard validator will suffice. This happens mainly because developers don’t know if a built-in/standard validator already exists for their use case or they just don’t know how to validate a specific input from its enclosing data structure (like a deeply nested object or an array). In this post, I will cover some built-in validators and sanitizers you never knew existed and how you can validate array inputs.

(more…)