Nest JS Tutorial #2: HTTP Request & Data Validation
Tags: api, javascript, typescript, nestjs
Published: 2022-12-02 04:49:03

---
title: Nest JS Tutorial #2: HTTP Request & Data Validation
published: true
description: Learn what is HTTP Request and how to validate the request data in Nest.JS
tags: #api #javascript #typescript #nestjs
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdi5jr3kr0y5wzgesg4h.png
series: Nest Tutorial
# Use a ratio of 100:42 for best results.
# published_at: 2022-11-28 18:52 +0000
---

Hi There! 👋

In this post, you will learn how to validate request data in [Nest JS](https://nestjs.com/)

If you don't know what is Nest JS, If you don't know what is Nest JS, Check out [my previous post](https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a)

> Originally Posted On - https://www.nandhakumar.io/post/nest-js-tutorial-2-http-request-data-validation

## Prerequisite
- [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)
- [Typescript](https://www.typescriptlang.org/) (Optional)
- Basics Of OOPS
- [Postman](https://www.postman.com/)  (For API Testing)
- [Code Editor](https://code.visualstudio.com/)
- [Node.JS](https://nodejs.org/en/)


# What is an HTTP Request?

HTTP Request is sent from the client machine(web browser or any other client application) to access or get information from the server.

These Requests are sent with the help of URLs like this 👇

```
https://yourapp.com/api/v1/users
```

### Three parts of HTTP Request

**Methods** - Defines the type of the request

**Headers** - Will have the information about the request and sender(client), you can add custom parameters as well. Usually, Authorization Cookies will be attached to Headers.

**Body** - Will have the requested data. For example, which registering as a new user, you have to pass user information as request data to store it in the server 


### HTTP Request Methods

| Methods |          Purpose          |
| ------- | :-----------------------: |
| GET     |        To get Data        |
| POST    |   To create or add data   |
| PATCH   | To update data partially  |
| PUT     | To update the entire data |
| DELETE  |      To delete data       |

That's a short brief on HTTP Requests.

**To know more about HTTP Request, [Click Here!](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)**

## Why Request Data Validation?

When the HTTP Request reaches the server, Getting the Body data directly without validating is not good. Some of you may get malicious data. So, you have to validate your Body data always. 

## Understanding Nest.JS Validation

Let's first understand how validation works behind the scene in Nest.JS

In Nest.JS, We have something called a validation pipe. Every time when a request body is passed through this pipe and validated. If there are any invalid data, it will throw an error.

Before moving further you need to understand what is DTO(Data Transfer Object)

DTO is a class where you will define the rules for validation. If you are not sure about this no worries, you'll understand this when implementing.

## Two Parts Of Validation Pipe

**Class Transformer**

Class Transformer helps to transform your request body into a class instance of DTO and then the instance will be passed to class validation

**Class Validator**

Class Validator will validate the request body based on the rules defined in the DTO and throws an error if there are any invalid data

***

Now let's start the implementation for the example scenario given below 👇

**Scenario**: Need to add a new user and the user should have email, age, name, and country( Country is optional).

As per the scenario, it's clear that we need to add data to the server

So, it is an HTTP POST Method and we need to pass user data in the request body.

Hope you would have already created a Nest.JS Project, If not follow my previous [tutorial](https://dev.to/nandhakumar/nest-js-part-1-creating-your-first-api-5f2a) to create one.

Let's do the implementation step by step,

### Step 1:

Install class validator and class transformer.

```bash
npm i class-validator class-transformer -s
```
###  Step 2:

Generate User Controller.

```bash
nest g controller user
```
> note: to execute nest commands you should have installed
> nest cli globally

### Step 3:

Generate User Module

```bash
nest g module user/user --flat
```
> Note: As we have already created a user folder in Step 2,
> To avoid creating additional folders --flag is used

### Step 4: 

Add User Controller to User Module

```ts
// user.module.ts
import { Module } from '@nestjs/common';
import { UserController } from './user.controller';
@Module({
  imports: [],
  providers: [],
  controllers: [UserController], // add user controller
})
export class UserModule {}

```

### Step 5:
Add User Module to App Module
```ts
// app.module.ts
import { Module } from '@nestjs/common';
import { UserModule } from './user/user.module';

@Module({
  imports: [UserModule], // add user module
  controllers: [],
  providers: [],
})
export class AppModule {}
```

### Step 6:

Create a dto folder under the user folder and create a file called user.dto.ts


![Project Structure - USER DTO](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bz9as4lek43gk0m2zoa.png)

### Step 7:
- Create UserDTO Class 
- Add the user properties to UserDTO as per the scenario

```ts
// user.dto.ts

export class UserDTO {

  email: string;

  age: string;

  name: number;

  country: number;
}

```

### Step 8:
Nest.JS uses [Typescript](https://www.typescriptlang.org/) decorators extensively.

> Note: [Decorators](https://www.typescriptlang.org/docs/handbook/decorators.html) are a simple function that helps to modify 
> the targeted value
> Decorators are defined like `@[decoratorname]()`

Coming back to the UserDTO,

**Class-Validator** has a list of validation functions which can be used in the form of a decorator

Now, Let's try to add those validation decorators to the class properties

```ts
import { IsEmail, IsNumber, IsOptional, IsString } from 'class-validator';

export class UserDTO {
  @IsEmail()
  email: string;

  @IsNumber()
  age: string;

  @IsString()
  name: number;

  // add @IsOptional to make the country property optional
  @IsOptional() 
  @IsString()
  country: number;
}

```

### Step 9:
- create a controller to add a user
- Add UserDTO as the type of the request body

```ts
import { Body, Controller, Post } from '@nestjs/common';

import { UserDTO } from './dto/user.dto';

@Controller('user')
export class UserController {
  @Post()
  async addUser(@Body() user: UserDTO) {
    return user;
  }
}
```
> Note: 
> I have added @Post Decorator to the function addUser as we > are dealing with Post HTTP Request
> Also, the @Body Decorator to get the request body

That's it we have added validation to the `addUser` request body

***

# Testing

Let's test the implementation now!

Start the server by executing

```bash
npm start:dev
```

Open the server URL `http://localhost:3000` in [Postman](https://www.postman.com/) or any API Testing Tool

**Test Case 1:**

Passing all the user properties with an invalid email

![Nest JS Request Body Validation - Test Case 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ark6dxnw3c3x4ajx5g3.png)

And the validation works!

**Test Case 2:**

Since the country property has been set to optional, Now let's try to send the request without the country property

![Nest JS Request Body Validation - Test Case 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wptt1ahr6qkkpnxl9pfe.png)


Now we got success response, even without the country.

Try testing with different scenarios by passing invalid data.

Congratulation! 👏

You have successfully validated the request body in Nest.JS

***

## Thanks For Reading!

Hope you have learned something new today 😊.

[Follow me](https://dev.to/nandhakumar) to get notified of all upcoming posts on this series.

Follow and connect with me on [Twitter](https://twitter.com/nandhakumar_io), [Instagram](https://www.instagram.com/nandhakumar.io/), [Email](mailto:contact@nandhakumar.io) and [LinkedIn](https://www.linkedin.com/in/nandhakumar-io/) for more interesting stuff like this.