Member-only story

Validate Incoming Requests in Node.js like a Pro

Using TypeScript classes and class-validator

Mohammad Faisal
JavaScript in Plain English
4 min readJan 5, 2023

Photo by Windows on Unsplash

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");
}
//…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Responses (10)

--

--

--

Recommended from Medium

Lists

See more recommendations