Skip to content

Commit

Permalink
feat: parameters validation (#451)
Browse files Browse the repository at this point in the history
Co-authored-by: Harry Chen <czy88840616@gmail.com>
  • Loading branch information
stone-jin and czy88840616 authored Jul 14, 2020
1 parent fd65da7 commit 92735b0
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/midway-decorator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
"dependencies": {
"camelcase": "^5.0.0",
"debug": "^4.1.1",
"joi": "^14.3.1",
"reflect-metadata": "^0.1.13"
},
"devDependencies": {
"@types/joi": "^14.3.4",
"midway-bin": "^2.0.15",
"mm": "^2.5.0"
},
Expand Down
22 changes: 22 additions & 0 deletions packages/midway-decorator/src/annotation/check.ts
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;
};
};
}
2 changes: 2 additions & 0 deletions packages/midway-decorator/src/annotation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export * from './priority';
export * from './provide';
export * from './schedule';
export * from './pipeline';
export * from './check';
export * from './rule';
22 changes: 22 additions & 0 deletions packages/midway-decorator/src/annotation/rule.ts
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 packages/midway-decorator/test/annotation/check.test.ts
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);
});
});
7 changes: 2 additions & 5 deletions packages/midway-decorator/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"stripInternal": true,
Expand All @@ -12,9 +13,5 @@
"outDir": "dist",
"sourceMap": true
},
"exclude": [
"dist",
"node_modules",
"test"
]
"exclude": ["dist", "node_modules", "test"]
}

0 comments on commit 92735b0

Please sign in to comment.