@midwayjs/validate 3.0.0-beta.14 → 3.0.0-beta.15
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/CHANGELOG.md +11 -0
- package/dist/error.d.ts +1 -7
- package/dist/error.js +1 -15
- package/dist/service.d.ts +7 -0
- package/dist/service.js +28 -5
- package/package.json +8 -8
- package/LICENSE +0 -21
- package/dist/util.d.ts +0 -5
- package/dist/util.js +0 -63
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [3.0.0-beta.15](https://github.com/midwayjs/midway/compare/v3.0.0-beta.14...v3.0.0-beta.15) (2022-01-07)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* add info middleware ([#1530](https://github.com/midwayjs/midway/issues/1530)) ([7077f1d](https://github.com/midwayjs/midway/commit/7077f1d84355633a1c2fced35bfcc2152f42a7ac))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [3.0.0-beta.14](https://github.com/midwayjs/midway/compare/v3.0.0-beta.13...v3.0.0-beta.14) (2022-01-04)
|
|
7
18
|
|
|
8
19
|
|
package/dist/error.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MidwayHttpError } from '@midwayjs/core';
|
|
2
2
|
export declare class MidwayValidationError extends MidwayHttpError {
|
|
3
3
|
constructor(message: any, status: any, cause: any);
|
|
4
4
|
}
|
|
5
|
-
export declare class ValidationPropertyMissingError extends MidwayError {
|
|
6
|
-
constructor(key: any);
|
|
7
|
-
}
|
|
8
|
-
export declare class ValidationNotMethodPropertyError extends MidwayError {
|
|
9
|
-
constructor(key: any);
|
|
10
|
-
}
|
|
11
5
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.js
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.MidwayValidationError = void 0;
|
|
4
4
|
const core_1 = require("@midwayjs/core");
|
|
5
5
|
const ValidateErrorCode = (0, core_1.registerErrorCode)('validate', {
|
|
6
6
|
VALIDATE_FAIL: 10000,
|
|
7
|
-
PROPERTY_MISSING: 10001,
|
|
8
|
-
NOT_METHOD_PROPERTY: 10002,
|
|
9
7
|
});
|
|
10
8
|
class MidwayValidationError extends core_1.MidwayHttpError {
|
|
11
9
|
constructor(message, status, cause) {
|
|
@@ -15,16 +13,4 @@ class MidwayValidationError extends core_1.MidwayHttpError {
|
|
|
15
13
|
}
|
|
16
14
|
}
|
|
17
15
|
exports.MidwayValidationError = MidwayValidationError;
|
|
18
|
-
class ValidationPropertyMissingError extends core_1.MidwayError {
|
|
19
|
-
constructor(key) {
|
|
20
|
-
super(`Cannot find property of "${key}"`, ValidateErrorCode.PROPERTY_MISSING);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.ValidationPropertyMissingError = ValidationPropertyMissingError;
|
|
24
|
-
class ValidationNotMethodPropertyError extends core_1.MidwayError {
|
|
25
|
-
constructor(key) {
|
|
26
|
-
super(`"${key}" is not a method property!`, ValidateErrorCode.NOT_METHOD_PROPERTY);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.ValidationNotMethodPropertyError = ValidationNotMethodPropertyError;
|
|
30
16
|
//# sourceMappingURL=error.js.map
|
package/dist/service.d.ts
CHANGED
|
@@ -1,6 +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
5
|
export declare class ValidateService {
|
|
5
6
|
protected validateConfig: typeof DefaultConfig.validate;
|
|
6
7
|
protected i18nConfig: any;
|
|
@@ -12,5 +13,11 @@ export declare class ValidateService {
|
|
|
12
13
|
locale?: string;
|
|
13
14
|
validationOptions?: Joi.ValidationOptions;
|
|
14
15
|
}): Joi.ValidationResult<T>;
|
|
16
|
+
validateWithSchema<T>(schema: ObjectSchema<any>, value: any, options?: {
|
|
17
|
+
errorStatus?: number;
|
|
18
|
+
locale?: string;
|
|
19
|
+
validationOptions?: Joi.ValidationOptions;
|
|
20
|
+
}): Joi.ValidationResult<T>;
|
|
21
|
+
getSchema<T extends new (...args: any[]) => any>(ClzType: T): ObjectSchema<any>;
|
|
15
22
|
}
|
|
16
23
|
//# sourceMappingURL=service.d.ts.map
|
package/dist/service.js
CHANGED
|
@@ -26,8 +26,8 @@ let ValidateService = class ValidateService {
|
|
|
26
26
|
this.messages[(0, i18n_1.formatLocale)(locale)] = Object.fromEntries(this.i18nService.getLocaleMapping(locale, 'validate'));
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
validate(ClzType, value, options) {
|
|
30
|
-
var _a, _b;
|
|
29
|
+
validate(ClzType, value, options = {}) {
|
|
30
|
+
var _a, _b, _c;
|
|
31
31
|
options.validationOptions = options.validationOptions || {};
|
|
32
32
|
options.validationOptions.errors = options.validationOptions.errors || {};
|
|
33
33
|
options.validationOptions.errors.language = (0, i18n_1.formatLocale)(this.i18nService.getAvailableLocale(options.validationOptions.errors.language ||
|
|
@@ -36,17 +36,40 @@ let ValidateService = class ValidateService {
|
|
|
36
36
|
const rules = (0, decorator_1.getClassExtendedMetadata)(constants_1.RULES_KEY, ClzType);
|
|
37
37
|
if (rules) {
|
|
38
38
|
const schema = Joi.object(rules);
|
|
39
|
-
const result = schema.validate(value, Object.assign(this.validateConfig.validationOptions, {
|
|
39
|
+
const result = schema.validate(value, Object.assign((_a = this.validateConfig.validationOptions) !== null && _a !== void 0 ? _a : {}, {
|
|
40
40
|
messages: this.messages,
|
|
41
|
-
}, (
|
|
41
|
+
}, (_b = options.validationOptions) !== null && _b !== void 0 ? _b : {}));
|
|
42
42
|
if (result.error) {
|
|
43
|
-
throw new error_1.MidwayValidationError(result.error.message, (
|
|
43
|
+
throw new error_1.MidwayValidationError(result.error.message, (_c = options === null || options === void 0 ? void 0 : options.errorStatus) !== null && _c !== void 0 ? _c : this.validateConfig.errorStatus, result.error);
|
|
44
44
|
}
|
|
45
45
|
else {
|
|
46
46
|
return result;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
+
validateWithSchema(schema, value, options = {}) {
|
|
51
|
+
var _a, _b, _c;
|
|
52
|
+
options.validationOptions = options.validationOptions || {};
|
|
53
|
+
options.validationOptions.errors = options.validationOptions.errors || {};
|
|
54
|
+
options.validationOptions.errors.language = (0, i18n_1.formatLocale)(this.i18nService.getAvailableLocale(options.validationOptions.errors.language ||
|
|
55
|
+
options.locale ||
|
|
56
|
+
this.i18nConfig.defaultLocale, 'validate'));
|
|
57
|
+
const result = schema.validate(value, Object.assign((_a = this.validateConfig.validationOptions) !== null && _a !== void 0 ? _a : {}, {
|
|
58
|
+
messages: this.messages,
|
|
59
|
+
}, (_b = options.validationOptions) !== null && _b !== void 0 ? _b : {}));
|
|
60
|
+
if (result.error) {
|
|
61
|
+
throw new error_1.MidwayValidationError(result.error.message, (_c = options === null || options === void 0 ? void 0 : options.errorStatus) !== null && _c !== void 0 ? _c : this.validateConfig.errorStatus, result.error);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
getSchema(ClzType) {
|
|
68
|
+
const rules = (0, decorator_1.getClassExtendedMetadata)(constants_1.RULES_KEY, ClzType);
|
|
69
|
+
if (rules) {
|
|
70
|
+
return Joi.object(rules);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
50
73
|
};
|
|
51
74
|
__decorate([
|
|
52
75
|
(0, decorator_1.Config)('validate'),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midwayjs/validate",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.15",
|
|
4
4
|
"description": "Midway Component for mongoose",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "index.d.ts",
|
|
@@ -23,15 +23,15 @@
|
|
|
23
23
|
},
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@midwayjs/i18n": "^3.0.0-beta.
|
|
26
|
+
"@midwayjs/i18n": "^3.0.0-beta.15",
|
|
27
27
|
"joi": "^17.2.1"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@midwayjs/core": "^3.0.0-beta.
|
|
31
|
-
"@midwayjs/decorator": "^3.0.0-beta.
|
|
32
|
-
"@midwayjs/express": "^3.0.0-beta.
|
|
33
|
-
"@midwayjs/koa": "^3.0.0-beta.
|
|
34
|
-
"@midwayjs/mock": "^3.0.0-beta.
|
|
30
|
+
"@midwayjs/core": "^3.0.0-beta.15",
|
|
31
|
+
"@midwayjs/decorator": "^3.0.0-beta.15",
|
|
32
|
+
"@midwayjs/express": "^3.0.0-beta.15",
|
|
33
|
+
"@midwayjs/koa": "^3.0.0-beta.15",
|
|
34
|
+
"@midwayjs/mock": "^3.0.0-beta.15"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "6b9778557289d9e8b562c7ce6127736db1248973"
|
|
37
37
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2013 - Now midwayjs
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/util.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function overrideMethodProperty(object: Record<any, any>, key: string, method: (superMethod: any) => any): void;
|
|
2
|
-
export declare function reach(target: object, path: string): any;
|
|
3
|
-
export declare function set<T extends object>(target: T, path: string, value: any): T;
|
|
4
|
-
export declare function merge(target: object, source: object): object;
|
|
5
|
-
//# sourceMappingURL=util.d.ts.map
|
package/dist/util.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.merge = exports.set = exports.reach = exports.overrideMethodProperty = void 0;
|
|
4
|
-
const error_1 = require("./error");
|
|
5
|
-
function overrideMethodProperty(object, key, method) {
|
|
6
|
-
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
7
|
-
if (!descriptor) {
|
|
8
|
-
throw new error_1.ValidationPropertyMissingError(key);
|
|
9
|
-
}
|
|
10
|
-
const superMethod = descriptor.value;
|
|
11
|
-
if (typeof superMethod !== 'function') {
|
|
12
|
-
throw new error_1.ValidationNotMethodPropertyError(key);
|
|
13
|
-
}
|
|
14
|
-
Object.defineProperty(object, key, {
|
|
15
|
-
...descriptor,
|
|
16
|
-
value: function overridedMethod() {
|
|
17
|
-
// eslint-disable-next-line prefer-rest-params
|
|
18
|
-
return method.call(this, superMethod).apply(this, arguments);
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
exports.overrideMethodProperty = overrideMethodProperty;
|
|
23
|
-
function reach(target, path) {
|
|
24
|
-
for (const key of path.split('.')) {
|
|
25
|
-
if (target && target[key] !== undefined) {
|
|
26
|
-
target = target[key];
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return target;
|
|
33
|
-
}
|
|
34
|
-
exports.reach = reach;
|
|
35
|
-
function set(target, path, value) {
|
|
36
|
-
path.split('.').reduce((res, path, index, array) => {
|
|
37
|
-
if (index === array.length - 1) {
|
|
38
|
-
res[path] = value;
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
res[path] = typeof res[path] === 'object' ? res[path] : {};
|
|
42
|
-
}
|
|
43
|
-
return res[path];
|
|
44
|
-
}, target);
|
|
45
|
-
return target;
|
|
46
|
-
}
|
|
47
|
-
exports.set = set;
|
|
48
|
-
function merge(target, source) {
|
|
49
|
-
for (const key in source) {
|
|
50
|
-
try {
|
|
51
|
-
target[key] =
|
|
52
|
-
typeof source[key] === 'object'
|
|
53
|
-
? { ...merge(target[key], source[key]) }
|
|
54
|
-
: source[key];
|
|
55
|
-
}
|
|
56
|
-
catch (e) {
|
|
57
|
-
target[key] = source[key];
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
return target;
|
|
61
|
-
}
|
|
62
|
-
exports.merge = merge;
|
|
63
|
-
//# sourceMappingURL=util.js.map
|