@midwayjs/validate 3.0.0-beta.11 → 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 +42 -0
- package/dist/error.d.ts +5 -0
- package/dist/error.js +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/service.d.ts +14 -7
- package/dist/service.js +29 -6
- package/package.json +8 -8
- package/LICENSE +0 -21
- package/dist/util.d.ts +0 -5
- package/dist/util.js +0 -62
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,48 @@
|
|
|
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
|
+
|
|
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)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* cos config definition & 3.x doc update ([#1515](https://github.com/midwayjs/midway/issues/1515)) ([0ac7ac5](https://github.com/midwayjs/midway/commit/0ac7ac5805b7ab8873f8792fc1712a74e3223172))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
# [3.0.0-beta.13](https://github.com/midwayjs/midway/compare/v3.0.0-beta.12...v3.0.0-beta.13) (2021-12-30)
|
|
29
|
+
|
|
30
|
+
**Note:** Version bump only for package @midwayjs/validate
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# [3.0.0-beta.12](https://github.com/midwayjs/midway/compare/v3.0.0-beta.11...v3.0.0-beta.12) (2021-12-28)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* custom error code & add @Files/@Fields ([#1438](https://github.com/midwayjs/midway/issues/1438)) ([b0032af](https://github.com/midwayjs/midway/commit/b0032afd2fa9ea0416fe69f4bd0c1a58bea5314e))
|
|
42
|
+
* support throw err status ([#1440](https://github.com/midwayjs/midway/issues/1440)) ([7b98110](https://github.com/midwayjs/midway/commit/7b98110d65c5287a8fcb3eb5356dea2d7a32cee9))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
6
48
|
# [3.0.0-beta.11](https://github.com/midwayjs/midway/compare/v3.0.0-beta.10...v3.0.0-beta.11) (2021-12-21)
|
|
7
49
|
|
|
8
50
|
**Note:** Version bump only for package @midwayjs/validate
|
package/dist/error.d.ts
ADDED
package/dist/error.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MidwayValidationError = void 0;
|
|
4
|
+
const core_1 = require("@midwayjs/core");
|
|
5
|
+
const ValidateErrorCode = (0, core_1.registerErrorCode)('validate', {
|
|
6
|
+
VALIDATE_FAIL: 10000,
|
|
7
|
+
});
|
|
8
|
+
class MidwayValidationError extends core_1.MidwayHttpError {
|
|
9
|
+
constructor(message, status, cause) {
|
|
10
|
+
super(message, status, ValidateErrorCode.VALIDATE_FAIL, {
|
|
11
|
+
cause,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.MidwayValidationError = MidwayValidationError;
|
|
16
|
+
//# sourceMappingURL=error.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -17,4 +17,5 @@ __exportStar(require("./decorator/rule"), exports);
|
|
|
17
17
|
__exportStar(require("./decorator/validate"), exports);
|
|
18
18
|
__exportStar(require("./dtoHelper"), exports);
|
|
19
19
|
__exportStar(require("./service"), exports);
|
|
20
|
+
__exportStar(require("./error"), exports);
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
package/dist/service.d.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
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
|
-
validateConfig: typeof DefaultConfig.validate;
|
|
6
|
-
i18nConfig: any;
|
|
7
|
-
i18nService: MidwayI18nServiceSingleton;
|
|
8
|
-
messages: {};
|
|
9
|
-
init(): Promise<void>;
|
|
10
|
-
validate
|
|
6
|
+
protected validateConfig: typeof DefaultConfig.validate;
|
|
7
|
+
protected i18nConfig: any;
|
|
8
|
+
protected i18nService: MidwayI18nServiceSingleton;
|
|
9
|
+
protected messages: {};
|
|
10
|
+
protected init(): Promise<void>;
|
|
11
|
+
validate<T extends new (...args: any[]) => any>(ClzType: T, value: any, options?: {
|
|
11
12
|
errorStatus?: number;
|
|
12
13
|
locale?: string;
|
|
13
14
|
validationOptions?: Joi.ValidationOptions;
|
|
14
|
-
}): Joi.ValidationResult<
|
|
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
|
@@ -13,9 +13,9 @@ exports.ValidateService = void 0;
|
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
14
|
const DefaultConfig = require("./config/config.default");
|
|
15
15
|
const constants_1 = require("./constants");
|
|
16
|
-
const core_1 = require("@midwayjs/core");
|
|
17
16
|
const Joi = require("joi");
|
|
18
17
|
const i18n_1 = require("@midwayjs/i18n");
|
|
18
|
+
const error_1 = require("./error");
|
|
19
19
|
let ValidateService = class ValidateService {
|
|
20
20
|
constructor() {
|
|
21
21
|
this.messages = {};
|
|
@@ -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
|
|
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,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.merge = exports.set = exports.reach = exports.overrideMethodProperty = void 0;
|
|
4
|
-
function overrideMethodProperty(object, key, method) {
|
|
5
|
-
const descriptor = Object.getOwnPropertyDescriptor(object, key);
|
|
6
|
-
if (!descriptor) {
|
|
7
|
-
throw new Error(`Cannot find property of "${key}"`);
|
|
8
|
-
}
|
|
9
|
-
const superMethod = descriptor.value;
|
|
10
|
-
if (typeof superMethod !== 'function') {
|
|
11
|
-
throw new Error(`"${key}" is not a method property!`);
|
|
12
|
-
}
|
|
13
|
-
Object.defineProperty(object, key, {
|
|
14
|
-
...descriptor,
|
|
15
|
-
value: function overridedMethod() {
|
|
16
|
-
// eslint-disable-next-line prefer-rest-params
|
|
17
|
-
return method.call(this, superMethod).apply(this, arguments);
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
exports.overrideMethodProperty = overrideMethodProperty;
|
|
22
|
-
function reach(target, path) {
|
|
23
|
-
for (const key of path.split('.')) {
|
|
24
|
-
if (target && target[key] !== undefined) {
|
|
25
|
-
target = target[key];
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return target;
|
|
32
|
-
}
|
|
33
|
-
exports.reach = reach;
|
|
34
|
-
function set(target, path, value) {
|
|
35
|
-
path.split('.').reduce((res, path, index, array) => {
|
|
36
|
-
if (index === array.length - 1) {
|
|
37
|
-
res[path] = value;
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
res[path] = typeof res[path] === 'object' ? res[path] : {};
|
|
41
|
-
}
|
|
42
|
-
return res[path];
|
|
43
|
-
}, target);
|
|
44
|
-
return target;
|
|
45
|
-
}
|
|
46
|
-
exports.set = set;
|
|
47
|
-
function merge(target, source) {
|
|
48
|
-
for (const key in source) {
|
|
49
|
-
try {
|
|
50
|
-
target[key] =
|
|
51
|
-
typeof source[key] === 'object'
|
|
52
|
-
? { ...merge(target[key], source[key]) }
|
|
53
|
-
: source[key];
|
|
54
|
-
}
|
|
55
|
-
catch (e) {
|
|
56
|
-
target[key] = source[key];
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return target;
|
|
60
|
-
}
|
|
61
|
-
exports.merge = merge;
|
|
62
|
-
//# sourceMappingURL=util.js.map
|