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.

Custom validation messages

Custom validation messages

We can also specify a custom validation message for various validator decorators. For example, let's try to give custom messages for length validations. First for the min length validator. To define the custom validation message, we have to give an optional object inside the validator argument and the message property. The message property is used for specifying a custom validation message for the respective validator. Let's give a message. Password is too short. Minimal length required is of eight characters. So now when the validation fails, this message will be returned, indicating to the user that the password is too short. Let's check it inside the postman. I have already defined the request body and the password is of six characters length. Let's make a request and the custom validation message is displayed. There are few special tokens that can be used with messages. Like here, if we want to display the dynamic constraint value, rather than specifying the static digit in the…

Contents