Skip to content

Commit 1cec4bc

Browse files
committed
Fix ValidateBy's handling of custom constraint classes
1 parent 419e270 commit 1cec4bc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/common/validators/validateBy.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { FnLike } from '@seedcompany/common';
1+
import { Type } from '@nestjs/common';
22
import {
33
registerDecorator,
44
ValidationOptions,
55
ValidatorConstraintInterface,
66
} from 'class-validator';
7+
import { MergeExclusive } from 'type-fest';
78

8-
export interface ValidateByOptions {
9-
name: string;
10-
constraints?: any[];
11-
validator: ValidatorConstraintInterface | FnLike;
12-
async?: boolean;
13-
}
9+
export type ValidateByOptions =
10+
| MergeExclusive<
11+
{
12+
validator: ValidatorConstraintInterface;
13+
name: string;
14+
async?: boolean;
15+
constraints?: any[];
16+
},
17+
{
18+
validator: Type<ValidatorConstraintInterface>;
19+
constraints?: any[];
20+
}
21+
>
22+
| Type<ValidatorConstraintInterface>;
1423

1524
export const ValidateBy =
1625
(
@@ -19,13 +28,11 @@ export const ValidateBy =
1928
): PropertyDecorator =>
2029
(object: Record<string, any>, propertyName: string | symbol) => {
2130
registerDecorator({
22-
name: options.name,
2331
target: object.constructor,
2432
propertyName: propertyName as string,
2533
options: validationOptions,
26-
constraints: options.constraints,
27-
validator: options.validator,
28-
async: options.async,
34+
...options,
35+
validator: typeof options === 'function' ? options : options.validator,
2936
});
3037
};
3138

0 commit comments

Comments
 (0)