Complete the code to import the necessary decorator for custom validation.
import { [1] } from 'class-validator';
The ValidatorConstraint decorator is used to define a custom validation constraint class in NestJS.
Complete the code to implement the ValidatorConstraintInterface.
export class CustomTextLengthValidator implements [1] { validate(text: string) { return text.length > 5; } }
The class must implement ValidatorConstraintInterface to define the validate method for custom validation logic.
Fix the error in the custom decorator function to apply the validation correctly.
export function IsCustomText(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
[1](CustomTextLengthValidator, {
message: 'Text is too short',
...validationOptions,
})(object, propertyName);
};
}The Validate decorator applies the custom validator class to the property inside the custom decorator function.
Fill both blanks to complete the custom validator class and decorator usage.
import { [1], ValidatorConstraintInterface, ValidationArguments } from 'class-validator'; @ValidatorConstraint({ name: 'customText', [2]: true }) export class CustomTextLengthValidator implements ValidatorConstraintInterface { validate(text: string, args: ValidationArguments) { return typeof text === 'string' && text.length > 5; } }
The ValidatorConstraint decorator marks the class as a validator. The async option set to true allows asynchronous validation if needed.
Fill all three blanks to create a custom decorator that uses the validator and accepts validation options.
import { Validate, ValidationOptions } from 'class-validator'; export function IsCustomText([1]?: ValidationOptions) { return function (object: Object, propertyName: string) { [2](CustomTextLengthValidator, { message: 'Text must be longer than 5 characters', ...[3], })(object, propertyName); }; }
The function parameter is named validationOptions. Inside, the Validate decorator is called with the validator class and options spread from validationOptions.