@midwayjs/validate 3.10.5 → 3.10.7

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.
@@ -1,4 +1,5 @@
1
1
  export declare const RULES_KEY = "common:rules";
2
+ export declare const RULES_CLASS_KEY = "common:class_rules";
2
3
  export declare const VALIDATE_KEY = "common:validate";
3
4
  export declare const VALID_KEY = "validate:valid";
4
5
  //# sourceMappingURL=constants.d.ts.map
package/dist/constants.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VALID_KEY = exports.VALIDATE_KEY = exports.RULES_KEY = void 0;
3
+ exports.VALID_KEY = exports.VALIDATE_KEY = exports.RULES_CLASS_KEY = exports.RULES_KEY = void 0;
4
4
  exports.RULES_KEY = 'common:rules';
5
+ exports.RULES_CLASS_KEY = 'common:class_rules';
5
6
  exports.VALIDATE_KEY = 'common:validate';
6
7
  exports.VALID_KEY = 'validate:valid';
7
8
  //# sourceMappingURL=constants.js.map
@@ -1,9 +1,16 @@
1
- import * as joi from 'joi';
1
+ import * as Joi from 'joi';
2
+ /**
3
+ * @deprecated
4
+ */
2
5
  export interface RuleOptions {
3
6
  required?: boolean;
4
7
  min?: number;
5
8
  max?: number;
6
9
  }
7
- export declare function Rule(rule: any, options?: RuleOptions): (...args: any[]) => void;
8
- export { joi as RuleType };
10
+ export declare function Rule(rule: Joi.AnySchema<any>): PropertyDecorator & ClassDecorator;
11
+ /**
12
+ * @deprecated
13
+ */
14
+ export declare function Rule(rule: new (...args: any[]) => any, options?: RuleOptions): PropertyDecorator & ClassDecorator;
15
+ export { Joi as RuleType };
9
16
  //# sourceMappingURL=rule.d.ts.map
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RuleType = exports.Rule = void 0;
4
- const joi = require("joi");
5
- exports.RuleType = joi;
4
+ const Joi = require("joi");
5
+ exports.RuleType = Joi;
6
6
  const core_1 = require("@midwayjs/core");
7
7
  const constants_1 = require("../constants");
8
8
  function Rule(rule, options = { required: true }) {
9
- return function (...args) {
10
- if (args[1]) {
11
- // 函数装饰器
12
- const [target, propertyKey] = args;
13
- if (!joi.isSchema(rule)) {
14
- rule = joi
15
- .object((0, core_1.getClassMetadata)(constants_1.RULES_KEY, rule))
16
- .meta({ id: rule.name });
9
+ return function (target, propertyKey) {
10
+ if (propertyKey) {
11
+ // property decorator
12
+ if (!Joi.isSchema(rule)) {
13
+ // 老代码,待废弃
14
+ rule = Joi.object((0, core_1.getClassMetadata)(constants_1.RULES_KEY, rule)).meta({
15
+ id: rule.name,
16
+ });
17
17
  if ((0, core_1.getPropertyType)(target, propertyKey).name === 'Array') {
18
- rule = joi.array().items(rule);
18
+ rule = Joi.array().items(rule);
19
19
  if (options.min) {
20
20
  rule = rule.min(options.min);
21
21
  }
@@ -30,17 +30,11 @@ function Rule(rule, options = { required: true }) {
30
30
  (0, core_1.attachClassMetadata)(constants_1.RULES_KEY, rule, target, propertyKey);
31
31
  }
32
32
  else {
33
- //类的装饰器
34
- const rules = (0, core_1.getClassMetadata)(constants_1.RULES_KEY, rule);
35
- if (rules) {
36
- let currentRule = (0, core_1.getClassMetadata)(constants_1.RULES_KEY, args[0]);
37
- currentRule = currentRule !== null && currentRule !== void 0 ? currentRule : {};
38
- Object.keys(rules).map(item => {
39
- if (!currentRule[item]) {
40
- currentRule[item] = rules[item];
41
- }
42
- });
43
- (0, core_1.saveClassMetadata)(constants_1.RULES_KEY, currentRule, args[0]);
33
+ // class decorator
34
+ if (Joi.isSchema(rule)) {
35
+ // TODO 下一个大版本,metadata 这里要完全重构,临时先加一个后缀
36
+ // mix schema with property
37
+ // saveClassMetadata(RULES_CLASS_KEY + '_EXT', rule, target);
44
38
  }
45
39
  }
46
40
  };
@@ -1,3 +1,4 @@
1
+ import { PipeUnionTransform } from '@midwayjs/core';
1
2
  import { AnySchema } from 'joi';
2
- export declare function Valid(schema?: AnySchema<any>): ParameterDecorator;
3
+ export declare function Valid(schemaOrPipes?: AnySchema<any> | PipeUnionTransform[], pipes?: PipeUnionTransform[]): ParameterDecorator;
3
4
  //# sourceMappingURL=valid.d.ts.map
@@ -4,11 +4,18 @@ exports.Valid = void 0;
4
4
  const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  const pipe_1 = require("../pipe");
7
- function Valid(schema) {
7
+ function Valid(schemaOrPipes, pipes) {
8
+ if (Array.isArray(schemaOrPipes)) {
9
+ pipes = schemaOrPipes;
10
+ schemaOrPipes = undefined;
11
+ }
12
+ else {
13
+ pipes = pipes || [];
14
+ }
8
15
  return (0, core_1.createCustomParamDecorator)(constants_1.VALID_KEY, {
9
- schema,
16
+ schema: schemaOrPipes,
10
17
  }, {
11
- pipes: [pipe_1.DecoratorValidPipe],
18
+ pipes: [pipe_1.DecoratorValidPipe, ...pipes],
12
19
  });
13
20
  }
14
21
  exports.Valid = Valid;
package/dist/pipe.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare abstract class AbstractValidationPipe implements PipeTransform {
13
13
  export declare class ValidationPipe extends AbstractValidationPipe {
14
14
  transform(value: any, options: TransformOptions): any;
15
15
  }
16
- declare abstract class ParsePipe extends AbstractValidationPipe {
16
+ export declare abstract class ParsePipe extends AbstractValidationPipe {
17
17
  transform(value: any, options: TransformOptions): any;
18
18
  }
19
19
  export declare class DecoratorValidPipe extends ParsePipe {
@@ -32,5 +32,4 @@ export declare class DefaultValuePipe<T = any, R = any> implements PipeTransform
32
32
  constructor(defaultValue: R);
33
33
  transform(value: any, options: any): any;
34
34
  }
35
- export {};
36
35
  //# sourceMappingURL=pipe.d.ts.map
package/dist/pipe.js CHANGED
@@ -9,7 +9,7 @@ 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.DefaultValuePipe = exports.ParseFloatPipe = exports.ParseBoolPipe = exports.ParseIntPipe = exports.DecoratorValidPipe = exports.ValidationPipe = exports.AbstractValidationPipe = void 0;
12
+ exports.DefaultValuePipe = exports.ParseFloatPipe = exports.ParseBoolPipe = exports.ParseIntPipe = exports.DecoratorValidPipe = exports.ParsePipe = exports.ValidationPipe = exports.AbstractValidationPipe = void 0;
13
13
  const core_1 = require("@midwayjs/core");
14
14
  const service_1 = require("./service");
15
15
  const i18n = require("@midwayjs/i18n");
@@ -69,6 +69,7 @@ class ParsePipe extends AbstractValidationPipe {
69
69
  return this.validateWithSchema(value, options, options.metadata['schema'] || this.getSchema());
70
70
  }
71
71
  }
72
+ exports.ParsePipe = ParsePipe;
72
73
  let DecoratorValidPipe = class DecoratorValidPipe extends ParsePipe {
73
74
  };
74
75
  DecoratorValidPipe = __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/validate",
3
- "version": "3.10.5",
3
+ "version": "3.10.7",
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.10.5",
26
+ "@midwayjs/i18n": "^3.10.7",
27
27
  "joi": "^17.2.1"
28
28
  },
29
29
  "devDependencies": {
30
- "@midwayjs/core": "^3.10.5",
31
- "@midwayjs/express": "^3.10.5",
32
- "@midwayjs/koa": "^3.10.5",
33
- "@midwayjs/mock": "^3.10.5"
30
+ "@midwayjs/core": "^3.10.7",
31
+ "@midwayjs/express": "^3.10.7",
32
+ "@midwayjs/koa": "^3.10.7",
33
+ "@midwayjs/mock": "^3.10.7"
34
34
  },
35
- "gitHead": "954706740b76dfabac9993a3cd6f4a504969a554"
35
+ "gitHead": "63bb5a11574dc6cae158c5de51fa5c5d5d482545"
36
36
  }