1import {
2 MinLength,
3 MaxLength,
4 IsNotEmpty,
5 ValidateNested,
6 IsDefined,
7 IsNotEmptyObject,
8 IsObject,
9 IsString,
10} from 'class-validator';
11import { Type } from 'class-transformer';
12
13class MultiLanguageDTO {
14 @IsString()
15 @IsNotEmpty()
16 @MinLength(4)
17 @MaxLength(40)
18 en: string;
19
20 @IsString()
21 @IsNotEmpty()
22 @MinLength(4)
23 @MaxLength(40)
24 ar: string;
25}
26
27export class VideoDTO {
28 @IsDefined()
29 @IsNotEmptyObject()
30 @IsObject()
31 @ValidateNested()
32 @Type(() => MultiLanguageDTO)
33 name!: MultiLanguageDTO;
34}
1useContainer(app.select(AppModule), { fallbackOnErrors: true });
2
3@ValidatorConstraint({ name: 'UserExists', async: true })
4@Injectable()
5export class UserExistsRule implements ValidatorConstraintInterface {
6 constructor(private usersRepository: UsersRepository) {}
7
8 async validate(value: number) {
9 try {
10 await this.usersRepository.getOneOrFail(value);
11 } catch (e) {
12 return false;
13 }
14
15 return true;
16 }
17
18 defaultMessage(args: ValidationArguments) {
19 return `User doesn't exist`;
20 }
21}
22
23export function UserExists(validationOptions?: ValidationOptions) {
24 return function (object: any, propertyName: string) {
25 registerDecorator({
26 name: 'UserExists',
27 target: object.constructor,
28 propertyName: propertyName,
29 options: validationOptions,
30 validator: UserExistsRule,
31 });
32 };
33}
34
35