@midwayjs/validate 3.0.0-beta.1 → 3.0.0-beta.5

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,45 @@
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.5](https://github.com/midwayjs/midway/compare/v3.0.0-beta.4...v3.0.0-beta.5) (2021-11-25)
7
+
8
+ **Note:** Version bump only for package @midwayjs/validate
9
+
10
+
11
+
12
+
13
+
14
+ # [3.0.0-beta.4](https://github.com/midwayjs/midway/compare/v3.0.0-beta.3...v3.0.0-beta.4) (2021-11-24)
15
+
16
+
17
+ ### Features
18
+
19
+ * add i18n ([#1375](https://github.com/midwayjs/midway/issues/1375)) ([bffefe0](https://github.com/midwayjs/midway/commit/bffefe07afe45777d49b5a76b9ab17fc2b9d9a55))
20
+ * auto transform args to type ([#1372](https://github.com/midwayjs/midway/issues/1372)) ([bb3f7d2](https://github.com/midwayjs/midway/commit/bb3f7d2028a034e1926d9df554849332354c3762))
21
+
22
+
23
+
24
+
25
+
26
+ # [3.0.0-beta.3](https://github.com/midwayjs/midway/compare/v3.0.0-beta.2...v3.0.0-beta.3) (2021-11-18)
27
+
28
+
29
+ ### Features
30
+
31
+ * add component and framework config definition ([#1367](https://github.com/midwayjs/midway/issues/1367)) ([b2fe615](https://github.com/midwayjs/midway/commit/b2fe6157f99659471ff1333eca0b86bb889f61a3))
32
+
33
+
34
+
35
+
36
+
37
+ # [3.0.0-beta.2](https://github.com/midwayjs/midway/compare/v3.0.0-beta.1...v3.0.0-beta.2) (2021-11-16)
38
+
39
+ **Note:** Version bump only for package @midwayjs/validate
40
+
41
+
42
+
43
+
44
+
6
45
  # [3.0.0-beta.1](https://github.com/midwayjs/midway/compare/v2.12.4...v3.0.0-beta.1) (2021-11-14)
7
46
 
8
47
  **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,14 @@
1
1
  import { MidwayDecoratorService } from '@midwayjs/core';
2
+ import * as DefaultConfig from './config.default';
3
+ export declare class ValidateService {
4
+ validateConfig: typeof DefaultConfig.validate;
5
+ validate(ClzType: new (...args: any[]) => any, value: any, options?: {
6
+ errorStatus?: number;
7
+ }): void;
8
+ }
2
9
  export declare class ValidateConfiguration {
3
10
  decoratorService: MidwayDecoratorService;
11
+ validateService: ValidateService;
4
12
  init(): Promise<void>;
5
13
  }
6
14
  //# sourceMappingURL=configuration.d.ts.map
@@ -9,14 +9,36 @@ 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
+ }
31
+ }
32
+ };
33
+ __decorate([
34
+ (0, decorator_1.Config)('validate'),
35
+ __metadata("design:type", Object)
36
+ ], ValidateService.prototype, "validateConfig", void 0);
37
+ ValidateService = __decorate([
38
+ (0, decorator_1.Provide)(),
39
+ (0, decorator_1.Scope)(decorator_1.ScopeEnum.Singleton)
40
+ ], ValidateService);
41
+ exports.ValidateService = ValidateService;
20
42
  let ValidateConfiguration = class ValidateConfiguration {
21
43
  async init() {
22
44
  debug(`[validate]: Register @validate "${constants_1.VALIDATE_KEY}" handler"`);
@@ -28,21 +50,7 @@ let ValidateConfiguration = class ValidateConfiguration {
28
50
  before: (joinPoint) => {
29
51
  for (let i = 0; i < paramTypes.length; i++) {
30
52
  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
- }
45
- }
53
+ this.validateService.validate(item, joinPoint.args[i], options.metadata);
46
54
  }
47
55
  },
48
56
  };
@@ -53,6 +61,10 @@ __decorate([
53
61
  (0, decorator_1.Inject)(),
54
62
  __metadata("design:type", core_1.MidwayDecoratorService)
55
63
  ], ValidateConfiguration.prototype, "decoratorService", void 0);
64
+ __decorate([
65
+ (0, decorator_1.Inject)(),
66
+ __metadata("design:type", ValidateService)
67
+ ], ValidateConfiguration.prototype, "validateService", void 0);
56
68
  __decorate([
57
69
  (0, decorator_1.Init)(),
58
70
  __metadata("design:type", Function),
@@ -62,6 +74,11 @@ __decorate([
62
74
  ValidateConfiguration = __decorate([
63
75
  (0, decorator_1.Configuration)({
64
76
  namespace: 'validate',
77
+ importConfigs: [
78
+ {
79
+ default: DefaultConfig,
80
+ },
81
+ ],
65
82
  })
66
83
  ], ValidateConfiguration);
67
84
  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.1",
3
+ "version": "3.0.0-beta.5",
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.1",
29
- "@midwayjs/decorator": "^3.0.0-beta.1",
30
- "@midwayjs/mock": "^3.0.0-beta.1"
28
+ "@midwayjs/core": "^3.0.0-beta.5",
29
+ "@midwayjs/decorator": "^3.0.0-beta.5",
30
+ "@midwayjs/mock": "^3.0.0-beta.5"
31
31
  },
32
- "gitHead": "72e4d20c678e65a5440c4e9a6d3b70046daa497c"
32
+ "gitHead": "ab0bf05ae6d13f6435db2f7223202be61d585a1b"
33
33
  }