Member-only story
Validate Incoming Requests in Node.js like a Pro
Using TypeScript classes and class-validator

Security is a great concern for any backend application. And one of the most important things is not letting bad things get into the application in the first place.
Today we will learn how to validate incoming requests in an express application.
Before we begin…
I have created a professional boilerplate with ExpressJS and Typescript. This article is a part of this series. You can find all the articles below.
* Creating a ExpressJS + Typescript Boilerplate
* How to setup Linter and Formatter for NodeJS
* How to handle multiple environments in NodeJS
* Error Handling in NodeJS
* Request Validation in NodeJS
* Using Docker Professionally with NodeJS
* Using Docker for Local Development in NodeJS
* Logging in NodeJS
* Kubernetes with NodeJS
* Security in NodeJS
Let’s begin!
Manual way
There are manual ways to do it by checking every body
parameter and ensuring that they are valid.
That would look something like the following.
const { body } = req;
if (!body.name && body.name.length === 0) {
throw new Error(400, "Name is required");
}
//…