@midwayjs/validate 3.19.2 → 4.0.0-alpha.1

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.
@@ -43,6 +43,7 @@ let ValidateConfiguration = class ValidateConfiguration {
43
43
  ]);
44
44
  }
45
45
  };
46
+ exports.ValidateConfiguration = ValidateConfiguration;
46
47
  __decorate([
47
48
  (0, core_1.Inject)(),
48
49
  __metadata("design:type", core_1.MidwayDecoratorService)
@@ -57,7 +58,7 @@ __decorate([
57
58
  __metadata("design:paramtypes", []),
58
59
  __metadata("design:returntype", Promise)
59
60
  ], ValidateConfiguration.prototype, "init", null);
60
- ValidateConfiguration = __decorate([
61
+ exports.ValidateConfiguration = ValidateConfiguration = __decorate([
61
62
  (0, core_1.Configuration)({
62
63
  namespace: 'validate',
63
64
  imports: [i18n],
@@ -68,5 +69,4 @@ ValidateConfiguration = __decorate([
68
69
  ],
69
70
  })
70
71
  ], ValidateConfiguration);
71
- exports.ValidateConfiguration = ValidateConfiguration;
72
72
  //# sourceMappingURL=configuration.js.map
@@ -1,16 +1,4 @@
1
1
  import * as Joi from 'joi';
2
- /**
3
- * @deprecated
4
- */
5
- export interface RuleOptions {
6
- required?: boolean;
7
- min?: number;
8
- max?: number;
9
- }
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;
2
+ export declare function Rule(rule: Joi.AnySchema<any>): PropertyDecorator;
15
3
  export { Joi as RuleType };
16
4
  //# sourceMappingURL=rule.d.ts.map
@@ -5,37 +5,10 @@ const Joi = require("joi");
5
5
  exports.RuleType = Joi;
6
6
  const core_1 = require("@midwayjs/core");
7
7
  const constants_1 = require("../constants");
8
- function Rule(rule, options = { required: true }) {
8
+ function Rule(rule) {
9
9
  return function (target, propertyKey) {
10
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
- if ((0, core_1.getPropertyType)(target, propertyKey).name === 'Array') {
18
- rule = Joi.array().items(rule);
19
- if (options.min) {
20
- rule = rule.min(options.min);
21
- }
22
- if (options.max) {
23
- rule = rule.max(options.max);
24
- }
25
- }
26
- if (options.required) {
27
- rule = rule.required();
28
- }
29
- }
30
- (0, core_1.attachClassMetadata)(constants_1.RULES_KEY, rule, target, propertyKey);
31
- }
32
- else {
33
- // class decorator
34
- if (Joi.isSchema(rule)) {
35
- // TODO 下一个大版本,metadata 这里要完全重构,临时先加一个后缀
36
- // mix schema with property
37
- // saveClassMetadata(RULES_CLASS_KEY + '_EXT', rule, target);
38
- }
11
+ core_1.MetadataManager.defineMetadata(constants_1.RULES_KEY, rule, target, propertyKey);
39
12
  }
40
13
  };
41
14
  }
@@ -12,7 +12,7 @@ function Valid(schemaOrPipes, pipes) {
12
12
  else {
13
13
  pipes = pipes || [];
14
14
  }
15
- return (0, core_1.createCustomParamDecorator)(constants_1.VALID_KEY, {
15
+ return core_1.DecoratorManager.createCustomParamDecorator(constants_1.VALID_KEY, {
16
16
  schema: schemaOrPipes,
17
17
  }, {
18
18
  pipes: [pipe_1.DecoratorValidPipe, ...pipes],
@@ -5,7 +5,7 @@ const core_1 = require("@midwayjs/core");
5
5
  const constants_1 = require("../constants");
6
6
  function Validate(options = {}) {
7
7
  return (target, methodName, descriptor) => {
8
- (0, core_1.savePropertyMetadata)(constants_1.VALIDATE_KEY, options, target, methodName);
8
+ core_1.MetadataManager.defineMetadata(constants_1.VALIDATE_KEY, options, target, methodName);
9
9
  };
10
10
  }
11
11
  exports.Validate = Validate;
package/dist/dtoHelper.js CHANGED
@@ -6,26 +6,22 @@ const constants_1 = require("./constants");
6
6
  function PickDto(dto, keys) {
7
7
  const pickedDto = function () { };
8
8
  pickedDto.prototype = dto.prototype;
9
- const fatherRule = (0, core_1.getClassExtendedMetadata)(constants_1.RULES_KEY, dto);
10
- const pickedRule = {};
11
- for (const key of keys) {
12
- if (fatherRule[key]) {
13
- pickedRule[key] = fatherRule[key];
14
- }
15
- }
16
- (0, core_1.saveClassMetadata)(constants_1.RULES_KEY, pickedRule, pickedDto);
9
+ core_1.MetadataManager.copyMetadata(dto, pickedDto, {
10
+ metadataFilter: (key, propertyName) => {
11
+ return key === constants_1.RULES_KEY && keys.includes(propertyName);
12
+ },
13
+ });
17
14
  return pickedDto;
18
15
  }
19
16
  exports.PickDto = PickDto;
20
17
  function OmitDto(dto, keys) {
21
18
  const pickedDto = function () { };
22
19
  pickedDto.prototype = dto.prototype;
23
- const fatherRule = (0, core_1.getClassExtendedMetadata)(constants_1.RULES_KEY, dto);
24
- const pickedRule = Object.assign({}, fatherRule);
25
- for (const key of keys) {
26
- delete pickedRule[key];
27
- }
28
- (0, core_1.saveClassMetadata)(constants_1.RULES_KEY, pickedRule, pickedDto);
20
+ core_1.MetadataManager.copyMetadata(dto, pickedDto, {
21
+ metadataFilter: (key, propertyName) => {
22
+ return key === constants_1.RULES_KEY && !keys.includes(propertyName);
23
+ },
24
+ });
29
25
  return pickedDto;
30
26
  }
31
27
  exports.OmitDto = OmitDto;
package/dist/pipe.js CHANGED
@@ -36,8 +36,7 @@ class AbstractValidationPipe {
36
36
  return value;
37
37
  }
38
38
  parseValidationOptions(options) {
39
- const validateOptions = (0, core_1.getPropertyMetadata)(constants_1.VALIDATE_KEY, options.target, options.methodName) ||
40
- {};
39
+ const validateOptions = core_1.MetadataManager.getMetadata(constants_1.VALIDATE_KEY, options.target, options.methodName) || {};
41
40
  if (!validateOptions.locale) {
42
41
  const maybeCtx = options.target[core_1.REQUEST_OBJ_CTX_KEY];
43
42
  if (maybeCtx && maybeCtx.getAttr) {
@@ -50,20 +49,20 @@ class AbstractValidationPipe {
50
49
  return undefined;
51
50
  }
52
51
  }
52
+ exports.AbstractValidationPipe = AbstractValidationPipe;
53
53
  __decorate([
54
54
  (0, core_1.Inject)(),
55
55
  __metadata("design:type", service_1.ValidateService)
56
56
  ], AbstractValidationPipe.prototype, "validateService", void 0);
57
- exports.AbstractValidationPipe = AbstractValidationPipe;
58
57
  let ValidationPipe = class ValidationPipe extends AbstractValidationPipe {
59
58
  transform(value, options) {
60
59
  return this.validate(value, options);
61
60
  }
62
61
  };
63
- ValidationPipe = __decorate([
62
+ exports.ValidationPipe = ValidationPipe;
63
+ exports.ValidationPipe = ValidationPipe = __decorate([
64
64
  (0, core_1.Pipe)()
65
65
  ], ValidationPipe);
66
- exports.ValidationPipe = ValidationPipe;
67
66
  class ParsePipe extends AbstractValidationPipe {
68
67
  transform(value, options) {
69
68
  return this.validateWithSchema(value, options, options.metadata['schema'] || this.getSchema());
@@ -72,37 +71,37 @@ class ParsePipe extends AbstractValidationPipe {
72
71
  exports.ParsePipe = ParsePipe;
73
72
  let DecoratorValidPipe = class DecoratorValidPipe extends ParsePipe {
74
73
  };
75
- DecoratorValidPipe = __decorate([
74
+ exports.DecoratorValidPipe = DecoratorValidPipe;
75
+ exports.DecoratorValidPipe = DecoratorValidPipe = __decorate([
76
76
  (0, core_1.Pipe)()
77
77
  ], DecoratorValidPipe);
78
- exports.DecoratorValidPipe = DecoratorValidPipe;
79
78
  let ParseIntPipe = class ParseIntPipe extends ParsePipe {
80
79
  getSchema() {
81
80
  return Joi.number().integer().required();
82
81
  }
83
82
  };
84
- ParseIntPipe = __decorate([
83
+ exports.ParseIntPipe = ParseIntPipe;
84
+ exports.ParseIntPipe = ParseIntPipe = __decorate([
85
85
  (0, core_1.Pipe)()
86
86
  ], ParseIntPipe);
87
- exports.ParseIntPipe = ParseIntPipe;
88
87
  let ParseBoolPipe = class ParseBoolPipe extends ParsePipe {
89
88
  getSchema() {
90
89
  return Joi.boolean().required();
91
90
  }
92
91
  };
93
- ParseBoolPipe = __decorate([
92
+ exports.ParseBoolPipe = ParseBoolPipe;
93
+ exports.ParseBoolPipe = ParseBoolPipe = __decorate([
94
94
  (0, core_1.Pipe)()
95
95
  ], ParseBoolPipe);
96
- exports.ParseBoolPipe = ParseBoolPipe;
97
96
  let ParseFloatPipe = class ParseFloatPipe extends ParsePipe {
98
97
  getSchema() {
99
98
  return Joi.number().required();
100
99
  }
101
100
  };
102
- ParseFloatPipe = __decorate([
101
+ exports.ParseFloatPipe = ParseFloatPipe;
102
+ exports.ParseFloatPipe = ParseFloatPipe = __decorate([
103
103
  (0, core_1.Pipe)()
104
104
  ], ParseFloatPipe);
105
- exports.ParseFloatPipe = ParseFloatPipe;
106
105
  class DefaultValuePipe {
107
106
  constructor(defaultValue) {
108
107
  this.defaultValue = defaultValue;
package/dist/service.js CHANGED
@@ -31,7 +31,6 @@ let ValidateService = class ValidateService {
31
31
  return this.validateWithSchema(objectSchema, value, options);
32
32
  }
33
33
  validateWithSchema(schema, value, options = {}) {
34
- var _a, _b, _c;
35
34
  if (!schema) {
36
35
  return undefined;
37
36
  }
@@ -40,11 +39,11 @@ let ValidateService = class ValidateService {
40
39
  options.validationOptions.errors.language = (0, i18n_1.formatLocale)(this.i18nService.getAvailableLocale(options.validationOptions.errors.language ||
41
40
  options.locale ||
42
41
  this.i18nConfig.defaultLocale, 'validate'));
43
- const result = schema.validate(value, Object.assign({}, (_a = this.validateConfig.validationOptions) !== null && _a !== void 0 ? _a : {}, {
42
+ const result = schema.validate(value, Object.assign({}, this.validateConfig.validationOptions ?? {}, {
44
43
  messages: this.messages,
45
- }, (_b = options.validationOptions) !== null && _b !== void 0 ? _b : {}));
44
+ }, options.validationOptions ?? {}));
46
45
  if (result.error) {
47
- 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);
46
+ throw new error_1.MidwayValidationError(result.error.message, options?.errorStatus ?? this.validateConfig.errorStatus, result.error);
48
47
  }
49
48
  else {
50
49
  return result;
@@ -54,6 +53,7 @@ let ValidateService = class ValidateService {
54
53
  return getSchema(ClzType);
55
54
  }
56
55
  };
56
+ exports.ValidateService = ValidateService;
57
57
  __decorate([
58
58
  (0, core_1.Config)('validate'),
59
59
  __metadata("design:type", Object)
@@ -72,13 +72,12 @@ __decorate([
72
72
  __metadata("design:paramtypes", []),
73
73
  __metadata("design:returntype", Promise)
74
74
  ], ValidateService.prototype, "init", null);
75
- ValidateService = __decorate([
75
+ exports.ValidateService = ValidateService = __decorate([
76
76
  (0, core_1.Provide)(),
77
77
  (0, core_1.Scope)(core_1.ScopeEnum.Singleton)
78
78
  ], ValidateService);
79
- exports.ValidateService = ValidateService;
80
79
  function getSchema(ClzType) {
81
- const rules = (0, core_1.getClassExtendedMetadata)(constants_1.RULES_KEY, ClzType);
80
+ const rules = core_1.MetadataManager.getPropertiesWithMetadata(constants_1.RULES_KEY, ClzType);
82
81
  if (rules) {
83
82
  return Joi.object(rules);
84
83
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@midwayjs/validate",
3
- "version": "3.19.2",
3
+ "version": "4.0.0-alpha.1",
4
4
  "description": "Midway Component for mongoose",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
- "test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
10
- "cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
9
+ "test": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand",
10
+ "cov": "node -r ts-node/register ../../node_modules/jest/bin/jest.js --runInBand --coverage --forceExit",
11
11
  "ci": "npm run test"
12
12
  },
13
13
  "keywords": [],
@@ -23,14 +23,14 @@
23
23
  },
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
- "@midwayjs/i18n": "^3.19.2",
26
+ "@midwayjs/i18n": "^4.0.0-alpha.1",
27
27
  "joi": "17.13.3"
28
28
  },
29
29
  "devDependencies": {
30
- "@midwayjs/core": "^3.19.0",
31
- "@midwayjs/express": "^3.19.2",
32
- "@midwayjs/koa": "^3.19.2",
33
- "@midwayjs/mock": "^3.19.2"
30
+ "@midwayjs/core": "^4.0.0-alpha.1",
31
+ "@midwayjs/express": "^4.0.0-alpha.1",
32
+ "@midwayjs/koa": "^4.0.0-alpha.1",
33
+ "@midwayjs/mock": "^4.0.0-alpha.1"
34
34
  },
35
- "gitHead": "57fd034be94897fb819b0d9ef776de0b9923ab0f"
35
+ "gitHead": "14bb4da91805a1cf52f190c0d37a74b395dd6372"
36
36
  }