@midwayjs/validate 3.0.0-beta.2 → 3.0.0-beta.6

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 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.6](https://github.com/midwayjs/midway/compare/v3.0.0-beta.5...v3.0.0-beta.6) (2021-11-26)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * class transformer method missing ([#1387](https://github.com/midwayjs/midway/issues/1387)) ([074e839](https://github.com/midwayjs/midway/commit/074e8393598dc95e2742f735df75a2191c5fe25d))
12
+
13
+
14
+
15
+
16
+
17
+ # [3.0.0-beta.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
18
+
19
+ **Note:** Version bump only for package @midwayjs/validate
20
+
21
+
22
+
23
+
24
+
25
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
26
+
27
+
28
+ ### Features
29
+
30
+ * add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
31
+ * auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
32
+
33
+
34
+
35
+
36
+
37
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
38
+
39
+
40
+ ### Features
41
+
42
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
43
+
44
+
45
+
46
+
47
+
6
48
  # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
7
49
 
8
50
  **Note:** Version bump only for package @midwayjs/validate
@@ -0,0 +1,5 @@
1
+ export declare const validate: {
2
+ allowUnknown: boolean;
3
+ errorStatus: number;
4
+ };
5
+ //# sourceMappingURL=config.default.d.ts.map
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validate = void 0;
4
+ exports.validate = {
5
+ allowUnknown: false,
6
+ errorStatus: 422,
7
+ };
8
+ //# sourceMappingURL=config.default.js.map
@@ -1,6 +1,16 @@
1
1
  import { MidwayDecoratorService } from '@midwayjs/core';
2
+ import * as Joi from 'joi';
3
+ import * as DefaultConfig from './config.default';
4
+ export declare class ValidateService {
5
+ validateConfig: typeof DefaultConfig.validate;
6
+ validate(ClzType: new (...args: any[]) => any, value: any, options?: {
7
+ errorStatus?: number;
8
+ validateOptions?: Joi.ValidationOptions;
9
+ }): Joi.ValidationResult;
10
+ }
2
11
  export declare class ValidateConfiguration {
3
12
  decoratorService: MidwayDecoratorService;
13
+ validateService: ValidateService;
4
14
  init(): Promise<void>;
5
15
  }
6
16
  //# sourceMappingURL=configuration.d.ts.map
@@ -9,14 +9,39 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.ValidateConfiguration = void 0;
12
+ exports.ValidateConfiguration = exports.ValidateService = void 0;
13
13
  const decorator_1 = require("@midwayjs/decorator");
14
14
  const core_1 = require("@midwayjs/core");
15
15
  const constants_1 = require("./constants");
16
16
  const Joi = require("joi");
17
- const class_transformer_1 = require("class-transformer");
18
17
  const util = require("util");
18
+ const DefaultConfig = require("./config.default");
19
19
  const debug = util.debuglog('midway:debug');
20
+ let ValidateService = class ValidateService {
21
+ validate(ClzType, value, options) {
22
+ var _a;
23
+ const rules = (0, decorator_1.getClassExtendedMetadata)(constants_1.RULES_KEY, ClzType);
24
+ if (rules) {
25
+ const schema = Joi.object(rules);
26
+ const result = schema.validate(value);
27
+ if (result.error) {
28
+ throw new core_1.MidwayValidationError(result.error.message, (_a = options === null || options === void 0 ? void 0 : options.errorStatus) !== null && _a !== void 0 ? _a : this.validateConfig.errorStatus, result.error);
29
+ }
30
+ else {
31
+ return result;
32
+ }
33
+ }
34
+ }
35
+ };
36
+ __decorate([
37
+ (0, decorator_1.Config)('validate'),
38
+ __metadata("design:type", Object)
39
+ ], ValidateService.prototype, "validateConfig", void 0);
40
+ ValidateService = __decorate([
41
+ (0, decorator_1.Provide)(),
42
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
43
+ ], ValidateService);
44
+ exports.ValidateService = ValidateService;
20
45
  let ValidateConfiguration = class ValidateConfiguration {
21
46
  async init() {
22
47
  debug(`[validate]: Register @validate "${constants_1.VALIDATE_KEY}" handler"`);
@@ -28,20 +53,9 @@ let ValidateConfiguration = class ValidateConfiguration {
28
53
  before: (joinPoint) => {
29
54
  for (let i = 0; i < paramTypes.length; i++) {
30
55
  const item = paramTypes[i];
31
- const rules = (0, decorator_1.getClassExtendedMetadata)(constants_1.RULES_KEY, item);
32
- if (rules) {
33
- const schema = Joi.object(rules);
34
- const result = schema.validate(joinPoint.args[i]);
35
- if (result.error) {
36
- throw result.error;
37
- }
38
- else {
39
- joinPoint.args[i] = result.value;
40
- }
41
- // passed
42
- if (options.metadata.isTransform) {
43
- joinPoint.args[i] = (0, class_transformer_1.plainToClass)(item, joinPoint.args[i]);
44
- }
56
+ const result = this.validateService.validate(item, joinPoint.args[i], options.metadata);
57
+ if (result && result.value) {
58
+ joinPoint.args[i] = result.value;
45
59
  }
46
60
  }
47
61
  },
@@ -53,6 +67,10 @@ __decorate([
53
67
  (0, decorator_1.Inject)(),
54
68
  __metadata("design:type", core_1.MidwayDecoratorService)
55
69
  ], ValidateConfiguration.prototype, "decoratorService", void 0);
70
+ __decorate([
71
+ (0, decorator_1.Inject)(),
72
+ __metadata("design:type", ValidateService)
73
+ ], ValidateConfiguration.prototype, "validateService", void 0);
56
74
  __decorate([
57
75
  (0, decorator_1.Init)(),
58
76
  __metadata("design:type", Function),
@@ -62,6 +80,11 @@ __decorate([
62
80
  ValidateConfiguration = __decorate([
63
81
  (0, decorator_1.Configuration)({
64
82
  namespace: 'validate',
83
+ importConfigs: [
84
+ {
85
+ default: DefaultConfig,
86
+ },
87
+ ],
65
88
  })
66
89
  ], ValidateConfiguration);
67
90
  exports.ValidateConfiguration = ValidateConfiguration;
@@ -1,2 +1,5 @@
1
- export declare function Validate(isTransform?: boolean): MethodDecorator;
1
+ export interface ValidateOptions {
2
+ errorStatus?: number;
3
+ }
4
+ export declare function Validate(options?: ValidateOptions): MethodDecorator;
2
5
  //# sourceMappingURL=validate.d.ts.map
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Validate = void 0;
4
4
  const decorator_1 = require("@midwayjs/decorator");
5
5
  const constants_1 = require("../constants");
6
- function Validate(isTransform = true) {
6
+ function Validate(options = {}) {
7
7
  return (0, decorator_1.createCustomMethodDecorator)(constants_1.VALIDATE_KEY, {
8
- isTransform,
8
+ options,
9
9
  });
10
10
  }
11
11
  exports.Validate = Validate;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { ValidateConfiguration as Configuration } from './configuration';
1
+ export { ValidateConfiguration as Configuration, ValidateService, } from './configuration';
2
2
  export * from './decorator/rule';
3
3
  export * from './decorator/validate';
4
4
  export * from './dtoHelper';
package/dist/index.js CHANGED
@@ -10,9 +10,10 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.Configuration = void 0;
13
+ exports.ValidateService = exports.Configuration = void 0;
14
14
  var configuration_1 = require("./configuration");
15
15
  Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.ValidateConfiguration; } });
16
+ Object.defineProperty(exports, "ValidateService", { enumerable: true, get: function () { return configuration_1.ValidateService; } });
16
17
  __exportStar(require("./decorator/rule"), exports);
17
18
  __exportStar(require("./decorator/validate"), exports);
18
19
  __exportStar(require("./dtoHelper"), exports);
package/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './dist/index';
2
+
3
+ import { validate } from './dist/config.default';
4
+
5
+ declare module '@midwayjs/core/dist/interface' {
6
+ interface MidwayConfig {
7
+ validate?: validate;
8
+ }
9
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@midwayjs/validate",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.6",
4
4
  "description": "Midway Component for mongoose",
5
5
  "main": "dist/index.js",
6
- "typings": "dist/index.d.ts",
6
+ "typings": "index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "test": "node --require=ts-node/register ../../node_modules/.bin/jest",
@@ -14,20 +14,20 @@
14
14
  "author": "czy88840616@gmail.com",
15
15
  "files": [
16
16
  "dist/**/*.js",
17
- "dist/**/*.d.ts"
17
+ "dist/**/*.d.ts",
18
+ "index.d.ts"
18
19
  ],
19
20
  "engines": {
20
21
  "node": ">=12"
21
22
  },
22
23
  "license": "MIT",
23
24
  "dependencies": {
24
- "class-transformer": "^0.3.1",
25
25
  "joi": "^17.2.1"
26
26
  },
27
27
  "devDependencies": {
28
- "@midwayjs/core": "^3.0.0-beta.2",
29
- "@midwayjs/decorator": "^3.0.0-beta.2",
30
- "@midwayjs/mock": "^3.0.0-beta.2"
28
+ "@midwayjs/core": "^3.0.0-beta.6",
29
+ "@midwayjs/decorator": "^3.0.0-beta.6",
30
+ "@midwayjs/mock": "^3.0.0-beta.6"
31
31
  },
32
- "gitHead": "7f07de960da1155a9f7df554e1789c7a97bdd3fe"
32
+ "gitHead": "e4595d30b369e36bef21b36f2b3737d8bc2f802d"
33
33
  }