Skip to content

Commit

Permalink
Add custom decorator
Browse files Browse the repository at this point in the history
@match decorator can be used to validate two same property
  • Loading branch information
sayeed205 committed May 31, 2023
1 parent d75076b commit 7bd59a5
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 316 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,5 @@ lerna-debug.log*
# API Clients - Thunder Client
thunder-tests
thunder-tests/*.json
thunder-tests/
/thunder-tests
10 changes: 10 additions & 0 deletions src/auth/dto/signup.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IsString,
IsStrongPassword,
} from 'class-validator';
import { Match } from 'src/common/decorators';

export class SignupDto {
@IsString()
Expand All @@ -30,4 +31,13 @@ export class SignupDto {
example: 'Abcd@1234',
})
password: string;

@IsNotEmpty()
@IsStrongPassword()
@Match(SignupDto, o => o.password)
@ApiProperty({
description: 'Confirm password',
example: 'Abcd@1234',
})
confirmPassword: string;
}
1 change: 1 addition & 0 deletions src/common/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './GetUser.decorator';
export * from './IsValidMongoId.decorator';
export * from './apiPaginatedResponse.decorator';
export * from './match.decorator';
38 changes: 38 additions & 0 deletions src/common/decorators/match.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ClassConstructor } from 'class-transformer';
import {
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
registerDecorator,
} from 'class-validator';

export const Match = <T>(
type: ClassConstructor<T>,
property: (o: T) => any,
validationOptions?: ValidationOptions,
) => {
return (object: any, propertyName: string) => {
registerDecorator({
target: object.constructor,
propertyName,
options: validationOptions,
constraints: [property],
validator: MatchConstraint,
});
};
};

@ValidatorConstraint({ name: 'Match' })
export class MatchConstraint implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const [fn] = args.constraints;
return fn(args.object) === value;
}

defaultMessage(args: ValidationArguments) {
const [constraintProperty]: (() => any)[] = args.constraints;
const prop = constraintProperty.toString().split('. ')[1];
return `${prop} and ${args.property} does not match`;
}
}
1 change: 0 additions & 1 deletion thunder-tests/thunderActivity.json

This file was deleted.

58 changes: 0 additions & 58 deletions thunder-tests/thunderCollection.json

This file was deleted.

34 changes: 0 additions & 34 deletions thunder-tests/thunderEnvironment.json

This file was deleted.

223 changes: 0 additions & 223 deletions thunder-tests/thunderclient.json

This file was deleted.

0 comments on commit 7bd59a5

Please sign in to comment.