-
Notifications
You must be signed in to change notification settings - Fork 568
/
configuration.ts
72 lines (66 loc) 路 1.77 KB
/
configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {
Configuration,
Inject,
MidwayDecoratorService,
WEB_ROUTER_PARAM_KEY,
IMidwayContainer,
Init,
} from '@midwayjs/core';
import * as DefaultConfig from './config/config.default';
import { ValidateService } from './service';
import * as i18n from '@midwayjs/i18n';
import {
DecoratorValidPipe,
ParseBoolPipe,
ParseFloatPipe,
ParseIntPipe,
ValidationPipe,
} from './pipe';
import { VALID_KEY } from './constants';
@Configuration({
namespace: 'validate',
imports: [i18n],
importConfigs: [
{
default: DefaultConfig,
},
],
})
export class ValidateConfiguration {
@Inject()
decoratorService: MidwayDecoratorService;
@Inject()
validateService: ValidateService;
@Init()
async init() {
this.decoratorService.registerParameterHandler(
VALID_KEY,
({ parameterIndex, originParamType, originArgs, metadata }) => {
if (!metadata.schema) {
metadata.schema = this.validateService.getSchema(originParamType);
}
return originArgs[parameterIndex];
}
);
}
async onReady(container: IMidwayContainer) {
await container.getAsync(ValidationPipe);
await container.getAsync(ParseIntPipe);
await container.getAsync(ParseBoolPipe);
await container.getAsync(ParseFloatPipe);
await container.getAsync(DecoratorValidPipe);
this.decoratorService.registerParameterHandler(
VALID_KEY,
({ parameterIndex, originParamType, originArgs, metadata }) => {
if (!metadata.schema) {
metadata.schema = this.validateService.getSchema(originParamType);
}
return originArgs[parameterIndex];
}
);
// register web param default pipe
this.decoratorService.registerParameterPipes(WEB_ROUTER_PARAM_KEY, [
ValidationPipe,
]);
}
}