📖 Scenario: You are building a NestJS backend for a user registration system. You want to ensure that the username field follows a custom rule: it must start with the letter 'A'. To do this, you will create a custom validation decorator.
🎯 Goal: Create a custom validation decorator called StartsWithA that checks if a string starts with the letter 'A'. Use this decorator in a DTO class to validate the username property.
📋 What You'll Learn
Create a custom validation decorator named StartsWithA using class-validator.
Create a DTO class called CreateUserDto with a username property.
Apply the StartsWithA decorator to the username property.
Use the IsString decorator from class-validator on username.
💡 Why This Matters
🌍 Real World
Custom validation decorators help enforce business rules on data inputs in backend APIs, improving data quality and user experience.
💼 Career
Backend developers often create custom validators to handle unique validation needs beyond built-in rules, making this skill valuable for NestJS and Node.js jobs.
Progress0 / 4 steps
1
Create the DTO class with username property
Create a class called CreateUserDto with a public property username of type string.
NestJS
Hint
Define a class with a property named username of type string.
2
Add IsString decorator to username
Import IsString from class-validator and add the @IsString() decorator above the username property in CreateUserDto.
NestJS
Hint
Use @IsString() decorator from class-validator on the username property.
3
Create the custom StartsWithA decorator
Import registerDecorator, ValidationOptions, and ValidationArguments from class-validator. Create a function called StartsWithA that takes an optional ValidationOptions parameter. Inside, use registerDecorator to define a validator that checks if the value starts with the letter 'A'.
NestJS
Hint
Create a function that uses registerDecorator to check if the string starts with 'A'.
4
Apply the StartsWithA decorator to username
Add the @StartsWithA() decorator above the username property in CreateUserDto to enforce the custom validation rule.
NestJS
Hint
Use the custom decorator @StartsWithA() on the username property.
Practice
(1/5)
1. What is the main purpose of creating a custom validation decorator in NestJS?
easy
A. To create new routes in the application
B. To automatically generate database schemas
C. To handle HTTP requests and responses
D. To define your own rules for validating data inputs
Solution
Step 1: Understand validation decorators
Validation decorators in NestJS are used to check if data meets certain rules before processing.
Step 2: Purpose of custom validation decorators
Custom validation decorators let you create your own specific rules that are not covered by built-in validators.
Final Answer:
To define your own rules for validating data inputs -> Option D
Quick Check:
Custom validation decorators = custom data rules [OK]
Hint: Custom decorators create your own data validation rules [OK]
Common Mistakes:
Confusing validation decorators with routing or database features
A. The validate method should return a Promise because async is true
B. The registerDecorator call is missing the validate method
C. The decorator function should not accept validationOptions
D. The class should not have @ValidatorConstraint decorator
Solution
Step 1: Check async flag and validate method
The @ValidatorConstraint decorator has async: true, so validate must return a Promise<boolean>.
Step 2: Validate method returns boolean, not Promise
Currently, validate returns a boolean directly, which conflicts with async: true.
Final Answer:
The validate method should return a Promise because async is true -> Option A
Quick Check:
async: true requires Promise return [OK]
Hint: If async: true, validate must return Promise<boolean> [OK]
Common Mistakes:
Returning boolean instead of Promise when async is true
Omitting @ValidatorConstraint decorator
Misusing registerDecorator parameters
5. You want to create a custom validation decorator that checks if a string property contains at least one uppercase letter and one digit. Which approach correctly combines the concepts to implement this in NestJS?
hard
A. Use built-in decorators @IsUppercase and @IsNumber together on the property
B. Create a class with @ValidatorConstraint, implement validate() using regex, then use registerDecorator in a function to apply it
C. Create a service with @Injectable and call it inside the DTO without decorators
D. Use only registerDecorator with a simple function without a class or @ValidatorConstraint
Solution
Step 1: Understand custom validation structure
Custom validators require a class decorated with @ValidatorConstraint implementing validate() method.
Step 2: Use registerDecorator to link the class to a decorator function
This function applies the validator to the property in the DTO.
Step 3: Implement validate() with regex checking uppercase and digit presence
This ensures the string meets both conditions in one check.
Final Answer:
Create a class with @ValidatorConstraint, implement validate() using regex, then use registerDecorator in a function to apply it -> Option B
Quick Check:
Custom validator class + registerDecorator function = correct approach [OK]
Hint: Combine @ValidatorConstraint class with registerDecorator function [OK]
Common Mistakes:
Skipping the validator class and using only registerDecorator
Trying to use services instead of decorators for validation