-
Notifications
You must be signed in to change notification settings - Fork 568
/
pipe.ts
126 lines (112 loc) 路 3.06 KB
/
pipe.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {
getPropertyMetadata,
Inject,
Pipe,
PipeTransform,
REQUEST_OBJ_CTX_KEY,
TransformOptions,
} from '@midwayjs/core';
import { ValidateService } from './service';
import * as i18n from '@midwayjs/i18n';
import { VALIDATE_KEY } from './constants';
import { ValidateOptions } from './decorator/validate';
import * as Joi from 'joi';
export abstract class AbstractValidationPipe implements PipeTransform {
@Inject()
protected validateService: ValidateService;
abstract transform(value: any, options: TransformOptions);
validateWithSchema(
value: any,
options: TransformOptions,
schema: Joi.AnySchema
) {
const validateOptions = this.parseValidationOptions(options);
const result = this.validateService.validateWithSchema(
schema,
value,
validateOptions
);
if (result && result.value !== undefined) {
return result.value;
}
return value;
}
validate(value: any, options: TransformOptions) {
const validateOptions = this.parseValidationOptions(options);
if (options.metaType.isBaseType) {
return value;
}
const result = this.validateService.validate(
options.metaType.originDesign as any,
value,
validateOptions
);
if (result && result.value) {
return result.value;
}
return value;
}
protected parseValidationOptions(options: TransformOptions): ValidateOptions {
const validateOptions: ValidateOptions =
getPropertyMetadata(VALIDATE_KEY, options.target, options.methodName) ||
{};
if (!validateOptions.locale) {
const maybeCtx = options.target[REQUEST_OBJ_CTX_KEY];
if (maybeCtx && maybeCtx.getAttr) {
validateOptions.locale = maybeCtx.getAttr(i18n.I18N_ATTR_KEY);
}
}
return validateOptions;
}
protected getSchema() {
return undefined;
}
}
@Pipe()
export class ValidationPipe extends AbstractValidationPipe {
transform(value: any, options: TransformOptions) {
return this.validate(value, options);
}
}
export abstract class ParsePipe extends AbstractValidationPipe {
transform(value: any, options: TransformOptions) {
return this.validateWithSchema(
value,
options,
options.metadata['schema'] || this.getSchema()
);
}
}
@Pipe()
export class DecoratorValidPipe extends ParsePipe {}
@Pipe()
export class ParseIntPipe extends ParsePipe {
getSchema(): Joi.AnySchema<any> {
return Joi.number().integer().required();
}
}
@Pipe()
export class ParseBoolPipe extends ParsePipe {
getSchema(): Joi.AnySchema<any> {
return Joi.boolean().required();
}
}
@Pipe()
export class ParseFloatPipe extends ParsePipe {
getSchema(): Joi.AnySchema<any> {
return Joi.number().required();
}
}
export class DefaultValuePipe<T = any, R = any> implements PipeTransform<T, R> {
constructor(protected readonly defaultValue: R) {}
transform(value: any, options: any) {
if (
value === undefined ||
value === null ||
(typeof value === 'number' && isNaN(value as unknown as number))
) {
return this.defaultValue;
}
return value;
}
}