This tutorial covers setting up input validation with class-validator, managing environment variables using @nestjs/config, and implementing structured logging with NestJS Pino. The video demonstrates installing dependencies, modifying the application configuration, and testing API requests in Postman to ensure secure and debuggable backend services.
Length 14:30
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nestjs #validation #pipes #configuration #logging #pino #backend #typescript #api #development
0:00 So to implement validation on our NestJS backend, we're gonna use 0:04 Class Validator and Class Transformer libraries, which work 0:08 great with NestJS out of the box. So let's go ahead and open up 0:11 our terminal in our backend and make sure we install 0:16 class-validator and 0:18 class-transformer. 0:23 So now that we have these dependencies, we need to actually integrate them into 0:26 our 0:26 NestJS backend, and we do that through the usage of pipes. 0:31 Now, pipes are essentially just a piece of code 0:33 that we send every 0:34 request through and transform it, or in this case, validate it in 0:38 some sort of way. So that's what we'll go ahead and do. 0:41 To apply this pipe, we'll open up our main TS file, and 0:45 before we listen on our HTTP server, we're gonna call 0:49 app.useGlobalPipes. So this is gonna go ahead and 0:53 apply this pipe to every single HTTP route 0:57 in our application. In this case, we want this new 1:01 validation pipe from NestJS Common to occur. 1:04 So go ahead and pass in this new validation pipe. 1:08 So we can be even more strict here and actually supply an 1:12 options object and supply a whitelist property. 1:16 So in addition to actually validating our DTOs, setting a 1:19 whitelist is going to ensure that we don't allow any extra 1:23 properties into the incoming request body object that are 1:27 not defined explicitly on the DTO. 1:30 So this makes our payloads even more secure, so I'll set this to true 1:34 as well. 1:35 Now, in addition to setting up our validation 1:37 pipe, I want to go ahead 1:39 and set up configuration on our NestJS backend. 1:43 Now, configuration in NestJS works by reading 1:46 in environment 1:47 variables through a .env, and this is gonna use the popular 1:51 dotenv library. So NestJS offers a config 1:55 module out of the box that will take care of reading in the
The rest of the transcript is for members.
Sign in