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.

Validating field length

Validating field length

Now we are going to see how to validate field length with NestJS. So let's move to the example and understand how to implement the validation. In the AuthDTO class, let us set the length of the name field. And to do so, I'll give the length validator. This validator can take up to two arguments, minimum length and maximum. Let me set the minimum length to 3. Now if I set a single value only, then it will be considered as min length value and the max length can be of any length if only I set a single value. To set the max length, I'll give a comma and give 20 as max length. So now the name field has to be between the given range. I'll remove the is empty validator from the name field because we won't be needing it here. Now let's check the validation in postman. I'll give the request body. Let's try setting the name value as a single character first, then the e-mail and password. I'll make the request and we get the validation message for the name field that is name must be longer than…

Contents