From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Built-in pipes (ValidationPipe)

Built-in pipes (ValidationPipe)

In NestJS, the built-in validation pipe automatically performs validation on incoming request payloads coming from the query parameters, body parameters, and route parameters. It offers various validation rules as decorators, such as the isString, isNumber, isEmail, etc. to ensure that the data received by the application meets the specified validation criteria before it reaches the route handler. Here is how we can use the validation pipe. First, let's create a folder, naming it as Auth, creating a file, AuthDTO.ts. In this file, let's create an auth class containing the e-mail and password fields. I'll give e-mail of type string and password of type string. Now let's create an auth controller. I'll give the command nest generate-controller auth hyphen hyphen no spec and the auth controller is created. Let me open it. I'm setting a post handler inside controller, naming the route as register, creating a method register user, which will take a request body, and I'll store the data…

Contents