-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Harry Chen <czy88840616@gmail.com>
- Loading branch information
1 parent
fd65da7
commit 92735b0
Showing
6 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'reflect-metadata'; | ||
import * as joi from 'joi'; | ||
export function Check(failValue?: any) { | ||
return function (target, propertyKey: string | symbol, descriptor: PropertyDescriptor) { | ||
const origin = descriptor.value; | ||
descriptor.value = function (...args: any[]) { | ||
const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey); | ||
for (let i = 0; i < paramTypes.length; i++) { | ||
const item = paramTypes[i]; | ||
const rules = Reflect.getMetadata('rules', item.prototype); | ||
if (rules) { | ||
const result = joi.validate(args[i], rules); | ||
if (result.error) { | ||
return failValue ? failValue : result; | ||
} | ||
} | ||
} | ||
const result = origin.call(this, ...arguments); | ||
return result; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as joi from 'joi'; | ||
|
||
export function Rule(rule) { | ||
return function (target: any, propertyKey: string) { | ||
if (!rule.isJoi) { | ||
rule = Reflect.getMetadata('rules', rule.prototype); | ||
if (Reflect.getMetadata('design:type', target, propertyKey).name === 'Array') { | ||
rule = joi.array().items(rule).required(); | ||
} else { | ||
rule = joi.object(rule).required(); | ||
} | ||
} | ||
let rules = Reflect.getMetadata('rules', target); | ||
if (!rules) { | ||
rules = {}; | ||
} | ||
rules[propertyKey] = rule; | ||
Reflect.defineMetadata('rules', rules, target); | ||
}; | ||
} | ||
|
||
export { joi as RuleType }; |
130 changes: 130 additions & 0 deletions
130
packages/midway-decorator/test/annotation/check.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { Check, Rule, RuleType } from '../../src'; | ||
import * as assert from 'assert'; | ||
describe('/test/annotation/check.test.ts', () => { | ||
it('check with check', () => { | ||
class UserDTO { | ||
@Rule(RuleType.number().max(10)) | ||
age: number; | ||
} | ||
|
||
class Hello { | ||
@Check() | ||
school(a, data: UserDTO) { | ||
return data; | ||
} | ||
} | ||
const user = { | ||
age: 8 | ||
}; | ||
const result = new Hello().school(1, user); | ||
assert.deepEqual(result, user); | ||
}); | ||
|
||
it('check with no check', () => { | ||
class UserDTO { | ||
@Rule(RuleType.number().max(10)) | ||
age: number; | ||
} | ||
|
||
class Hello { | ||
school(a, data: UserDTO) { | ||
return data; | ||
} | ||
} | ||
const user = { | ||
age: 18 | ||
}; | ||
const result = new Hello().school(1, user); | ||
assert.deepEqual(result, user); | ||
}); | ||
|
||
it('check with check when vo have two level', () => { | ||
class WorldDTO { | ||
@Rule(RuleType.number().max(20)) | ||
age: number; | ||
} | ||
|
||
class UserDTO { | ||
@Rule(RuleType.number().max(10)) | ||
age: number; | ||
|
||
@Rule(WorldDTO) | ||
world: WorldDTO; | ||
} | ||
|
||
class Hello { | ||
@Check() | ||
school(a, data: UserDTO) { | ||
return data; | ||
} | ||
} | ||
const user = { | ||
age: 10, | ||
world: { | ||
age: 18 | ||
} | ||
}; | ||
const result = new Hello().school(1, user); | ||
assert.deepEqual(result, user); | ||
}); | ||
|
||
it('check with check when vo have two level not equal', () => { | ||
class WorldDTO { | ||
@Rule(RuleType.number().max(20)) | ||
age: number; | ||
} | ||
|
||
class UserDTO { | ||
@Rule(RuleType.number().max(10)) | ||
age: number; | ||
|
||
@Rule(WorldDTO) | ||
world: WorldDTO; | ||
} | ||
|
||
class Hello { | ||
@Check() | ||
school(a, data: UserDTO) { | ||
return data; | ||
} | ||
} | ||
const user = { | ||
age: 10, | ||
world: { | ||
age: 22 | ||
} | ||
}; | ||
const result = new Hello().school(1, user); | ||
assert.notDeepEqual(result, user); | ||
}); | ||
|
||
it('check with check when two level and array and not equal', () => { | ||
class WorldDTO { | ||
@Rule(RuleType.number().max(20)) | ||
age: number; | ||
} | ||
|
||
class UserDTO { | ||
@Rule(RuleType.number().max(10)) | ||
age: number; | ||
|
||
@Rule(WorldDTO) | ||
worlds: WorldDTO[]; | ||
} | ||
|
||
class Hello { | ||
@Check() | ||
school(a, data: UserDTO) { | ||
return data; | ||
} | ||
} | ||
const user = { | ||
age: 10, | ||
worlds: [{ | ||
age: 22 | ||
}] | ||
}; | ||
const result = new Hello().school(1, user); | ||
assert.notDeepEqual(result, user); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters