A custom validation decorator in NestJS is a user-defined function that adds specific validation rules to class properties, allowing you to check data beyond built-in validators.
Click to reveal answer
beginner
Which package is commonly used to create custom validation decorators in NestJS?
The 'class-validator' package is commonly used to create custom validation decorators in NestJS, enabling easy integration with validation pipes.
Click to reveal answer
intermediate
What are the two main parts needed to create a custom validation decorator in NestJS?
You need a validator constraint class implementing the validation logic and a decorator function that applies this constraint to a property.
Click to reveal answer
intermediate
How do you ensure your custom validator works with NestJS validation pipe?
By using the 'ValidatorConstraint' and 'Validate' decorators from 'class-validator' and applying the custom decorator on DTO properties, the validation pipe automatically runs your custom logic.
Click to reveal answer
beginner
Why might you create a custom validation decorator instead of using built-in ones?
You create custom validation decorators to handle specific rules unique to your app, like checking a username format or verifying a business rule that built-in validators don't cover.
Click to reveal answer
Which decorator marks a class as a custom validator constraint in NestJS?
A@IsString
B@ValidatorConstraint
C@Validate
D@IsNotEmpty
✗ Incorrect
The @ValidatorConstraint decorator marks a class as a custom validator constraint.
What does the @Validate decorator do in a custom validation setup?
AMarks a property as optional
BDefines a new validation rule
CRuns validation on the whole class
DApplies a custom validator to a property
✗ Incorrect
The @Validate decorator applies a custom validator class to a specific property.
Which package provides the tools to create custom validation decorators in NestJS?
Aclass-validator
Bexpress-validator
Cjoi
Dnestjs-validation
✗ Incorrect
The class-validator package is used in NestJS for validation decorators.
What must a custom validator class implement to work properly?
AValidatorConstraintInterface
BValidationPipe
CPipeTransform
DOnModuleInit
✗ Incorrect
The custom validator class must implement ValidatorConstraintInterface.
Why use custom validation decorators in NestJS?
ATo replace the validation pipe
BTo speed up the app
CTo validate data with rules not covered by built-in validators
DTo style the UI
✗ Incorrect
Custom validation decorators allow validation of specific rules not covered by built-in validators.
Explain how to create a custom validation decorator in NestJS step-by-step.
Think about the class-validator decorators and how they connect.
You got /5 concepts.
Describe why and when you would use a custom validation decorator in a NestJS project.
Consider unique app requirements and validation needs.
You got /4 concepts.
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