Skip to content

Commit c87d27f

Browse files
committed
fix: decorating inherited property #633
1 parent e0040c4 commit c87d27f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/metadata/MetadataStorage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ export class MetadataStorage {
119119
// filter out duplicate metadatas, prefer original metadatas instead of inherited metadatas
120120
const uniqueInheritedMetadatas = inheritedMetadatas.filter(inheritedMetadata => {
121121
return !originalMetadatas.find(originalMetadata => {
122+
// expect validators to be duplicate if they point to the same validator function
122123
return (
123124
originalMetadata.propertyName === inheritedMetadata.propertyName &&
124-
originalMetadata.type === inheritedMetadata.type
125+
originalMetadata.constraintCls === inheritedMetadata.constraintCls
125126
);
126127
});
127128
});

test/functional/inherited-validation.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,31 @@ describe('inherited validation', () => {
3434
expect(errors[1].value).toEqual('helo world');
3535
});
3636
});
37+
38+
it('should use validators from parent and child classes', () => {
39+
expect.assertions(5);
40+
41+
class MyClass {
42+
@Contains('hello')
43+
title: string;
44+
}
45+
46+
class MySubClass extends MyClass {
47+
@MinLength(5)
48+
title: string;
49+
}
50+
51+
const model = new MySubClass();
52+
model.title = 'helo';
53+
return validator.validate(model).then(errors => {
54+
expect(errors.length).toEqual(1);
55+
expect(errors[0].target).toEqual(model);
56+
expect(errors[0].property).toEqual('title');
57+
expect(errors[0].constraints).toEqual({
58+
minLength: 'title must be longer than or equal to 5 characters',
59+
contains: 'title must contain a hello string' ,
60+
});
61+
expect(errors[0].value).toEqual('helo');
62+
});
63+
});
3764
});

0 commit comments

Comments
 (0)