@midwayjs/validate 3.9.9 → 3.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/configuration.d.ts +3 -2
- package/dist/configuration.js +31 -28
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +2 -1
- package/dist/decorator/valid.d.ts +3 -0
- package/dist/decorator/valid.js +15 -0
- package/dist/decorator/validate.d.ts +1 -1
- package/dist/decorator/validate.js +3 -3
- package/dist/dtoHelper.d.ts +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/pipe.d.ts +36 -0
- package/dist/pipe.js +119 -0
- package/dist/service.d.ts +2 -2
- package/package.json +7 -7
package/dist/configuration.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
1
|
+
import { MidwayDecoratorService, IMidwayContainer } from '@midwayjs/core';
|
|
2
2
|
import { ValidateService } from './service';
|
|
3
3
|
export declare class ValidateConfiguration {
|
|
4
4
|
decoratorService: MidwayDecoratorService;
|
|
5
5
|
validateService: ValidateService;
|
|
6
|
-
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
onReady(container: IMidwayContainer): Promise<void>;
|
|
7
8
|
}
|
|
8
9
|
//# sourceMappingURL=configuration.d.ts.map
|
package/dist/configuration.js
CHANGED
|
@@ -11,40 +11,37 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ValidateConfiguration = void 0;
|
|
13
13
|
const core_1 = require("@midwayjs/core");
|
|
14
|
-
const constants_1 = require("./constants");
|
|
15
|
-
const util = require("util");
|
|
16
14
|
const DefaultConfig = require("./config/config.default");
|
|
17
15
|
const service_1 = require("./service");
|
|
18
16
|
const i18n = require("@midwayjs/i18n");
|
|
19
|
-
const
|
|
17
|
+
const pipe_1 = require("./pipe");
|
|
18
|
+
const constants_1 = require("./constants");
|
|
20
19
|
let ValidateConfiguration = class ValidateConfiguration {
|
|
21
|
-
async
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const validateOptions = (_a = options.metadata) === null || _a === void 0 ? void 0 : _a.options;
|
|
28
|
-
// add aspect method
|
|
29
|
-
return {
|
|
30
|
-
before: (joinPoint) => {
|
|
31
|
-
for (let i = 0; i < paramTypes.length; i++) {
|
|
32
|
-
if (!validateOptions.locale) {
|
|
33
|
-
const maybeCtx = joinPoint.target[core_1.REQUEST_OBJ_CTX_KEY];
|
|
34
|
-
if (maybeCtx && maybeCtx.getAttr) {
|
|
35
|
-
validateOptions.locale = maybeCtx.getAttr(i18n.I18N_ATTR_KEY);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const item = paramTypes[i];
|
|
39
|
-
const result = this.validateService.validate(item, joinPoint.args[i], validateOptions);
|
|
40
|
-
if (result && result.value) {
|
|
41
|
-
joinPoint.args[i] = result.value;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
};
|
|
20
|
+
async init() {
|
|
21
|
+
this.decoratorService.registerParameterHandler(constants_1.VALID_KEY, ({ parameterIndex, originParamType, originArgs, metadata }) => {
|
|
22
|
+
if (!metadata.schema) {
|
|
23
|
+
metadata.schema = this.validateService.getSchema(originParamType);
|
|
24
|
+
}
|
|
25
|
+
return originArgs[parameterIndex];
|
|
46
26
|
});
|
|
47
27
|
}
|
|
28
|
+
async onReady(container) {
|
|
29
|
+
await container.getAsync(pipe_1.ValidationPipe);
|
|
30
|
+
await container.getAsync(pipe_1.ParseIntPipe);
|
|
31
|
+
await container.getAsync(pipe_1.ParseBoolPipe);
|
|
32
|
+
await container.getAsync(pipe_1.ParseFloatPipe);
|
|
33
|
+
await container.getAsync(pipe_1.DefaultValidPipe);
|
|
34
|
+
this.decoratorService.registerParameterHandler(constants_1.VALID_KEY, ({ parameterIndex, originParamType, originArgs, metadata }) => {
|
|
35
|
+
if (!metadata.schema) {
|
|
36
|
+
metadata.schema = this.validateService.getSchema(originParamType);
|
|
37
|
+
}
|
|
38
|
+
return originArgs[parameterIndex];
|
|
39
|
+
});
|
|
40
|
+
// register web param default pipe
|
|
41
|
+
this.decoratorService.registerParameterPipes(core_1.WEB_ROUTER_PARAM_KEY, [
|
|
42
|
+
pipe_1.ValidationPipe,
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
48
45
|
};
|
|
49
46
|
__decorate([
|
|
50
47
|
(0, core_1.Inject)(),
|
|
@@ -54,6 +51,12 @@ __decorate([
|
|
|
54
51
|
(0, core_1.Inject)(),
|
|
55
52
|
__metadata("design:type", service_1.ValidateService)
|
|
56
53
|
], ValidateConfiguration.prototype, "validateService", void 0);
|
|
54
|
+
__decorate([
|
|
55
|
+
(0, core_1.Init)(),
|
|
56
|
+
__metadata("design:type", Function),
|
|
57
|
+
__metadata("design:paramtypes", []),
|
|
58
|
+
__metadata("design:returntype", Promise)
|
|
59
|
+
], ValidateConfiguration.prototype, "init", null);
|
|
57
60
|
ValidateConfiguration = __decorate([
|
|
58
61
|
(0, core_1.Configuration)({
|
|
59
62
|
namespace: 'validate',
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VALIDATE_KEY = exports.RULES_KEY = void 0;
|
|
3
|
+
exports.VALID_KEY = exports.VALIDATE_KEY = exports.RULES_KEY = void 0;
|
|
4
4
|
exports.RULES_KEY = 'common:rules';
|
|
5
5
|
exports.VALIDATE_KEY = 'common:validate';
|
|
6
|
+
exports.VALID_KEY = 'validate:valid';
|
|
6
7
|
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Valid = void 0;
|
|
4
|
+
const core_1 = require("@midwayjs/core");
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
const pipe_1 = require("../pipe");
|
|
7
|
+
function Valid(schema) {
|
|
8
|
+
return (0, core_1.createCustomParamDecorator)(constants_1.VALID_KEY, {
|
|
9
|
+
schema,
|
|
10
|
+
}, {
|
|
11
|
+
pipes: [pipe_1.DefaultValidPipe],
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
exports.Valid = Valid;
|
|
15
|
+
//# sourceMappingURL=valid.js.map
|
|
@@ -4,5 +4,5 @@ export interface ValidateOptions {
|
|
|
4
4
|
locale?: string;
|
|
5
5
|
validationOptions?: Joi.ValidationOptions;
|
|
6
6
|
}
|
|
7
|
-
export declare function Validate(options?: ValidateOptions):
|
|
7
|
+
export declare function Validate(options?: ValidateOptions): (target: any, methodName: any, descriptor: any) => void;
|
|
8
8
|
//# sourceMappingURL=validate.d.ts.map
|
|
@@ -4,9 +4,9 @@ exports.Validate = void 0;
|
|
|
4
4
|
const core_1 = require("@midwayjs/core");
|
|
5
5
|
const constants_1 = require("../constants");
|
|
6
6
|
function Validate(options = {}) {
|
|
7
|
-
return (
|
|
8
|
-
options,
|
|
9
|
-
}
|
|
7
|
+
return (target, methodName, descriptor) => {
|
|
8
|
+
(0, core_1.savePropertyMetadata)(constants_1.VALIDATE_KEY, options, target, methodName);
|
|
9
|
+
};
|
|
10
10
|
}
|
|
11
11
|
exports.Validate = Validate;
|
|
12
12
|
//# sourceMappingURL=validate.js.map
|
package/dist/dtoHelper.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export interface Dto<T> extends Function {
|
|
2
2
|
new (): T;
|
|
3
3
|
}
|
|
4
|
-
export declare function PickDto<T, K extends keyof T>(dto: Dto<T>, keys: K[]): Dto<Pick<T, typeof keys[number]>>;
|
|
5
|
-
export declare function OmitDto<T, K extends keyof T>(dto: Dto<T>, keys: K[]): Dto<Omit<T, typeof keys[number]>>;
|
|
4
|
+
export declare function PickDto<T, K extends keyof T>(dto: Dto<T>, keys: K[]): Dto<Pick<T, (typeof keys)[number]>>;
|
|
5
|
+
export declare function OmitDto<T, K extends keyof T>(dto: Dto<T>, keys: K[]): Dto<Omit<T, (typeof keys)[number]>>;
|
|
6
6
|
//# sourceMappingURL=dtoHelper.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export { ValidateConfiguration as Configuration } from './configuration';
|
|
2
2
|
export * from './decorator/rule';
|
|
3
3
|
export * from './decorator/validate';
|
|
4
|
+
export * from './decorator/valid';
|
|
4
5
|
export * from './dtoHelper';
|
|
5
6
|
export * from './service';
|
|
6
7
|
export * from './error';
|
|
8
|
+
export * from './pipe';
|
|
7
9
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -19,7 +19,9 @@ var configuration_1 = require("./configuration");
|
|
|
19
19
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.ValidateConfiguration; } });
|
|
20
20
|
__exportStar(require("./decorator/rule"), exports);
|
|
21
21
|
__exportStar(require("./decorator/validate"), exports);
|
|
22
|
+
__exportStar(require("./decorator/valid"), exports);
|
|
22
23
|
__exportStar(require("./dtoHelper"), exports);
|
|
23
24
|
__exportStar(require("./service"), exports);
|
|
24
25
|
__exportStar(require("./error"), exports);
|
|
26
|
+
__exportStar(require("./pipe"), exports);
|
|
25
27
|
//# sourceMappingURL=index.js.map
|
package/dist/pipe.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { PipeTransform, TransformOptions } from '@midwayjs/core';
|
|
2
|
+
import { ValidateService } from './service';
|
|
3
|
+
import { ValidateOptions } from './decorator/validate';
|
|
4
|
+
import * as Joi from 'joi';
|
|
5
|
+
export declare abstract class AbstractValidationPipe implements PipeTransform {
|
|
6
|
+
protected validateService: ValidateService;
|
|
7
|
+
abstract transform(value: any, options: TransformOptions): any;
|
|
8
|
+
validateWithSchema(value: any, options: TransformOptions, schema: Joi.AnySchema): any;
|
|
9
|
+
validate(value: any, options: TransformOptions): any;
|
|
10
|
+
protected parseValidationOptions(options: TransformOptions): ValidateOptions;
|
|
11
|
+
protected getSchema(): any;
|
|
12
|
+
}
|
|
13
|
+
export declare class ValidationPipe extends AbstractValidationPipe {
|
|
14
|
+
transform(value: any, options: TransformOptions): any;
|
|
15
|
+
}
|
|
16
|
+
declare abstract class ParsePipe extends AbstractValidationPipe {
|
|
17
|
+
transform(value: any, options: TransformOptions): any;
|
|
18
|
+
}
|
|
19
|
+
export declare class DefaultValidPipe extends ParsePipe {
|
|
20
|
+
}
|
|
21
|
+
export declare class ParseIntPipe extends ParsePipe {
|
|
22
|
+
getSchema(): Joi.AnySchema<any>;
|
|
23
|
+
}
|
|
24
|
+
export declare class ParseBoolPipe extends ParsePipe {
|
|
25
|
+
getSchema(): Joi.AnySchema<any>;
|
|
26
|
+
}
|
|
27
|
+
export declare class ParseFloatPipe extends ParsePipe {
|
|
28
|
+
getSchema(): Joi.AnySchema<any>;
|
|
29
|
+
}
|
|
30
|
+
export declare class DefaultValuePipe<T = any, R = any> implements PipeTransform<T, R> {
|
|
31
|
+
protected readonly defaultValue: R;
|
|
32
|
+
constructor(defaultValue: R);
|
|
33
|
+
transform(value: any, options: any): any;
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=pipe.d.ts.map
|
package/dist/pipe.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.DefaultValuePipe = exports.ParseFloatPipe = exports.ParseBoolPipe = exports.ParseIntPipe = exports.DefaultValidPipe = exports.ValidationPipe = exports.AbstractValidationPipe = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const service_1 = require("./service");
|
|
15
|
+
const i18n = require("@midwayjs/i18n");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
const Joi = require("joi");
|
|
18
|
+
class AbstractValidationPipe {
|
|
19
|
+
validateWithSchema(value, options, schema) {
|
|
20
|
+
const validateOptions = this.parseValidationOptions(options);
|
|
21
|
+
const result = this.validateService.validateWithSchema(schema, value, validateOptions);
|
|
22
|
+
if (result && result.value !== undefined) {
|
|
23
|
+
return result.value;
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
validate(value, options) {
|
|
28
|
+
const validateOptions = this.parseValidationOptions(options);
|
|
29
|
+
if (options.metaType.isBaseType) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
const result = this.validateService.validate(options.metaType.originDesign, value, validateOptions);
|
|
33
|
+
if (result && result.value) {
|
|
34
|
+
return result.value;
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
parseValidationOptions(options) {
|
|
39
|
+
const validateOptions = (0, core_1.getPropertyMetadata)(constants_1.VALIDATE_KEY, options.target, options.methodName) ||
|
|
40
|
+
{};
|
|
41
|
+
if (!validateOptions.locale) {
|
|
42
|
+
const maybeCtx = options.target[core_1.REQUEST_OBJ_CTX_KEY];
|
|
43
|
+
if (maybeCtx && maybeCtx.getAttr) {
|
|
44
|
+
validateOptions.locale = maybeCtx.getAttr(i18n.I18N_ATTR_KEY);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return validateOptions;
|
|
48
|
+
}
|
|
49
|
+
getSchema() {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
__decorate([
|
|
54
|
+
(0, core_1.Inject)(),
|
|
55
|
+
__metadata("design:type", service_1.ValidateService)
|
|
56
|
+
], AbstractValidationPipe.prototype, "validateService", void 0);
|
|
57
|
+
exports.AbstractValidationPipe = AbstractValidationPipe;
|
|
58
|
+
let ValidationPipe = class ValidationPipe extends AbstractValidationPipe {
|
|
59
|
+
transform(value, options) {
|
|
60
|
+
return this.validate(value, options);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
ValidationPipe = __decorate([
|
|
64
|
+
(0, core_1.Pipe)()
|
|
65
|
+
], ValidationPipe);
|
|
66
|
+
exports.ValidationPipe = ValidationPipe;
|
|
67
|
+
class ParsePipe extends AbstractValidationPipe {
|
|
68
|
+
transform(value, options) {
|
|
69
|
+
return this.validateWithSchema(value, options, options.metadata['schema'] || this.getSchema());
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
let DefaultValidPipe = class DefaultValidPipe extends ParsePipe {
|
|
73
|
+
};
|
|
74
|
+
DefaultValidPipe = __decorate([
|
|
75
|
+
(0, core_1.Pipe)()
|
|
76
|
+
], DefaultValidPipe);
|
|
77
|
+
exports.DefaultValidPipe = DefaultValidPipe;
|
|
78
|
+
let ParseIntPipe = class ParseIntPipe extends ParsePipe {
|
|
79
|
+
getSchema() {
|
|
80
|
+
return Joi.number().integer().required();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
ParseIntPipe = __decorate([
|
|
84
|
+
(0, core_1.Pipe)()
|
|
85
|
+
], ParseIntPipe);
|
|
86
|
+
exports.ParseIntPipe = ParseIntPipe;
|
|
87
|
+
let ParseBoolPipe = class ParseBoolPipe extends ParsePipe {
|
|
88
|
+
getSchema() {
|
|
89
|
+
return Joi.boolean().required();
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
ParseBoolPipe = __decorate([
|
|
93
|
+
(0, core_1.Pipe)()
|
|
94
|
+
], ParseBoolPipe);
|
|
95
|
+
exports.ParseBoolPipe = ParseBoolPipe;
|
|
96
|
+
let ParseFloatPipe = class ParseFloatPipe extends ParsePipe {
|
|
97
|
+
getSchema() {
|
|
98
|
+
return Joi.number().required();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
ParseFloatPipe = __decorate([
|
|
102
|
+
(0, core_1.Pipe)()
|
|
103
|
+
], ParseFloatPipe);
|
|
104
|
+
exports.ParseFloatPipe = ParseFloatPipe;
|
|
105
|
+
class DefaultValuePipe {
|
|
106
|
+
constructor(defaultValue) {
|
|
107
|
+
this.defaultValue = defaultValue;
|
|
108
|
+
}
|
|
109
|
+
transform(value, options) {
|
|
110
|
+
if (value === undefined ||
|
|
111
|
+
value === null ||
|
|
112
|
+
(typeof value === 'number' && isNaN(value))) {
|
|
113
|
+
return this.defaultValue;
|
|
114
|
+
}
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
exports.DefaultValuePipe = DefaultValuePipe;
|
|
119
|
+
//# sourceMappingURL=pipe.js.map
|
package/dist/service.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as DefaultConfig from './config/config.default';
|
|
2
2
|
import * as Joi from 'joi';
|
|
3
3
|
import { MidwayI18nServiceSingleton } from '@midwayjs/i18n';
|
|
4
|
-
import { ObjectSchema } from 'joi';
|
|
4
|
+
import { ObjectSchema, AnySchema } from 'joi';
|
|
5
5
|
export declare class ValidateService {
|
|
6
6
|
protected validateConfig: typeof DefaultConfig.validate;
|
|
7
7
|
protected i18nConfig: any;
|
|
@@ -13,7 +13,7 @@ export declare class ValidateService {
|
|
|
13
13
|
locale?: string;
|
|
14
14
|
validationOptions?: Joi.ValidationOptions;
|
|
15
15
|
}): Joi.ValidationResult<T> | undefined;
|
|
16
|
-
validateWithSchema<T>(schema:
|
|
16
|
+
validateWithSchema<T>(schema: AnySchema<T>, value: any, options?: {
|
|
17
17
|
errorStatus?: number;
|
|
18
18
|
locale?: string;
|
|
19
19
|
validationOptions?: Joi.ValidationOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/validate",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.1",
|
|
4
4
|
"description": "Midway Component for mongoose",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -23,14 +23,14 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@midwayjs/i18n": "^3.
|
|
26
|
+
"@midwayjs/i18n": "^3.10.1",
|
|
27
27
|
"joi": "^17.2.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@midwayjs/core": "^3.
|
|
31
|
-
"@midwayjs/express": "^3.
|
|
32
|
-
"@midwayjs/koa": "^3.
|
|
33
|
-
"@midwayjs/mock": "^3.
|
|
30
|
+
"@midwayjs/core": "^3.10.1",
|
|
31
|
+
"@midwayjs/express": "^3.10.1",
|
|
32
|
+
"@midwayjs/koa": "^3.10.1",
|
|
33
|
+
"@midwayjs/mock": "^3.10.1"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "6bc9f7a97e4188399d2406f5a38bad5aeb983e07"
|
|
36
36
|
}
|