koatty_validation 1.2.8 → 1.2.9

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,8 @@
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.9](https://github.com/koatty/koatty_validation/compare/v1.2.8...v1.2.9) (2023-09-01)
6
+
5
7
  ### [1.2.8](https://github.com/koatty/koatty_validation/compare/v1.2.7...v1.2.8) (2023-02-17)
6
8
 
7
9
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-02-17 14:25:23
3
+ * @Date: 2023-09-01 11:55:18
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -355,7 +355,7 @@ export declare enum paramterTypes {
355
355
  }
356
356
 
357
357
  /**
358
- *
358
+ * plain object convert to class instance
359
359
  *
360
360
  * @export
361
361
  * @param {*} clazz
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-02-17 14:25:08
3
+ * @Date: 2023-09-01 11:54:57
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -55,7 +55,7 @@ function setExpose(object, propertyName) {
55
55
  }
56
56
  }
57
57
  /**
58
- *
58
+ * plain object convert to class instance
59
59
  *
60
60
  * @export
61
61
  * @param {*} clazz
@@ -65,22 +65,63 @@ function setExpose(object, propertyName) {
65
65
  */
66
66
  function plainToClass(clazz, data, convert = false) {
67
67
  if (helper__namespace.isClass(clazz)) {
68
- let cls;
69
68
  if (!helper__namespace.isObject(data)) {
70
69
  data = {};
71
70
  }
72
71
  if (data instanceof clazz) {
73
- cls = data;
74
- }
75
- else {
76
- cls = Object.assign(Reflect.construct(clazz, []), data);
72
+ return data;
77
73
  }
78
- if (convert) {
79
- return convertDtoParamsType(clazz, cls);
74
+ return assignDtoParams(clazz, data, convert);
75
+ }
76
+ return data;
77
+ }
78
+ /**
79
+ * assign dto params
80
+ * @param clazz
81
+ * @param data
82
+ * @param convert
83
+ * @returns
84
+ */
85
+ function assignDtoParams(clazz, data, convert = false) {
86
+ const cls = Reflect.construct(clazz, []);
87
+ if (convert) {
88
+ return convertAssignDtoParams(clazz, cls, data);
89
+ }
90
+ else {
91
+ for (const key in cls) {
92
+ if (Object.prototype.hasOwnProperty.call(data, key) &&
93
+ data[key] !== undefined) {
94
+ cls[key] = data[key];
95
+ }
80
96
  }
81
97
  return cls;
82
98
  }
83
- return data;
99
+ }
100
+ /**
101
+ * convert type and assign dto params
102
+ * @param clazz
103
+ * @param cls
104
+ * @param data
105
+ * @returns
106
+ */
107
+ function convertAssignDtoParams(clazz, cls, data) {
108
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
109
+ for (const key in cls) {
110
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
111
+ data[key] !== undefined) {
112
+ cls[key] = convertParamsType(data[key], cls._typeDef[key]);
113
+ }
114
+ }
115
+ }
116
+ else {
117
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
118
+ for (const [key, type] of originMap) {
119
+ if (key && data[key] !== undefined) {
120
+ cls[key] = convertParamsType(data[key], type);
121
+ }
122
+ }
123
+ }
124
+ return cls;
84
125
  }
85
126
  /**
86
127
  * convertDtoParamsType
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-02-17 14:25:08
3
+ * @Date: 2023-09-01 11:54:57
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -31,7 +31,7 @@ function setExpose(object, propertyName) {
31
31
  }
32
32
  }
33
33
  /**
34
- *
34
+ * plain object convert to class instance
35
35
  *
36
36
  * @export
37
37
  * @param {*} clazz
@@ -41,22 +41,63 @@ function setExpose(object, propertyName) {
41
41
  */
42
42
  function plainToClass(clazz, data, convert = false) {
43
43
  if (helper.isClass(clazz)) {
44
- let cls;
45
44
  if (!helper.isObject(data)) {
46
45
  data = {};
47
46
  }
48
47
  if (data instanceof clazz) {
49
- cls = data;
50
- }
51
- else {
52
- cls = Object.assign(Reflect.construct(clazz, []), data);
48
+ return data;
53
49
  }
54
- if (convert) {
55
- return convertDtoParamsType(clazz, cls);
50
+ return assignDtoParams(clazz, data, convert);
51
+ }
52
+ return data;
53
+ }
54
+ /**
55
+ * assign dto params
56
+ * @param clazz
57
+ * @param data
58
+ * @param convert
59
+ * @returns
60
+ */
61
+ function assignDtoParams(clazz, data, convert = false) {
62
+ const cls = Reflect.construct(clazz, []);
63
+ if (convert) {
64
+ return convertAssignDtoParams(clazz, cls, data);
65
+ }
66
+ else {
67
+ for (const key in cls) {
68
+ if (Object.prototype.hasOwnProperty.call(data, key) &&
69
+ data[key] !== undefined) {
70
+ cls[key] = data[key];
71
+ }
56
72
  }
57
73
  return cls;
58
74
  }
59
- return data;
75
+ }
76
+ /**
77
+ * convert type and assign dto params
78
+ * @param clazz
79
+ * @param cls
80
+ * @param data
81
+ * @returns
82
+ */
83
+ function convertAssignDtoParams(clazz, cls, data) {
84
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
85
+ for (const key in cls) {
86
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
87
+ data[key] !== undefined) {
88
+ cls[key] = convertParamsType(data[key], cls._typeDef[key]);
89
+ }
90
+ }
91
+ }
92
+ else {
93
+ const originMap = getOriginMetadata(PARAM_TYPE_KEY, clazz);
94
+ for (const [key, type] of originMap) {
95
+ if (key && data[key] !== undefined) {
96
+ cls[key] = convertParamsType(data[key], type);
97
+ }
98
+ }
99
+ }
100
+ return cls;
60
101
  }
61
102
  /**
62
103
  * convertDtoParamsType
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
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",
@@ -75,7 +75,7 @@
75
75
  "koatty_lib": "^1.x.x",
76
76
  "koatty_logger": "^2.x.x",
77
77
  "reflect-metadata": "^0.1.13",
78
- "tslib": "^2.5.0"
78
+ "tslib": "^2.6.2"
79
79
  },
80
80
  "peerDependencies": {
81
81
  "koatty_container": "^1.x.x",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koatty_validation",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
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",
@@ -75,7 +75,7 @@
75
75
  "koatty_lib": "^1.x.x",
76
76
  "koatty_logger": "^2.x.x",
77
77
  "reflect-metadata": "^0.1.13",
78
- "tslib": "^2.5.0"
78
+ "tslib": "^2.6.2"
79
79
  },
80
80
  "peerDependencies": {
81
81
  "koatty_container": "^1.x.x",