Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validator class extends #896

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions packages/decorator/src/annotation/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@ import {
attachClassMetadata,
getClassMetadata,
getPropertyType,
saveClassMetadata,
RULES_KEY,
} from '..';

export function Rule(rule) {
return function (target: any, propertyKey: string) {
if (!joi.isSchema(rule)) {
rule = getClassMetadata(RULES_KEY, rule);
if (getPropertyType(target, propertyKey)?.name === 'Array') {
rule = joi.array().items(rule).required();
} else {
rule = joi.object(rule).required();
return function (...args) {
if (args[1]) {
// 函数装饰器
const [target, propertyKey] = args;
if (!joi.isSchema(rule)) {
rule = getClassMetadata(RULES_KEY, rule);
if (getPropertyType(target, propertyKey)?.name === 'Array') {
rule = joi.array().items(rule).required();
} else {
rule = joi.object(rule).required();
}
}
}

attachClassMetadata(RULES_KEY, rule, target, propertyKey);
attachClassMetadata(RULES_KEY, rule, target, propertyKey);
} else {
//类的装饰器
const rules = getClassMetadata(RULES_KEY, rule);
if (rules) {
let currentRule = getClassMetadata(RULES_KEY, args[0]);
currentRule = currentRule ?? {};
Object.keys(rules).map(item => {
if (!currentRule[item]) {
currentRule[item] = rules[item];
}
});
saveClassMetadata(RULES_KEY, currentRule, args[0]);
}
}
};
}

Expand Down
46 changes: 43 additions & 3 deletions packages/decorator/test/annotation/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,59 @@ import { Validate, Rule, RuleType } from '../../src';
import * as assert from 'assert';
describe('/test/annotation/check.test.ts', () => {
it('check with check', () => {
class UserDTO {
class TO{

}

@Rule(TO)
class UserDTO extends TO{
@Rule(RuleType.number().max(10))
age: number;
}

@Rule(UserDTO)
class HelloDTO extends UserDTO{
}

class Hello {
@Validate()
school(a, data: UserDTO) {
school(a, data: HelloDTO) {
return data;
}
}
const user = {
age: 8,
age: 8
};
const result = new Hello().school(1, user);
assert.deepEqual(result, user);
});

it('check with check with extends', () => {
class TO{

}

@Rule(TO)
class UserDTO extends TO{
@Rule(RuleType.number().max(10))
age: number;
}

@Rule(UserDTO)
class HelloDTO extends UserDTO{

@Rule(RuleType.number().min(4))
age: number;
}

class Hello {
@Validate()
school(a, data: HelloDTO) {
return data;
}
}
const user = {
age: 11
};
const result = new Hello().school(1, user);
assert.deepEqual(result, user);
Expand Down