Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 7x 7x 7x 6x 6x 6x 7x 7x | import {
IsString,
IsNotEmpty,
registerDecorator,
ValidationOptions,
ValidationArguments,
} from 'class-validator';
function Is3To5Words(validationOptions?: ValidationOptions) {
return function (target: object, propertyName: string) {
registerDecorator({
name: 'is3To5Words',
target: target.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate(value: any, _args: ValidationArguments) {
Iif (typeof value !== 'string') return false;
const words = value.trim().split(/\s+/).filter(Boolean);
return words.length >= 3 && words.length <= 5;
},
defaultMessage(_args: ValidationArguments) {
return 'Title must be between 3 and 5 words.';
},
},
});
};
}
export class GenerateTitleArgsDto {
@IsString({ message: 'title must be a string.' })
@IsNotEmpty({ message: 'title must not be empty.' })
@Is3To5Words({ message: 'Title must be between 3 and 5 words.' })
title: string;
}
|