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 empty fields

Validating empty fields

Checking if the field is not empty is a fundamental part of making sure that the information we receive is complete and accurate. Just like when we fill out a form and leave a required field blank, our programs need to ensure that all necessary pieces of data are provided. In NestJS, we can validate empty fields by applying the is empty and is not empty decorators provided by the class validator package. Let's see an example. Currently, we just have the is e-mail validator applied on the e-mail field, though this validator will also check for an empty e-mail, but it won't display an accurate message. Let's check it out. In the postman, I have already defined the data where the e-mail field is empty. Now when we make the request, we get the validation error message, e-mail must be an e-mail. What if we want to display an accurate message for the e-mail field when it is left empty? For that, we give the is not empty decorator on the e-mail field like this. This validator checks that the…

Contents