Skip to content

Commit

Permalink
fix: transform type after validate (#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Oct 4, 2020
1 parent daa28ff commit 2b30ba7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/decorator/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require('path');

module.exports = {
preset: 'ts-jest',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
};
2 changes: 2 additions & 0 deletions packages/decorator/src/annotation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function Validate(isTransform = true) {
const result = schema.validate(args[i]);
if (result.error) {
throw result.error;
} else {
args[i] = result.value;
}
// passed
if (isTransform) {
Expand Down
19 changes: 19 additions & 0 deletions packages/decorator/test/annotation/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,23 @@ describe('/test/annotation/check.test.ts', () => {
new Hello().school(1, user);
}).toThrow(Error);
});

it('should transform string to number', function () {
class UserNewDTO {
@Rule(RuleType.number().required())
id: number;
}

class Hello {
@Validate()
school(user: UserNewDTO) {
return user;
}
}

const data = new Hello().school({
id: '555'
} as any)
expect(typeof data.id).toEqual('number');
});
});

0 comments on commit 2b30ba7

Please sign in to comment.