koatty_validation 1.2.4 → 1.2.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
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.2.5](https://github.com/koatty/koatty_validation/compare/v1.2.4...v1.2.5) (2022-08-19)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * merge ([52c65f4](https://github.com/koatty/koatty_validation/commit/52c65f41c46a02a3fc5735e6e22ee6a3ed5337f8))
11
+
5
12
  ### [1.2.4](https://github.com/koatty/koatty_validation/compare/v1.2.3...v1.2.4) (2022-05-27)
6
13
 
7
14
  ### [1.2.3](https://github.com/koatty/koatty_validation/compare/v1.2.2...v1.2.3) (2022-03-09)
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-05-27 09:55:28
3
+ * @Date: 2022-08-19 14:24:55
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -38,8 +38,9 @@ export declare function Contains(seed: string, validationOptions?: ValidationOpt
38
38
  *
39
39
  * @param {*} clazz
40
40
  * @param {*} cls
41
- * @param {*} data
42
- * @returns {*}
41
+ * @param {boolean} convert
42
+ * @param {boolean} data
43
+ * @returns {*} cls
43
44
  */
44
45
  export declare function convertDtoParamsType(clazz: any, cls: any, data: any): any;
45
46
 
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-05-27 09:55:14
3
+ * @Date: 2022-08-19 14:24:42
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -15,21 +15,21 @@ var koatty_container = require('koatty_container');
15
15
  var classValidator = require('class-validator');
16
16
 
17
17
  function _interopNamespace(e) {
18
- if (e && e.__esModule) return e;
19
- var n = Object.create(null);
20
- if (e) {
21
- Object.keys(e).forEach(function (k) {
22
- if (k !== 'default') {
23
- var d = Object.getOwnPropertyDescriptor(e, k);
24
- Object.defineProperty(n, k, d.get ? d : {
25
- enumerable: true,
26
- get: function () { return e[k]; }
27
- });
28
- }
18
+ if (e && e.__esModule) return e;
19
+ var n = Object.create(null);
20
+ if (e) {
21
+ Object.keys(e).forEach(function (k) {
22
+ if (k !== 'default') {
23
+ var d = Object.getOwnPropertyDescriptor(e, k);
24
+ Object.defineProperty(n, k, d.get ? d : {
25
+ enumerable: true,
26
+ get: function () { return e[k]; }
29
27
  });
30
- }
31
- n["default"] = e;
32
- return Object.freeze(n);
28
+ }
29
+ });
30
+ }
31
+ n["default"] = e;
32
+ return Object.freeze(n);
33
33
  }
34
34
 
35
35
  var helper__namespace = /*#__PURE__*/_interopNamespace(helper);
@@ -73,36 +73,73 @@ function plainToClass(clazz, data, convert = false) {
73
73
  cls = data;
74
74
  }
75
75
  else {
76
- cls = Reflect.construct(clazz, []);
76
+ cls = Object.assign(Reflect.construct(clazz, []), data);
77
77
  }
78
- if (convert) {
79
- return convertDtoParamsType(clazz, cls, data);
80
- }
81
- return Object.assign(cls, data);
78
+ return convertOrCheckDtoParamsType(clazz, cls, convert);
82
79
  }
83
80
  return data;
84
81
  }
82
+ /**
83
+ * convertOrCheckDtoParamsType
84
+ *
85
+ * @param {*} clazz
86
+ * @param {*} cls
87
+ * @param {boolean} convert
88
+ * @returns {*} cls
89
+ */
90
+ function convertOrCheckDtoParamsType(clazz, cls, convert) {
91
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
92
+ for (const key in cls) {
93
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
94
+ if (convert) {
95
+ cls[key] = convertParamsType(cls[key], cls._typeDef[key]);
96
+ }
97
+ else {
98
+ if (!checkParamsType(cls[key], cls._typeDef[key])) {
99
+ throw new Error("invalid arguments type");
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ else {
106
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
107
+ for (const [key, type] of originMap) {
108
+ if (key) {
109
+ if (convert) {
110
+ cls[key] = convertParamsType(cls[key], type);
111
+ }
112
+ else {
113
+ if (!checkParamsType(cls[key], type)) {
114
+ throw new Error("invalid arguments type");
115
+ }
116
+ }
117
+ }
118
+ }
119
+ }
120
+ return cls;
121
+ }
85
122
  /**
86
123
  * convertDtoParamsType
87
124
  *
88
125
  * @param {*} clazz
89
126
  * @param {*} cls
90
- * @param {*} data
91
- * @returns {*}
127
+ * @param {boolean} convert
128
+ * @param {boolean} data
129
+ * @returns {*} cls
92
130
  */
93
131
  function convertDtoParamsType(clazz, cls, data) {
94
132
  if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
95
133
  for (const key in cls) {
96
- if (Object.prototype.hasOwnProperty.call(data, key)
97
- && Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
98
- data[key] = convertParamsType(data[key], cls._typeDef[key]);
134
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
135
+ cls[key] = convertParamsType(data[key], cls._typeDef[key]);
99
136
  }
100
137
  }
101
138
  }
102
139
  else {
103
140
  const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
104
141
  for (const [key, type] of originMap) {
105
- if (key && Object.prototype.hasOwnProperty.call(data, key)) {
142
+ if (key) {
106
143
  cls[key] = convertParamsType(data[key], type);
107
144
  }
108
145
  }
@@ -314,7 +351,7 @@ function plateNumber(value) {
314
351
  * @Usage:
315
352
  * @Author: richen
316
353
  * @Date: 2021-11-25 10:47:04
317
- * @LastEditTime: 2022-03-09 18:23:18
354
+ * @LastEditTime: 2022-08-19 14:21:20
318
355
  */
319
356
  // constant
320
357
  const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
@@ -640,7 +677,7 @@ Object.keys(ValidFuncs).forEach((key) => {
640
677
  * @Usage:
641
678
  * @Author: richen
642
679
  * @Date: 2021-11-25 10:46:57
643
- * @LastEditTime: 2022-03-02 11:23:27
680
+ * @LastEditTime: 2022-08-19 14:21:12
644
681
  */
645
682
  /**
646
683
  * Validation parameter's type and values.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2022-05-27 09:55:14
3
+ * @Date: 2022-08-19 14:24:42
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -49,36 +49,73 @@ function plainToClass(clazz, data, convert = false) {
49
49
  cls = data;
50
50
  }
51
51
  else {
52
- cls = Reflect.construct(clazz, []);
52
+ cls = Object.assign(Reflect.construct(clazz, []), data);
53
53
  }
54
- if (convert) {
55
- return convertDtoParamsType(clazz, cls, data);
56
- }
57
- return Object.assign(cls, data);
54
+ return convertOrCheckDtoParamsType(clazz, cls, convert);
58
55
  }
59
56
  return data;
60
57
  }
58
+ /**
59
+ * convertOrCheckDtoParamsType
60
+ *
61
+ * @param {*} clazz
62
+ * @param {*} cls
63
+ * @param {boolean} convert
64
+ * @returns {*} cls
65
+ */
66
+ function convertOrCheckDtoParamsType(clazz, cls, convert) {
67
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
68
+ for (const key in cls) {
69
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
70
+ if (convert) {
71
+ cls[key] = convertParamsType(cls[key], cls._typeDef[key]);
72
+ }
73
+ else {
74
+ if (!checkParamsType(cls[key], cls._typeDef[key])) {
75
+ throw new Error("invalid arguments type");
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ else {
82
+ const originMap = getOriginMetadata(PARAM_TYPE_KEY, clazz);
83
+ for (const [key, type] of originMap) {
84
+ if (key) {
85
+ if (convert) {
86
+ cls[key] = convertParamsType(cls[key], type);
87
+ }
88
+ else {
89
+ if (!checkParamsType(cls[key], type)) {
90
+ throw new Error("invalid arguments type");
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
96
+ return cls;
97
+ }
61
98
  /**
62
99
  * convertDtoParamsType
63
100
  *
64
101
  * @param {*} clazz
65
102
  * @param {*} cls
66
- * @param {*} data
67
- * @returns {*}
103
+ * @param {boolean} convert
104
+ * @param {boolean} data
105
+ * @returns {*} cls
68
106
  */
69
107
  function convertDtoParamsType(clazz, cls, data) {
70
108
  if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
71
109
  for (const key in cls) {
72
- if (Object.prototype.hasOwnProperty.call(data, key)
73
- && Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
74
- data[key] = convertParamsType(data[key], cls._typeDef[key]);
110
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key)) {
111
+ cls[key] = convertParamsType(data[key], cls._typeDef[key]);
75
112
  }
76
113
  }
77
114
  }
78
115
  else {
79
116
  const originMap = getOriginMetadata(PARAM_TYPE_KEY, clazz);
80
117
  for (const [key, type] of originMap) {
81
- if (key && Object.prototype.hasOwnProperty.call(data, key)) {
118
+ if (key) {
82
119
  cls[key] = convertParamsType(data[key], type);
83
120
  }
84
121
  }
@@ -290,7 +327,7 @@ function plateNumber(value) {
290
327
  * @Usage:
291
328
  * @Author: richen
292
329
  * @Date: 2021-11-25 10:47:04
293
- * @LastEditTime: 2022-03-09 18:23:18
330
+ * @LastEditTime: 2022-08-19 14:21:20
294
331
  */
295
332
  // constant
296
333
  const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
@@ -616,7 +653,7 @@ Object.keys(ValidFuncs).forEach((key) => {
616
653
  * @Usage:
617
654
  * @Author: richen
618
655
  * @Date: 2021-11-25 10:46:57
619
- * @LastEditTime: 2022-03-02 11:23:27
656
+ * @LastEditTime: 2022-08-19 14:21:12
620
657
  */
621
658
  /**
622
659
  * Validation parameter's type and values.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "Validation Util for Koatty and ThinkORM.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -81,4 +81,4 @@
81
81
  "koatty_lib": "^1.x.x",
82
82
  "koatty_logger": "^1.x.x"
83
83
  }
84
- }
84
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "Validation Util for Koatty and ThinkORM.",
5
5
  "scripts": {
6
6
  "build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
@@ -81,4 +81,4 @@
81
81
  "koatty_lib": "^1.x.x",
82
82
  "koatty_logger": "^1.x.x"
83
83
  }
84
- }
84
+ }