koatty_validation 1.2.9 → 1.2.10

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/dist/index.js CHANGED
@@ -1,21 +1,18 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2023-09-01 11:54:57
3
+ * @Date: 2023-12-16 14:42:53
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
8
  'use strict';
9
9
 
10
- Object.defineProperty(exports, '__esModule', { value: true });
11
-
12
10
  var helper = require('koatty_lib');
13
11
  require('reflect-metadata');
14
12
  var koatty_container = require('koatty_container');
15
13
  var classValidator = require('class-validator');
16
14
 
17
- function _interopNamespace(e) {
18
- if (e && e.__esModule) return e;
15
+ function _interopNamespaceDefault(e) {
19
16
  var n = Object.create(null);
20
17
  if (e) {
21
18
  Object.keys(e).forEach(function (k) {
@@ -28,1358 +25,1359 @@ function _interopNamespace(e) {
28
25
  }
29
26
  });
30
27
  }
31
- n["default"] = e;
28
+ n.default = e;
32
29
  return Object.freeze(n);
33
30
  }
34
31
 
35
- var helper__namespace = /*#__PURE__*/_interopNamespace(helper);
32
+ var helper__namespace = /*#__PURE__*/_interopNamespaceDefault(helper);
36
33
 
37
- /**
38
- * @ author: richen
39
- * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
40
- * @ license: MIT
41
- * @ version: 2020-03-20 11:34:38
42
- */
43
- /**
44
- * Set property as included in the process of transformation.
45
- *
46
- * @export
47
- * @param {Object} object
48
- * @param {(string | symbol)} propertyName
49
- */
50
- function setExpose(object, propertyName) {
51
- const types = Reflect.getMetadata("design:type", object, propertyName);
52
- if (types) {
53
- const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, object);
54
- originMap.set(propertyName, types.name);
55
- }
56
- }
57
- /**
58
- * plain object convert to class instance
59
- *
60
- * @export
61
- * @param {*} clazz
62
- * @param {*} data
63
- * @param {boolean} [convert=false]
64
- * @returns
65
- */
66
- function plainToClass(clazz, data, convert = false) {
67
- if (helper__namespace.isClass(clazz)) {
68
- if (!helper__namespace.isObject(data)) {
69
- data = {};
70
- }
71
- if (data instanceof clazz) {
72
- return data;
73
- }
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
- }
96
- }
97
- return cls;
98
- }
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;
125
- }
126
- /**
127
- * convertDtoParamsType
128
- *
129
- * @param {*} clazz
130
- * @param {*} cls
131
- * @returns {*} cls
132
- */
133
- function convertDtoParamsType(clazz, cls) {
134
- if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
135
- for (const key in cls) {
136
- if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
137
- cls[key] !== undefined) {
138
- cls[key] = convertParamsType(cls[key], cls._typeDef[key]);
139
- }
140
- }
141
- }
142
- else {
143
- const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
144
- for (const [key, type] of originMap) {
145
- if (key && cls[key] !== undefined) {
146
- cls[key] = convertParamsType(cls[key], type);
147
- }
148
- }
149
- }
150
- return cls;
151
- }
152
- /**
153
- * 绑定参数类型转换
154
- *
155
- * @param {*} param
156
- * @param {string} type
157
- * @returns {*}
158
- */
159
- function convertParamsType(param, type) {
160
- try {
161
- switch (type) {
162
- case "Number":
163
- case "number":
164
- if (helper__namespace.isNaN(param)) {
165
- return NaN;
166
- }
167
- if (helper__namespace.isNumber(param)) {
168
- return param;
169
- }
170
- if (helper__namespace.isNumberString(param)) {
171
- return helper__namespace.toNumber(param);
172
- }
173
- return NaN;
174
- case "Boolean":
175
- case "boolean":
176
- return !!param;
177
- case "Array":
178
- case "array":
179
- case "Tuple":
180
- case "tuple":
181
- if (helper__namespace.isArray(param)) {
182
- return param;
183
- }
184
- return helper__namespace.toArray(param);
185
- case "String":
186
- case "string":
187
- if (helper__namespace.isString(param)) {
188
- return param;
189
- }
190
- return helper__namespace.toString(param);
191
- case "Null":
192
- case "null":
193
- return null;
194
- case "Undefined":
195
- case "undefined":
196
- return undefined;
197
- case "Bigint":
198
- case "bigint":
199
- if (typeof param === 'bigint') {
200
- return param;
201
- }
202
- return BigInt(param);
203
- // case "object":
204
- // case "enum":
205
- default: //any
206
- return param;
207
- }
208
- }
209
- catch (err) {
210
- return param;
211
- }
212
- }
213
- /**
214
- * Check the base types.
215
- *
216
- * @param {*} value
217
- * @param {string} type
218
- * @returns {*}
219
- */
220
- function checkParamsType(value, type) {
221
- switch (type) {
222
- case "Number":
223
- case "number":
224
- if (!helper__namespace.isNumber(value) || helper__namespace.isNaN(value)) {
225
- return false;
226
- }
227
- return true;
228
- case "Boolean":
229
- case "boolean":
230
- if (!helper__namespace.isBoolean(value)) {
231
- return false;
232
- }
233
- return true;
234
- case "Array":
235
- case "array":
236
- case "Tuple":
237
- case "tuple":
238
- if (!helper__namespace.isArray(value)) {
239
- return false;
240
- }
241
- return true;
242
- case "String":
243
- case "string":
244
- if (!helper__namespace.isString(value)) {
245
- return false;
246
- }
247
- return true;
248
- case "Object":
249
- case "object":
250
- case "Enum":
251
- case "enum":
252
- if (helper__namespace.isTrueEmpty(value)) {
253
- return false;
254
- }
255
- return true;
256
- case "Null":
257
- case "null":
258
- if (!helper__namespace.isNull(value)) {
259
- return false;
260
- }
261
- return true;
262
- case "Undefined":
263
- case "undefined":
264
- if (!helper__namespace.isUndefined(value)) {
265
- return false;
266
- }
267
- return true;
268
- case "Bigint":
269
- case "bigint":
270
- if (typeof value !== 'bigint') {
271
- return false;
272
- }
273
- return true;
274
- default: //any
275
- return true;
276
- }
277
- }
278
- /**
279
- * Checks if value is a chinese name.
280
- *
281
- * @param {string} value
282
- * @returns {boolean}
283
- */
284
- function cnName(value) {
285
- const reg = /^([a-zA-Z0-9\u4e00-\u9fa5\·]{1,10})$/;
286
- return reg.test(value);
287
- }
288
- /**
289
- * Checks if value is a idCard number.
290
- *
291
- * @param {string} value
292
- * @returns
293
- */
294
- function idNumber(value) {
295
- if (/^\d{15}$/.test(value)) {
296
- return true;
297
- }
298
- if ((/^\d{17}[0-9X]$/).test(value)) {
299
- const vs = '1,0,x,9,8,7,6,5,4,3,2'.split(',');
300
- const ps = '7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'.split(',');
301
- const ss = value.toLowerCase().split('');
302
- let r = 0;
303
- for (let i = 0; i < 17; i++) {
304
- r += ps[i] * ss[i];
305
- }
306
- const isOk = (vs[r % 11] === ss[17]);
307
- return isOk;
308
- }
309
- return false;
310
- }
311
- /**
312
- * Checks if value is a mobile phone number.
313
- *
314
- * @param {string} value
315
- * @returns {boolean}
316
- */
317
- function mobile(value) {
318
- const reg = /^(13|14|15|16|17|18|19)\d{9}$/;
319
- return reg.test(value);
320
- }
321
- /**
322
- * Checks if value is a zipCode.
323
- *
324
- * @param {string} value
325
- * @returns {boolean}
326
- */
327
- function zipCode(value) {
328
- const reg = /^\d{6}$/;
329
- return reg.test(value);
330
- }
331
- /**
332
- * Checks if value is a plateNumber.
333
- *
334
- * @param {string} value
335
- * @returns {boolean}
336
- */
337
- function plateNumber(value) {
338
- // let reg = new RegExp('^(([\u4e00-\u9fa5][a-zA-Z]|[\u4e00-\u9fa5]{2}\d{2}|[\u4e00-\u9fa5]{2}[a-zA-Z])[-]?|([wW][Jj][\u4e00-\u9fa5]{1}[-]?)|([a-zA-Z]{2}))([A-Za-z0-9]{5}|[DdFf][A-HJ-NP-Za-hj-np-z0-9][0-9]{4}|[0-9]{5}[DdFf])$');
339
- // let xReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
340
- const xReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
341
- // let cReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
342
- const cReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
343
- if (value.length === 7) {
344
- return cReg.test(value);
345
- }
346
- else {
347
- //新能源车牌
348
- return xReg.test(value);
349
- }
34
+ /**
35
+ * @ author: richen
36
+ * @ copyright: Copyright (c) - <richenlin(at)gmail.com>
37
+ * @ license: MIT
38
+ * @ version: 2020-03-20 11:34:38
39
+ */
40
+ // tslint:disable-next-line: no-import-side-effect
41
+ /**
42
+ * Set property as included in the process of transformation.
43
+ *
44
+ * @export
45
+ * @param {Object} object
46
+ * @param {(string | symbol)} propertyName
47
+ */
48
+ function setExpose(object, propertyName) {
49
+ const types = Reflect.getMetadata("design:type", object, propertyName);
50
+ if (types) {
51
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, object);
52
+ originMap.set(propertyName, types.name);
53
+ }
54
+ }
55
+ /**
56
+ * plain object convert to class instance
57
+ *
58
+ * @export
59
+ * @param {*} clazz
60
+ * @param {*} data
61
+ * @param {boolean} [convert=false]
62
+ * @returns
63
+ */
64
+ function plainToClass(clazz, data, convert = false) {
65
+ if (helper__namespace.isClass(clazz)) {
66
+ if (!helper__namespace.isObject(data)) {
67
+ data = {};
68
+ }
69
+ if (data instanceof clazz) {
70
+ return data;
71
+ }
72
+ return assignDtoParams(clazz, data, convert);
73
+ }
74
+ return data;
75
+ }
76
+ /**
77
+ * assign dto params
78
+ * @param clazz
79
+ * @param data
80
+ * @param convert
81
+ * @returns
82
+ */
83
+ function assignDtoParams(clazz, data, convert = false) {
84
+ const cls = Reflect.construct(clazz, []);
85
+ if (convert) {
86
+ return convertAssignDtoParams(clazz, cls, data);
87
+ }
88
+ else {
89
+ for (const key in cls) {
90
+ if (Object.prototype.hasOwnProperty.call(data, key) &&
91
+ data[key] !== undefined) {
92
+ cls[key] = data[key];
93
+ }
94
+ }
95
+ return cls;
96
+ }
97
+ }
98
+ /**
99
+ * convert type and assign dto params
100
+ * @param clazz
101
+ * @param cls
102
+ * @param data
103
+ * @returns
104
+ */
105
+ function convertAssignDtoParams(clazz, cls, data) {
106
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
107
+ for (const key in cls) {
108
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
109
+ data[key] !== undefined) {
110
+ cls[key] = convertParamsType(data[key], cls._typeDef[key]);
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
116
+ for (const [key, type] of originMap) {
117
+ if (key && data[key] !== undefined) {
118
+ cls[key] = convertParamsType(data[key], type);
119
+ }
120
+ }
121
+ }
122
+ return cls;
123
+ }
124
+ /**
125
+ * convertDtoParamsType
126
+ *
127
+ * @param {*} clazz
128
+ * @param {*} cls
129
+ * @returns {*} cls
130
+ */
131
+ function convertDtoParamsType(clazz, cls) {
132
+ if (Object.prototype.hasOwnProperty.call(cls, "_typeDef")) {
133
+ for (const key in cls) {
134
+ if (Object.prototype.hasOwnProperty.call(cls._typeDef, key) &&
135
+ cls[key] !== undefined) {
136
+ cls[key] = convertParamsType(cls[key], cls._typeDef[key]);
137
+ }
138
+ }
139
+ }
140
+ else {
141
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, clazz);
142
+ for (const [key, type] of originMap) {
143
+ if (key && cls[key] !== undefined) {
144
+ cls[key] = convertParamsType(cls[key], type);
145
+ }
146
+ }
147
+ }
148
+ return cls;
149
+ }
150
+ /**
151
+ * 绑定参数类型转换
152
+ *
153
+ * @param {*} param
154
+ * @param {string} type
155
+ * @returns {*}
156
+ */
157
+ function convertParamsType(param, type) {
158
+ try {
159
+ switch (type) {
160
+ case "Number":
161
+ case "number":
162
+ if (helper__namespace.isNaN(param)) {
163
+ return NaN;
164
+ }
165
+ if (helper__namespace.isNumber(param)) {
166
+ return param;
167
+ }
168
+ if (helper__namespace.isNumberString(param)) {
169
+ return helper__namespace.toNumber(param);
170
+ }
171
+ return NaN;
172
+ case "Boolean":
173
+ case "boolean":
174
+ return !!param;
175
+ case "Array":
176
+ case "array":
177
+ case "Tuple":
178
+ case "tuple":
179
+ if (helper__namespace.isArray(param)) {
180
+ return param;
181
+ }
182
+ return helper__namespace.toArray(param);
183
+ case "String":
184
+ case "string":
185
+ if (helper__namespace.isString(param)) {
186
+ return param;
187
+ }
188
+ return helper__namespace.toString(param);
189
+ case "Null":
190
+ case "null":
191
+ return null;
192
+ case "Undefined":
193
+ case "undefined":
194
+ return undefined;
195
+ case "Bigint":
196
+ case "bigint":
197
+ if (typeof param === 'bigint') {
198
+ return param;
199
+ }
200
+ return BigInt(param);
201
+ // case "object":
202
+ // case "enum":
203
+ default: //any
204
+ return param;
205
+ }
206
+ }
207
+ catch (err) {
208
+ return param;
209
+ }
210
+ }
211
+ /**
212
+ * Check the base types.
213
+ *
214
+ * @param {*} value
215
+ * @param {string} type
216
+ * @returns {*}
217
+ */
218
+ function checkParamsType(value, type) {
219
+ switch (type) {
220
+ case "Number":
221
+ case "number":
222
+ if (!helper__namespace.isNumber(value) || helper__namespace.isNaN(value)) {
223
+ return false;
224
+ }
225
+ return true;
226
+ case "Boolean":
227
+ case "boolean":
228
+ if (!helper__namespace.isBoolean(value)) {
229
+ return false;
230
+ }
231
+ return true;
232
+ case "Array":
233
+ case "array":
234
+ case "Tuple":
235
+ case "tuple":
236
+ if (!helper__namespace.isArray(value)) {
237
+ return false;
238
+ }
239
+ return true;
240
+ case "String":
241
+ case "string":
242
+ if (!helper__namespace.isString(value)) {
243
+ return false;
244
+ }
245
+ return true;
246
+ case "Object":
247
+ case "object":
248
+ case "Enum":
249
+ case "enum":
250
+ if (helper__namespace.isTrueEmpty(value)) {
251
+ return false;
252
+ }
253
+ return true;
254
+ case "Null":
255
+ case "null":
256
+ if (!helper__namespace.isNull(value)) {
257
+ return false;
258
+ }
259
+ return true;
260
+ case "Undefined":
261
+ case "undefined":
262
+ if (!helper__namespace.isUndefined(value)) {
263
+ return false;
264
+ }
265
+ return true;
266
+ case "Bigint":
267
+ case "bigint":
268
+ if (typeof value !== 'bigint') {
269
+ return false;
270
+ }
271
+ return true;
272
+ default: //any
273
+ return true;
274
+ }
275
+ }
276
+ /**
277
+ * Checks if value is a chinese name.
278
+ *
279
+ * @param {string} value
280
+ * @returns {boolean}
281
+ */
282
+ function cnName(value) {
283
+ const reg = /^([a-zA-Z0-9\u4e00-\u9fa5\·]{1,10})$/;
284
+ return reg.test(value);
285
+ }
286
+ /**
287
+ * Checks if value is a idCard number.
288
+ *
289
+ * @param {string} value
290
+ * @returns
291
+ */
292
+ function idNumber(value) {
293
+ if (/^\d{15}$/.test(value)) {
294
+ return true;
295
+ }
296
+ if ((/^\d{17}[0-9X]$/).test(value)) {
297
+ const vs = '1,0,x,9,8,7,6,5,4,3,2'.split(',');
298
+ const ps = '7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'.split(',');
299
+ const ss = value.toLowerCase().split('');
300
+ let r = 0;
301
+ for (let i = 0; i < 17; i++) {
302
+ r += ps[i] * ss[i];
303
+ }
304
+ const isOk = (vs[r % 11] === ss[17]);
305
+ return isOk;
306
+ }
307
+ return false;
308
+ }
309
+ /**
310
+ * Checks if value is a mobile phone number.
311
+ *
312
+ * @param {string} value
313
+ * @returns {boolean}
314
+ */
315
+ function mobile(value) {
316
+ const reg = /^(13|14|15|16|17|18|19)\d{9}$/;
317
+ return reg.test(value);
318
+ }
319
+ /**
320
+ * Checks if value is a zipCode.
321
+ *
322
+ * @param {string} value
323
+ * @returns {boolean}
324
+ */
325
+ function zipCode(value) {
326
+ const reg = /^\d{6}$/;
327
+ return reg.test(value);
328
+ }
329
+ /**
330
+ * Checks if value is a plateNumber.
331
+ *
332
+ * @param {string} value
333
+ * @returns {boolean}
334
+ */
335
+ function plateNumber(value) {
336
+ // let reg = new RegExp('^(([\u4e00-\u9fa5][a-zA-Z]|[\u4e00-\u9fa5]{2}\d{2}|[\u4e00-\u9fa5]{2}[a-zA-Z])[-]?|([wW][Jj][\u4e00-\u9fa5]{1}[-]?)|([a-zA-Z]{2}))([A-Za-z0-9]{5}|[DdFf][A-HJ-NP-Za-hj-np-z0-9][0-9]{4}|[0-9]{5}[DdFf])$');
337
+ // let xReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
338
+ const xReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
339
+ // let cReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
340
+ const cReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
341
+ if (value.length === 7) {
342
+ return cReg.test(value);
343
+ }
344
+ else {
345
+ //新能源车牌
346
+ return xReg.test(value);
347
+ }
350
348
  }
351
349
 
352
- /*
353
- * @Description:
354
- * @Usage:
355
- * @Author: richen
356
- * @Date: 2021-11-25 10:47:04
357
- * @LastEditTime: 2022-08-19 14:21:20
358
- */
359
- // constant
360
- const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
361
- const PARAM_RULE_KEY = 'PARAM_RULE_KEY';
362
- const PARAM_CHECK_KEY = 'PARAM_CHECK_KEY';
363
- const ENABLE_VALIDATED = "ENABLE_VALIDATED";
364
- /**
365
- * paramterTypes
366
- *
367
- * @export
368
- * @enum {number}
369
- */
370
- exports.paramterTypes = void 0;
371
- (function (paramterTypes) {
372
- paramterTypes[paramterTypes["Number"] = 0] = "Number";
373
- paramterTypes[paramterTypes["number"] = 1] = "number";
374
- paramterTypes[paramterTypes["String"] = 2] = "String";
375
- paramterTypes[paramterTypes["string"] = 3] = "string";
376
- paramterTypes[paramterTypes["Boolean"] = 4] = "Boolean";
377
- paramterTypes[paramterTypes["boolean"] = 5] = "boolean";
378
- paramterTypes[paramterTypes["Array"] = 6] = "Array";
379
- paramterTypes[paramterTypes["array"] = 7] = "array";
380
- paramterTypes[paramterTypes["Tuple"] = 8] = "Tuple";
381
- paramterTypes[paramterTypes["tuple"] = 9] = "tuple";
382
- paramterTypes[paramterTypes["Object"] = 10] = "Object";
383
- paramterTypes[paramterTypes["object"] = 11] = "object";
384
- paramterTypes[paramterTypes["Enum"] = 12] = "Enum";
385
- paramterTypes[paramterTypes["enum"] = 13] = "enum";
386
- paramterTypes[paramterTypes["Bigint"] = 14] = "Bigint";
387
- paramterTypes[paramterTypes["bigint"] = 15] = "bigint";
388
- paramterTypes[paramterTypes["Null"] = 16] = "Null";
389
- paramterTypes[paramterTypes["null"] = 17] = "null";
390
- paramterTypes[paramterTypes["Undefined"] = 18] = "Undefined";
391
- paramterTypes[paramterTypes["undefined"] = 19] = "undefined";
392
- })(exports.paramterTypes || (exports.paramterTypes = {}));
393
- class ValidateClass {
394
- constructor() {
395
- }
396
- /**
397
- *
398
- *
399
- * @static
400
- * @returns
401
- * @memberof ValidateUtil
402
- */
403
- static getInstance() {
404
- return this.instance || (this.instance = new ValidateClass());
405
- }
406
- /**
407
- * validated data vs dto class
408
- *
409
- * @param {*} Clazz
410
- * @param {*} data
411
- * @param {boolean} [convert=false] auto convert parameters type
412
- * @returns {Promise<any>}
413
- * @memberof ValidateClass
414
- */
415
- async valid(Clazz, data, convert = false) {
416
- let obj = {};
417
- if (data instanceof Clazz) {
418
- obj = data;
419
- }
420
- else {
421
- obj = plainToClass(Clazz, data, convert);
422
- }
423
- let errors = [];
424
- if (convert) {
425
- errors = await classValidator.validate(obj);
426
- }
427
- else {
428
- errors = await classValidator.validate(obj, { skipMissingProperties: true });
429
- }
430
- if (errors.length > 0) {
431
- throw new Error(Object.values(errors[0].constraints)[0]);
432
- }
433
- return obj;
434
- }
435
- }
436
- /**
437
- * ClassValidator for manual
438
- */
439
- const ClassValidator = ValidateClass.getInstance();
440
- /**
441
- * Validator Functions
442
- */
443
- const ValidFuncs = {
444
- /**
445
- * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
446
- * tabs, formfeeds, etc.), returns false
447
- */
448
- IsNotEmpty: (value) => {
449
- return !helper__namespace.isEmpty(value);
450
- },
451
- /**
452
- * Checks if a given value is a real date.
453
- */
454
- IsDate: (value) => {
455
- return helper__namespace.isDate(value);
456
- },
457
- /**
458
- * Checks if the string is an email. If given value is not a string, then it returns false.
459
- */
460
- IsEmail: (value, options) => {
461
- return classValidator.isEmail(value, options);
462
- },
463
- /**
464
- * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
465
- */
466
- IsIP: (value, version) => {
467
- return classValidator.isIP(value, version);
468
- },
469
- /**
470
- * Checks if the string is a valid phone number.
471
- * @param value — the potential phone number string to test
472
- * @param region 2 characters uppercase country code (e.g. DE, US, CH). If users must enter the intl.
473
- * prefix (e.g. +41), then you may pass "ZZ" or null as region.
474
- * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
475
- * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
476
- */
477
- IsPhoneNumber: (value, region) => {
478
- return classValidator.isPhoneNumber(value, region);
479
- },
480
- /**
481
- * Checks if the string is an url. If given value is not a string, then it returns false.
482
- */
483
- IsUrl: (value, options) => {
484
- return classValidator.isURL(value, options);
485
- },
486
- /**
487
- * check if the string is a hash of type algorithm. Algorithm is one of
488
- * ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
489
- */
490
- IsHash: (value, algorithm) => {
491
- return classValidator.isHash(value, algorithm);
492
- },
493
- /**
494
- * Checks if value is a chinese name.
495
- */
496
- IsCnName: (value) => {
497
- if (!helper__namespace.isString(value)) {
498
- return false;
499
- }
500
- return cnName(value);
501
- },
502
- /**
503
- * Checks if value is a idcard number.
504
- */
505
- IsIdNumber: (value) => {
506
- if (!helper__namespace.isString(value)) {
507
- return false;
508
- }
509
- return idNumber(value);
510
- },
511
- /**
512
- * Checks if value is a zipCode.
513
- */
514
- IsZipCode: (value) => {
515
- if (!helper__namespace.isString(value)) {
516
- return false;
517
- }
518
- return zipCode(value);
519
- },
520
- /**
521
- * Checks if value is a mobile phone number.
522
- */
523
- IsMobile: (value) => {
524
- if (!helper__namespace.isString(value)) {
525
- return false;
526
- }
527
- return mobile(value);
528
- },
529
- /**
530
- * Checks if value is a plateNumber.
531
- */
532
- IsPlateNumber: (value) => {
533
- if (!helper__namespace.isString(value)) {
534
- return false;
535
- }
536
- return plateNumber(value);
537
- },
538
- /**
539
- * Checks if value matches ("===") the comparison.
540
- */
541
- Equals: (value, comparison) => {
542
- return classValidator.equals(value, comparison);
543
- },
544
- /**
545
- * Checks if value does not match ("!==") the comparison.
546
- */
547
- NotEquals: (value, comparison) => {
548
- return classValidator.notEquals(value, comparison);
549
- },
550
- /**
551
- * Checks if the string contains the seed. If given value is not a string, then it returns false.
552
- */
553
- Contains: (value, seed) => {
554
- return classValidator.contains(value, seed);
555
- },
556
- /**
557
- * Checks if given value is in a array of allowed values.
558
- */
559
- IsIn: (value, possibleValues) => {
560
- return classValidator.isIn(value, possibleValues);
561
- },
562
- /**
563
- * Checks if given value not in a array of allowed values.
564
- */
565
- IsNotIn: (value, possibleValues) => {
566
- return classValidator.isNotIn(value, possibleValues);
567
- },
568
- /**
569
- * Checks if the first number is greater than or equal to the second.
570
- */
571
- Gt: (num, min) => {
572
- return helper__namespace.toNumber(num) > min;
573
- },
574
- /**
575
- * Checks if the first number is less than or equal to the second.
576
- */
577
- Lt: (num, max) => {
578
- return helper__namespace.toNumber(num) < max;
579
- },
580
- /**
581
- * Checks if the first number is greater than or equal to the second.
582
- */
583
- Gte: (num, min) => {
584
- return helper__namespace.toNumber(num) >= min;
585
- },
586
- /**
587
- * Checks if the first number is less than or equal to the second.
588
- */
589
- Lte: (num, max) => {
590
- return helper__namespace.toNumber(num) <= max;
591
- },
592
- };
593
- /**
594
- * Use functions or built-in rules for validation.
595
- *
596
- * @export
597
- * @param {ValidRules} rule
598
- * @param {unknown} value
599
- * @param {(string | ValidOtpions)} [options]
600
- * @returns {*}
601
- */
602
- const FunctionValidator = {
603
- IsNotEmpty: function (value, options) {
604
- throw new Error("Function not implemented.");
605
- },
606
- IsDate: function (value, options) {
607
- throw new Error("Function not implemented.");
608
- },
609
- IsEmail: function (value, options) {
610
- throw new Error("Function not implemented.");
611
- },
612
- IsIP: function (value, options) {
613
- throw new Error("Function not implemented.");
614
- },
615
- IsPhoneNumber: function (value, options) {
616
- throw new Error("Function not implemented.");
617
- },
618
- IsUrl: function (value, options) {
619
- throw new Error("Function not implemented.");
620
- },
621
- IsHash: function (value, options) {
622
- throw new Error("Function not implemented.");
623
- },
624
- IsCnName: function (value, options) {
625
- throw new Error("Function not implemented.");
626
- },
627
- IsIdNumber: function (value, options) {
628
- throw new Error("Function not implemented.");
629
- },
630
- IsZipCode: function (value, options) {
631
- throw new Error("Function not implemented.");
632
- },
633
- IsMobile: function (value, options) {
634
- throw new Error("Function not implemented.");
635
- },
636
- IsPlateNumber: function (value, options) {
637
- throw new Error("Function not implemented.");
638
- },
639
- Equals: function (value, options) {
640
- throw new Error("Function not implemented.");
641
- },
642
- NotEquals: function (value, options) {
643
- throw new Error("Function not implemented.");
644
- },
645
- Contains: function (value, options) {
646
- throw new Error("Function not implemented.");
647
- },
648
- IsIn: function (value, options) {
649
- throw new Error("Function not implemented.");
650
- },
651
- IsNotIn: function (value, options) {
652
- throw new Error("Function not implemented.");
653
- },
654
- Gt: function (value, options) {
655
- throw new Error("Function not implemented.");
656
- },
657
- Lt: function (value, options) {
658
- throw new Error("Function not implemented.");
659
- },
660
- Gte: function (value, options) {
661
- throw new Error("Function not implemented.");
662
- },
663
- Lte: function (value, options) {
664
- throw new Error("Function not implemented.");
665
- }
666
- };
667
- Object.keys(ValidFuncs).forEach((key) => {
668
- FunctionValidator[key] = (value, options) => {
669
- if (helper__namespace.isString(options)) {
670
- options = { message: options, value: null };
671
- }
672
- if (!ValidFuncs[key](value, options.value)) {
673
- throw new Error(options.message || `ValidatorError: invalid arguments.`);
674
- }
675
- };
350
+ /*
351
+ * @Description:
352
+ * @Usage:
353
+ * @Author: richen
354
+ * @Date: 2021-11-25 10:47:04
355
+ * @LastEditTime: 2022-08-19 14:21:20
356
+ */
357
+ // constant
358
+ const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
359
+ const PARAM_RULE_KEY = 'PARAM_RULE_KEY';
360
+ const PARAM_CHECK_KEY = 'PARAM_CHECK_KEY';
361
+ const ENABLE_VALIDATED = "ENABLE_VALIDATED";
362
+ /**
363
+ * paramterTypes
364
+ *
365
+ * @export
366
+ * @enum {number}
367
+ */
368
+ exports.paramterTypes = void 0;
369
+ (function (paramterTypes) {
370
+ paramterTypes[paramterTypes["Number"] = 0] = "Number";
371
+ paramterTypes[paramterTypes["number"] = 1] = "number";
372
+ paramterTypes[paramterTypes["String"] = 2] = "String";
373
+ paramterTypes[paramterTypes["string"] = 3] = "string";
374
+ paramterTypes[paramterTypes["Boolean"] = 4] = "Boolean";
375
+ paramterTypes[paramterTypes["boolean"] = 5] = "boolean";
376
+ paramterTypes[paramterTypes["Array"] = 6] = "Array";
377
+ paramterTypes[paramterTypes["array"] = 7] = "array";
378
+ paramterTypes[paramterTypes["Tuple"] = 8] = "Tuple";
379
+ paramterTypes[paramterTypes["tuple"] = 9] = "tuple";
380
+ paramterTypes[paramterTypes["Object"] = 10] = "Object";
381
+ paramterTypes[paramterTypes["object"] = 11] = "object";
382
+ paramterTypes[paramterTypes["Enum"] = 12] = "Enum";
383
+ paramterTypes[paramterTypes["enum"] = 13] = "enum";
384
+ paramterTypes[paramterTypes["Bigint"] = 14] = "Bigint";
385
+ paramterTypes[paramterTypes["bigint"] = 15] = "bigint";
386
+ paramterTypes[paramterTypes["Null"] = 16] = "Null";
387
+ paramterTypes[paramterTypes["null"] = 17] = "null";
388
+ paramterTypes[paramterTypes["Undefined"] = 18] = "Undefined";
389
+ paramterTypes[paramterTypes["undefined"] = 19] = "undefined";
390
+ })(exports.paramterTypes || (exports.paramterTypes = {}));
391
+ class ValidateClass {
392
+ constructor() {
393
+ }
394
+ /**
395
+ *
396
+ *
397
+ * @static
398
+ * @returns
399
+ * @memberof ValidateUtil
400
+ */
401
+ static getInstance() {
402
+ return this.instance || (this.instance = new ValidateClass());
403
+ }
404
+ /**
405
+ * validated data vs dto class
406
+ *
407
+ * @param {*} Clazz
408
+ * @param {*} data
409
+ * @param {boolean} [convert=false] auto convert parameters type
410
+ * @returns {Promise<any>}
411
+ * @memberof ValidateClass
412
+ */
413
+ async valid(Clazz, data, convert = false) {
414
+ let obj = {};
415
+ if (data instanceof Clazz) {
416
+ obj = data;
417
+ }
418
+ else {
419
+ obj = plainToClass(Clazz, data, convert);
420
+ }
421
+ let errors = [];
422
+ if (convert) {
423
+ errors = await classValidator.validate(obj);
424
+ }
425
+ else {
426
+ errors = await classValidator.validate(obj, { skipMissingProperties: true });
427
+ }
428
+ if (errors.length > 0) {
429
+ throw new Error(Object.values(errors[0].constraints)[0]);
430
+ }
431
+ return obj;
432
+ }
433
+ }
434
+ /**
435
+ * ClassValidator for manual
436
+ */
437
+ const ClassValidator = ValidateClass.getInstance();
438
+ /**
439
+ * Validator Functions
440
+ */
441
+ const ValidFuncs = {
442
+ /**
443
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces,
444
+ * tabs, formfeeds, etc.), returns false
445
+ */
446
+ IsNotEmpty: (value) => {
447
+ return !helper__namespace.isEmpty(value);
448
+ },
449
+ /**
450
+ * Checks if a given value is a real date.
451
+ */
452
+ IsDate: (value) => {
453
+ return helper__namespace.isDate(value);
454
+ },
455
+ /**
456
+ * Checks if the string is an email. If given value is not a string, then it returns false.
457
+ */
458
+ IsEmail: (value, options) => {
459
+ return classValidator.isEmail(value, options);
460
+ },
461
+ /**
462
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
463
+ */
464
+ IsIP: (value, version) => {
465
+ return classValidator.isIP(value, version);
466
+ },
467
+ /**
468
+ * Checks if the string is a valid phone number.
469
+ * @param value — the potential phone number string to test
470
+ * @param region 2 characters uppercase country code (e.g. DE, US, CH). If users must enter the intl.
471
+ * prefix (e.g. +41), then you may pass "ZZ" or null as region.
472
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
473
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
474
+ */
475
+ IsPhoneNumber: (value, region) => {
476
+ return classValidator.isPhoneNumber(value, region);
477
+ },
478
+ /**
479
+ * Checks if the string is an url. If given value is not a string, then it returns false.
480
+ */
481
+ IsUrl: (value, options) => {
482
+ return classValidator.isURL(value, options);
483
+ },
484
+ /**
485
+ * check if the string is a hash of type algorithm. Algorithm is one of
486
+ * ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
487
+ */
488
+ IsHash: (value, algorithm) => {
489
+ return classValidator.isHash(value, algorithm);
490
+ },
491
+ /**
492
+ * Checks if value is a chinese name.
493
+ */
494
+ IsCnName: (value) => {
495
+ if (!helper__namespace.isString(value)) {
496
+ return false;
497
+ }
498
+ return cnName(value);
499
+ },
500
+ /**
501
+ * Checks if value is a idcard number.
502
+ */
503
+ IsIdNumber: (value) => {
504
+ if (!helper__namespace.isString(value)) {
505
+ return false;
506
+ }
507
+ return idNumber(value);
508
+ },
509
+ /**
510
+ * Checks if value is a zipCode.
511
+ */
512
+ IsZipCode: (value) => {
513
+ if (!helper__namespace.isString(value)) {
514
+ return false;
515
+ }
516
+ return zipCode(value);
517
+ },
518
+ /**
519
+ * Checks if value is a mobile phone number.
520
+ */
521
+ IsMobile: (value) => {
522
+ if (!helper__namespace.isString(value)) {
523
+ return false;
524
+ }
525
+ return mobile(value);
526
+ },
527
+ /**
528
+ * Checks if value is a plateNumber.
529
+ */
530
+ IsPlateNumber: (value) => {
531
+ if (!helper__namespace.isString(value)) {
532
+ return false;
533
+ }
534
+ return plateNumber(value);
535
+ },
536
+ /**
537
+ * Checks if value matches ("===") the comparison.
538
+ */
539
+ Equals: (value, comparison) => {
540
+ return classValidator.equals(value, comparison);
541
+ },
542
+ /**
543
+ * Checks if value does not match ("!==") the comparison.
544
+ */
545
+ NotEquals: (value, comparison) => {
546
+ return classValidator.notEquals(value, comparison);
547
+ },
548
+ /**
549
+ * Checks if the string contains the seed. If given value is not a string, then it returns false.
550
+ */
551
+ Contains: (value, seed) => {
552
+ return classValidator.contains(value, seed);
553
+ },
554
+ /**
555
+ * Checks if given value is in a array of allowed values.
556
+ */
557
+ IsIn: (value, possibleValues) => {
558
+ return classValidator.isIn(value, possibleValues);
559
+ },
560
+ /**
561
+ * Checks if given value not in a array of allowed values.
562
+ */
563
+ IsNotIn: (value, possibleValues) => {
564
+ return classValidator.isNotIn(value, possibleValues);
565
+ },
566
+ /**
567
+ * Checks if the first number is greater than or equal to the second.
568
+ */
569
+ Gt: (num, min) => {
570
+ return helper__namespace.toNumber(num) > min;
571
+ },
572
+ /**
573
+ * Checks if the first number is less than or equal to the second.
574
+ */
575
+ Lt: (num, max) => {
576
+ return helper__namespace.toNumber(num) < max;
577
+ },
578
+ /**
579
+ * Checks if the first number is greater than or equal to the second.
580
+ */
581
+ Gte: (num, min) => {
582
+ return helper__namespace.toNumber(num) >= min;
583
+ },
584
+ /**
585
+ * Checks if the first number is less than or equal to the second.
586
+ */
587
+ Lte: (num, max) => {
588
+ return helper__namespace.toNumber(num) <= max;
589
+ },
590
+ };
591
+ /**
592
+ * Use functions or built-in rules for validation.
593
+ *
594
+ * @export
595
+ * @param {ValidRules} rule
596
+ * @param {unknown} value
597
+ * @param {(string | ValidOtpions)} [options]
598
+ * @returns {*}
599
+ */
600
+ const FunctionValidator = {
601
+ IsNotEmpty: function (value, options) {
602
+ throw new Error("Function not implemented.");
603
+ },
604
+ IsDate: function (value, options) {
605
+ throw new Error("Function not implemented.");
606
+ },
607
+ IsEmail: function (value, options) {
608
+ throw new Error("Function not implemented.");
609
+ },
610
+ IsIP: function (value, options) {
611
+ throw new Error("Function not implemented.");
612
+ },
613
+ IsPhoneNumber: function (value, options) {
614
+ throw new Error("Function not implemented.");
615
+ },
616
+ IsUrl: function (value, options) {
617
+ throw new Error("Function not implemented.");
618
+ },
619
+ IsHash: function (value, options) {
620
+ throw new Error("Function not implemented.");
621
+ },
622
+ IsCnName: function (value, options) {
623
+ throw new Error("Function not implemented.");
624
+ },
625
+ IsIdNumber: function (value, options) {
626
+ throw new Error("Function not implemented.");
627
+ },
628
+ IsZipCode: function (value, options) {
629
+ throw new Error("Function not implemented.");
630
+ },
631
+ IsMobile: function (value, options) {
632
+ throw new Error("Function not implemented.");
633
+ },
634
+ IsPlateNumber: function (value, options) {
635
+ throw new Error("Function not implemented.");
636
+ },
637
+ Equals: function (value, options) {
638
+ throw new Error("Function not implemented.");
639
+ },
640
+ NotEquals: function (value, options) {
641
+ throw new Error("Function not implemented.");
642
+ },
643
+ Contains: function (value, options) {
644
+ throw new Error("Function not implemented.");
645
+ },
646
+ IsIn: function (value, options) {
647
+ throw new Error("Function not implemented.");
648
+ },
649
+ IsNotIn: function (value, options) {
650
+ throw new Error("Function not implemented.");
651
+ },
652
+ Gt: function (value, options) {
653
+ throw new Error("Function not implemented.");
654
+ },
655
+ Lt: function (value, options) {
656
+ throw new Error("Function not implemented.");
657
+ },
658
+ Gte: function (value, options) {
659
+ throw new Error("Function not implemented.");
660
+ },
661
+ Lte: function (value, options) {
662
+ throw new Error("Function not implemented.");
663
+ }
664
+ };
665
+ Object.keys(ValidFuncs).forEach((key) => {
666
+ FunctionValidator[key] = (value, options) => {
667
+ if (helper__namespace.isString(options)) {
668
+ options = { message: options, value: null };
669
+ }
670
+ if (!ValidFuncs[key](value, options.value)) {
671
+ throw new Error(options.message || `ValidatorError: invalid arguments.`);
672
+ }
673
+ };
676
674
  });
677
675
 
678
- /*
679
- * @Description:
680
- * @Usage:
681
- * @Author: richen
682
- * @Date: 2021-11-25 10:46:57
683
- * @LastEditTime: 2022-08-19 14:21:12
684
- */
685
- /**
686
- * Validation parameter's type and values.
687
- *
688
- * @export
689
- * @param {(ValidRules | ValidRules[] | Function)} rule
690
- * @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
691
- * Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
692
- * @returns {*} {ParameterDecorator}
693
- */
694
- function Valid(rule, options) {
695
- let rules = [];
696
- if (helper__namespace.isString(rule)) {
697
- rules = rule.split(",");
698
- }
699
- else {
700
- rules = rule;
701
- }
702
- return (target, propertyKey, descriptor) => {
703
- var _a;
704
- // 获取成员参数类型
705
- const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
706
- const type = ((_a = paramTypes[descriptor]) === null || _a === void 0 ? void 0 : _a.name) ? paramTypes[descriptor].name : 'object';
707
- if (helper__namespace.isString(options)) {
708
- options = { message: options, value: null };
709
- }
710
- koatty_container.IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
711
- name: propertyKey,
712
- rule: rules,
713
- options,
714
- index: descriptor,
715
- type
716
- }, target, propertyKey);
717
- };
718
- }
719
- /**
720
- * Validation parameter's type and values from DTO class.
721
- *
722
- * @export
723
- * @returns {MethodDecorator}
724
- */
725
- function Validated() {
726
- return (target, propertyKey, descriptor) => {
727
- //
728
- koatty_container.IOCContainer.savePropertyData(PARAM_CHECK_KEY, {
729
- dtoCheck: 1
730
- }, target, propertyKey);
731
- // 获取成员参数类型
732
- // const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey) || [];
733
- // const { value, configurable, enumerable } = descriptor;
734
- // descriptor = {
735
- // configurable,
736
- // enumerable,
737
- // writable: true,
738
- // value: async function valid(...props: any[]) {
739
- // const ps: any[] = [];
740
- // // tslint:disable-next-line: no-unused-expression
741
- // (props || []).map((value: any, index: number) => {
742
- // const type = (paramTypes[index] && paramTypes[index].name) ? paramTypes[index].name : "any";
743
- // if (!paramterTypes[type]) {
744
- // ps.push(ClassValidator.valid(paramTypes[index], value, true));
745
- // } else {
746
- // ps.push(Promise.resolve(value));
747
- // }
748
- // });
749
- // if (ps.length > 0) {
750
- // props = await Promise.all(ps);
751
- // }
752
- // // tslint:disable-next-line: no-invalid-this
753
- // return value.apply(this, props);
754
- // }
755
- // };
756
- // return descriptor;
757
- };
758
- }
759
- /**
760
- * Marks property as included in the process of transformation.
761
- *
762
- * @export
763
- * @returns {PropertyDecorator}
764
- */
765
- function Expose() {
766
- return function (object, propertyName) {
767
- const types = Reflect.getMetadata("design:type", object, propertyName);
768
- if (types) {
769
- const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, object);
770
- originMap.set(propertyName, types.name);
771
- }
772
- };
773
- }
774
- /**
775
- * Identifies that the field needs to be defined
776
- *
777
- * @export
778
- * @returns {PropertyDecorator}
779
- */
780
- function IsDefined() {
781
- return function (object, propertyName) {
782
- setExpose(object, propertyName);
783
- };
784
- }
785
- /**
786
- * Checks if value is a chinese name.
787
- *
788
- * @export
789
- * @param {string} property
790
- * @param {ValidationOptions} [validationOptions]
791
- * @returns {PropertyDecorator}
792
- */
793
- function IsCnName(validationOptions) {
794
- return function (object, propertyName) {
795
- setExpose(object, propertyName);
796
- classValidator.registerDecorator({
797
- name: "IsCnName",
798
- target: object.constructor,
799
- propertyName,
800
- options: validationOptions,
801
- validator: {
802
- validate(value, args) {
803
- return cnName(value);
804
- },
805
- defaultMessage(args) {
806
- return "invalid parameter ($property).";
807
- }
808
- }
809
- });
810
- };
811
- }
812
- /**
813
- * Checks if value is a idCard number(chinese).
814
- *
815
- * @export
816
- * @param {string} property
817
- * @param {ValidationOptions} [validationOptions]
818
- * @returns {PropertyDecorator}
819
- */
820
- function IsIdNumber(validationOptions) {
821
- return function (object, propertyName) {
822
- setExpose(object, propertyName);
823
- classValidator.registerDecorator({
824
- name: "IsIdNumber",
825
- target: object.constructor,
826
- propertyName,
827
- options: validationOptions,
828
- validator: {
829
- validate(value, args) {
830
- return idNumber(value);
831
- },
832
- defaultMessage(args) {
833
- return "invalid parameter ($property).";
834
- }
835
- }
836
- });
837
- };
838
- }
839
- /**
840
- * Checks if value is a zipCode(chinese).
841
- *
842
- * @export
843
- * @param {string} property
844
- * @param {ValidationOptions} [validationOptions]
845
- * @returns {PropertyDecorator}
846
- */
847
- function IsZipCode(validationOptions) {
848
- return function (object, propertyName) {
849
- setExpose(object, propertyName);
850
- classValidator.registerDecorator({
851
- name: "IsZipCode",
852
- target: object.constructor,
853
- propertyName,
854
- options: validationOptions,
855
- validator: {
856
- validate(value, args) {
857
- return zipCode(value);
858
- },
859
- defaultMessage(args) {
860
- return "invalid parameter ($property).";
861
- }
862
- }
863
- });
864
- };
865
- }
866
- /**
867
- * Checks if value is a mobile phone number(chinese).
868
- *
869
- * @export
870
- * @param {string} property
871
- * @param {ValidationOptions} [validationOptions]
872
- * @returns {PropertyDecorator}
873
- */
874
- function IsMobile(validationOptions) {
875
- return function (object, propertyName) {
876
- setExpose(object, propertyName);
877
- classValidator.registerDecorator({
878
- name: "IsMobile",
879
- target: object.constructor,
880
- propertyName,
881
- options: validationOptions,
882
- validator: {
883
- validate(value, args) {
884
- return mobile(value);
885
- },
886
- defaultMessage(args) {
887
- return "invalid parameter ($property).";
888
- }
889
- }
890
- });
891
- };
892
- }
893
- /**
894
- * Checks if value is a plate number(chinese).
895
- *
896
- * @export
897
- * @param {string} property
898
- * @param {ValidationOptions} [validationOptions]
899
- * @returns {PropertyDecorator}
900
- */
901
- function IsPlateNumber(validationOptions) {
902
- return function (object, propertyName) {
903
- setExpose(object, propertyName);
904
- classValidator.registerDecorator({
905
- name: "IsPlateNumber",
906
- target: object.constructor,
907
- propertyName,
908
- options: validationOptions,
909
- validator: {
910
- validate(value, args) {
911
- return plateNumber(value);
912
- },
913
- defaultMessage(args) {
914
- return "invalid parameter ($property).";
915
- }
916
- }
917
- });
918
- };
919
- }
920
- /**
921
- * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces, tabs, formfeeds, etc.), returns false.
922
- *
923
- * @export
924
- * @param {ValidationOptions} [validationOptions]
925
- * @returns {PropertyDecorator}
926
- */
927
- function IsNotEmpty(validationOptions) {
928
- return function (object, propertyName) {
929
- setExpose(object, propertyName);
930
- classValidator.registerDecorator({
931
- name: "IsNotEmpty",
932
- target: object.constructor,
933
- propertyName,
934
- options: validationOptions,
935
- validator: {
936
- validate(value, args) {
937
- return !helper__namespace.isEmpty(value);
938
- },
939
- defaultMessage(args) {
940
- return "invalid parameter ($property).";
941
- }
942
- }
943
- });
944
- };
945
- }
946
- /**
947
- * Checks if value matches ("===") the comparison.
948
- *
949
- * @export
950
- * @param {*} comparison
951
- * @param {ValidationOptions} [validationOptions]
952
- * @returns {PropertyDecorator}
953
- */
954
- function Equals(comparison, validationOptions) {
955
- return function (object, propertyName) {
956
- setExpose(object, propertyName);
957
- classValidator.registerDecorator({
958
- name: "vEquals",
959
- target: object.constructor,
960
- propertyName,
961
- options: validationOptions,
962
- validator: {
963
- validate(value, args) {
964
- return classValidator.equals(value, comparison);
965
- },
966
- defaultMessage(args) {
967
- return `invalid parameter, ($property) must be equals ${comparison}.`;
968
- }
969
- }
970
- });
971
- };
972
- }
973
- /**
974
- * Checks if value does not match ("!==") the comparison.
975
- *
976
- * @export
977
- * @param {*} comparison
978
- * @param {ValidationOptions} [validationOptions]
979
- * @returns {PropertyDecorator}
980
- */
981
- function NotEquals(comparison, validationOptions) {
982
- return function (object, propertyName) {
983
- setExpose(object, propertyName);
984
- classValidator.registerDecorator({
985
- name: "vNotEquals",
986
- target: object.constructor,
987
- propertyName,
988
- options: validationOptions,
989
- validator: {
990
- validate(value, args) {
991
- return classValidator.notEquals(value, comparison);
992
- },
993
- defaultMessage(args) {
994
- return `invalid parameter, ($property) must be not equals ${comparison}.`;
995
- }
996
- }
997
- });
998
- };
999
- }
1000
- /**
1001
- * Checks if the string contains the seed.
1002
- *
1003
- * @export
1004
- * @param {string} seed
1005
- * @param {ValidationOptions} [validationOptions]
1006
- * @returns {PropertyDecorator}
1007
- */
1008
- function Contains(seed, validationOptions) {
1009
- return function (object, propertyName) {
1010
- setExpose(object, propertyName);
1011
- classValidator.registerDecorator({
1012
- name: "vContains",
1013
- target: object.constructor,
1014
- propertyName,
1015
- options: validationOptions,
1016
- validator: {
1017
- validate(value, args) {
1018
- return classValidator.contains(value, seed);
1019
- // return typeof value === "string" && (value.indexOf(seed) > -1);
1020
- },
1021
- defaultMessage(args) {
1022
- return `invalid parameter, ($property) must be contains ${seed}.`;
1023
- }
1024
- }
1025
- });
1026
- };
1027
- }
1028
- /**
1029
- * Checks if given value is in a array of allowed values.
1030
- *
1031
- * @export
1032
- * @param {any[]} possibleValues
1033
- * @param {ValidationOptions} [validationOptions]
1034
- * @returns {PropertyDecorator}
1035
- */
1036
- function IsIn(possibleValues, validationOptions) {
1037
- return function (object, propertyName) {
1038
- setExpose(object, propertyName);
1039
- classValidator.registerDecorator({
1040
- name: "vIsIn",
1041
- target: object.constructor,
1042
- propertyName,
1043
- options: validationOptions,
1044
- validator: {
1045
- validate(value, args) {
1046
- return classValidator.isIn(value, possibleValues);
1047
- },
1048
- defaultMessage(args) {
1049
- return `invalid parameter ($property).`;
1050
- }
1051
- }
1052
- });
1053
- };
1054
- }
1055
- /**
1056
- * Checks if given value not in a array of allowed values.
1057
- *
1058
- * @export
1059
- * @param {any[]} possibleValues
1060
- * @param {ValidationOptions} [validationOptions]
1061
- * @returns {PropertyDecorator}
1062
- */
1063
- function IsNotIn(possibleValues, validationOptions) {
1064
- return function (object, propertyName) {
1065
- setExpose(object, propertyName);
1066
- classValidator.registerDecorator({
1067
- name: "vIsNotIn",
1068
- target: object.constructor,
1069
- propertyName,
1070
- options: validationOptions,
1071
- validator: {
1072
- validate(value, args) {
1073
- return classValidator.isNotIn(value, possibleValues);
1074
- },
1075
- defaultMessage(args) {
1076
- return `invalid parameter ($property).`;
1077
- }
1078
- }
1079
- });
1080
- };
1081
- }
1082
- /**
1083
- * Checks if a given value is a real date.
1084
- *
1085
- * @export
1086
- * @param {ValidationOptions} [validationOptions]
1087
- * @returns {PropertyDecorator}
1088
- */
1089
- function IsDate(validationOptions) {
1090
- return function (object, propertyName) {
1091
- setExpose(object, propertyName);
1092
- classValidator.registerDecorator({
1093
- name: "vIsDate",
1094
- target: object.constructor,
1095
- propertyName,
1096
- options: validationOptions,
1097
- validator: {
1098
- validate(value, args) {
1099
- return classValidator.isDate(value);
1100
- },
1101
- defaultMessage(args) {
1102
- return `invalid parameter ($property).`;
1103
- }
1104
- }
1105
- });
1106
- };
1107
- }
1108
- /**
1109
- * Checks if the first number is greater than or equal to the min value.
1110
- *
1111
- * @export
1112
- * @param {number} min
1113
- * @param {ValidationOptions} [validationOptions]
1114
- * @returns {PropertyDecorator}
1115
- */
1116
- function Gt(min, validationOptions) {
1117
- return function (object, propertyName) {
1118
- setExpose(object, propertyName);
1119
- classValidator.registerDecorator({
1120
- name: "vMin",
1121
- target: object.constructor,
1122
- propertyName,
1123
- options: validationOptions,
1124
- validator: {
1125
- validate(value, args) {
1126
- return helper__namespace.toNumber(value) > min;
1127
- },
1128
- defaultMessage(args) {
1129
- return `invalid parameter ($property).`;
1130
- }
1131
- }
1132
- });
1133
- };
1134
- }
1135
- /**
1136
- * Checks if the first number is less than or equal to the max value.
1137
- *
1138
- * @export
1139
- * @param {number} max
1140
- * @param {ValidationOptions} [validationOptions]
1141
- * @returns {PropertyDecorator}
1142
- */
1143
- function Lt(max, validationOptions) {
1144
- return function (object, propertyName) {
1145
- setExpose(object, propertyName);
1146
- classValidator.registerDecorator({
1147
- name: "vMax",
1148
- target: object.constructor,
1149
- propertyName,
1150
- options: validationOptions,
1151
- validator: {
1152
- validate(value, args) {
1153
- return helper__namespace.toNumber(value) < max;
1154
- },
1155
- defaultMessage(args) {
1156
- return `invalid parameter ($property).`;
1157
- }
1158
- }
1159
- });
1160
- };
1161
- }
1162
- /**
1163
- * Checks if the first number is greater than or equal to the min value.
1164
- *
1165
- * @export
1166
- * @param {number} min
1167
- * @param {ValidationOptions} [validationOptions]
1168
- * @returns {PropertyDecorator}
1169
- */
1170
- function Gte(min, validationOptions) {
1171
- return function (object, propertyName) {
1172
- setExpose(object, propertyName);
1173
- classValidator.registerDecorator({
1174
- name: "vMin",
1175
- target: object.constructor,
1176
- propertyName,
1177
- options: validationOptions,
1178
- validator: {
1179
- validate(value, args) {
1180
- return helper__namespace.toNumber(value) >= min;
1181
- },
1182
- defaultMessage(args) {
1183
- return `invalid parameter ($property).`;
1184
- }
1185
- }
1186
- });
1187
- };
1188
- }
1189
- /**
1190
- * Checks if the first number is less than or equal to the max value.
1191
- *
1192
- * @export
1193
- * @param {number} max
1194
- * @param {ValidationOptions} [validationOptions]
1195
- * @returns {PropertyDecorator}
1196
- */
1197
- function Lte(max, validationOptions) {
1198
- return function (object, propertyName) {
1199
- setExpose(object, propertyName);
1200
- classValidator.registerDecorator({
1201
- name: "vMax",
1202
- target: object.constructor,
1203
- propertyName,
1204
- options: validationOptions,
1205
- validator: {
1206
- validate(value, args) {
1207
- return helper__namespace.toNumber(value) <= max;
1208
- },
1209
- defaultMessage(args) {
1210
- return `invalid parameter ($property).`;
1211
- }
1212
- }
1213
- });
1214
- };
1215
- }
1216
- /**
1217
- * Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
1218
- * If given value is not a string, then it returns false.
1219
- *
1220
- * @export
1221
- * @param {number} min
1222
- * @param {number} [max]
1223
- * @param {ValidationOptions} [validationOptions]
1224
- * @returns {PropertyDecorator}
1225
- */
1226
- function Length(min, max, validationOptions) {
1227
- return function (object, propertyName) {
1228
- setExpose(object, propertyName);
1229
- classValidator.registerDecorator({
1230
- name: "vLength",
1231
- target: object.constructor,
1232
- propertyName,
1233
- options: validationOptions,
1234
- validator: {
1235
- validate(value, args) {
1236
- return classValidator.length(value, min, max);
1237
- },
1238
- defaultMessage(args) {
1239
- return `invalid parameter ($property).`;
1240
- }
1241
- }
1242
- });
1243
- };
1244
- }
1245
- /**
1246
- * Checks if the string is an email. If given value is not a string, then it returns false.
1247
- *
1248
- * @export
1249
- * @param {IsEmailOptions} [options]
1250
- * @param {ValidationOptions} [validationOptions]
1251
- * @returns {PropertyDecorator}
1252
- */
1253
- function IsEmail(options, validationOptions) {
1254
- return function (object, propertyName) {
1255
- setExpose(object, propertyName);
1256
- classValidator.registerDecorator({
1257
- name: "vIsEmail",
1258
- target: object.constructor,
1259
- propertyName,
1260
- options: validationOptions,
1261
- validator: {
1262
- validate(value, args) {
1263
- return classValidator.isEmail(value);
1264
- },
1265
- defaultMessage(args) {
1266
- return `invalid parameter ($property).`;
1267
- }
1268
- }
1269
- });
1270
- };
1271
- }
1272
- /**
1273
- * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
1274
- *
1275
- * @export
1276
- * @param {number} [version]
1277
- * @param {ValidationOptions} [validationOptions]
1278
- * @returns {PropertyDecorator}
1279
- */
1280
- function IsIP(version, validationOptions) {
1281
- return function (object, propertyName) {
1282
- setExpose(object, propertyName);
1283
- classValidator.registerDecorator({
1284
- name: "vIsIP",
1285
- target: object.constructor,
1286
- propertyName,
1287
- options: validationOptions,
1288
- validator: {
1289
- validate(value, args) {
1290
- return classValidator.isIP(value, version);
1291
- },
1292
- defaultMessage(args) {
1293
- return `invalid parameter ($property).`;
1294
- }
1295
- }
1296
- });
1297
- };
1298
- }
1299
- /**
1300
- * Checks if the string is a valid phone number.
1301
- *
1302
- * @export
1303
- * @param {string} {string} region 2 characters uppercase country code (e.g. DE, US, CH).
1304
- * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
1305
- * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
1306
- * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
1307
- * @param {ValidationOptions} [validationOptions]
1308
- * @returns {PropertyDecorator}
1309
- */
1310
- function IsPhoneNumber(region, validationOptions) {
1311
- return function (object, propertyName) {
1312
- setExpose(object, propertyName);
1313
- classValidator.registerDecorator({
1314
- name: "vIsPhoneNumber",
1315
- target: object.constructor,
1316
- propertyName,
1317
- options: validationOptions,
1318
- validator: {
1319
- validate(value, args) {
1320
- return classValidator.isPhoneNumber(value, region);
1321
- },
1322
- defaultMessage(args) {
1323
- return `invalid parameter ($property).`;
1324
- }
1325
- }
1326
- });
1327
- };
1328
- }
1329
- /**
1330
- * Checks if the string is an url.
1331
- *
1332
- * @export
1333
- * @param {IsURLOptions} [options]
1334
- * @param {ValidationOptions} [validationOptions]
1335
- * @returns {PropertyDecorator}
1336
- */
1337
- function IsUrl(options, validationOptions) {
1338
- return function (object, propertyName) {
1339
- setExpose(object, propertyName);
1340
- classValidator.registerDecorator({
1341
- name: "vIsUrl",
1342
- target: object.constructor,
1343
- propertyName,
1344
- options: validationOptions,
1345
- validator: {
1346
- validate(value, args) {
1347
- return classValidator.isURL(value, options);
1348
- },
1349
- defaultMessage(args) {
1350
- return `invalid parameter ($property).`;
1351
- }
1352
- }
1353
- });
1354
- };
1355
- }
1356
- /**
1357
- * check if the string is a hash of type algorithm. Algorithm is one of ['md4', 'md5', 'sha1', 'sha256',
1358
- * 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
1359
- *
1360
- * @export
1361
- * @param {HashAlgorithm} algorithm
1362
- * @param {ValidationOptions} [validationOptions]
1363
- * @returns {PropertyDecorator}
1364
- */
1365
- function IsHash(algorithm, validationOptions) {
1366
- return function (object, propertyName) {
1367
- setExpose(object, propertyName);
1368
- classValidator.registerDecorator({
1369
- name: "vIsHash",
1370
- target: object.constructor,
1371
- propertyName,
1372
- options: validationOptions,
1373
- validator: {
1374
- validate(value, args) {
1375
- return classValidator.isHash(value, algorithm);
1376
- },
1377
- defaultMessage(args) {
1378
- return `invalid parameter, ($property) must be is an ${algorithm} Hash string.`;
1379
- }
1380
- }
1381
- });
1382
- };
676
+ /*
677
+ * @Description:
678
+ * @Usage:
679
+ * @Author: richen
680
+ * @Date: 2021-11-25 10:46:57
681
+ * @LastEditTime: 2023-12-16 14:23:12
682
+ */
683
+ /**
684
+ * Validation parameter's type and values.
685
+ *
686
+ * @export
687
+ * @param {(ValidRules | ValidRules[] | Function)} rule
688
+ * @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
689
+ * Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
690
+ * @returns {*} {ParameterDecorator}
691
+ */
692
+ function Valid(rule, options) {
693
+ let rules = [];
694
+ if (helper__namespace.isString(rule)) {
695
+ rules = rule.split(",");
696
+ }
697
+ else {
698
+ rules = rule;
699
+ }
700
+ return (target, propertyKey, descriptor) => {
701
+ var _a;
702
+ // 获取成员参数类型
703
+ const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
704
+ const type = ((_a = paramTypes[descriptor]) === null || _a === void 0 ? void 0 : _a.name) ? paramTypes[descriptor].name : 'object';
705
+ if (helper__namespace.isString(options)) {
706
+ options = { message: options, value: null };
707
+ }
708
+ koatty_container.IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
709
+ name: propertyKey,
710
+ rule: rules,
711
+ options,
712
+ index: descriptor,
713
+ type
714
+ }, target, propertyKey);
715
+ };
716
+ }
717
+ /**
718
+ * Validation parameter's type and values from DTO class.
719
+ *
720
+ * @export
721
+ * @returns {MethodDecorator}
722
+ */
723
+ function Validated() {
724
+ return (target, propertyKey, descriptor) => {
725
+ //
726
+ koatty_container.IOCContainer.savePropertyData(PARAM_CHECK_KEY, {
727
+ dtoCheck: 1
728
+ }, target, propertyKey);
729
+ // 获取成员参数类型
730
+ // const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey) || [];
731
+ // const { value, configurable, enumerable } = descriptor;
732
+ // descriptor = {
733
+ // configurable,
734
+ // enumerable,
735
+ // writable: true,
736
+ // value: async function valid(...props: any[]) {
737
+ // const ps: any[] = [];
738
+ // // tslint:disable-next-line: no-unused-expression
739
+ // (props || []).map((value: any, index: number) => {
740
+ // const type = (paramTypes[index] && paramTypes[index].name) ? paramTypes[index].name : "any";
741
+ // if (!paramterTypes[type]) {
742
+ // ps.push(ClassValidator.valid(paramTypes[index], value, true));
743
+ // } else {
744
+ // ps.push(Promise.resolve(value));
745
+ // }
746
+ // });
747
+ // if (ps.length > 0) {
748
+ // props = await Promise.all(ps);
749
+ // }
750
+ // // tslint:disable-next-line: no-invalid-this
751
+ // return value.apply(this, props);
752
+ // }
753
+ // };
754
+ // return descriptor;
755
+ };
756
+ }
757
+ /**
758
+ * Marks property as included in the process of transformation.
759
+ *
760
+ * @export
761
+ * @returns {PropertyDecorator}
762
+ */
763
+ function Expose() {
764
+ return function (object, propertyName) {
765
+ const types = Reflect.getMetadata("design:type", object, propertyName);
766
+ if (types) {
767
+ const originMap = koatty_container.getOriginMetadata(PARAM_TYPE_KEY, object);
768
+ originMap.set(propertyName, types.name);
769
+ }
770
+ };
771
+ }
772
+ /**
773
+ * Identifies that the field needs to be defined
774
+ *
775
+ * @export
776
+ * @returns {PropertyDecorator}
777
+ */
778
+ function IsDefined() {
779
+ return function (object, propertyName) {
780
+ setExpose(object, propertyName);
781
+ };
782
+ }
783
+ /**
784
+ * Checks if value is a chinese name.
785
+ *
786
+ * @export
787
+ * @param {string} property
788
+ * @param {ValidationOptions} [validationOptions]
789
+ * @returns {PropertyDecorator}
790
+ */
791
+ function IsCnName(validationOptions) {
792
+ return function (object, propertyName) {
793
+ setExpose(object, propertyName);
794
+ classValidator.registerDecorator({
795
+ name: "IsCnName",
796
+ target: object.constructor,
797
+ propertyName,
798
+ options: validationOptions,
799
+ validator: {
800
+ validate(value, args) {
801
+ return cnName(value);
802
+ },
803
+ defaultMessage(args) {
804
+ return "invalid parameter ($property).";
805
+ }
806
+ }
807
+ });
808
+ };
809
+ }
810
+ /**
811
+ * Checks if value is a idCard number(chinese).
812
+ *
813
+ * @export
814
+ * @param {string} property
815
+ * @param {ValidationOptions} [validationOptions]
816
+ * @returns {PropertyDecorator}
817
+ */
818
+ function IsIdNumber(validationOptions) {
819
+ return function (object, propertyName) {
820
+ setExpose(object, propertyName);
821
+ classValidator.registerDecorator({
822
+ name: "IsIdNumber",
823
+ target: object.constructor,
824
+ propertyName,
825
+ options: validationOptions,
826
+ validator: {
827
+ validate(value, args) {
828
+ return idNumber(value);
829
+ },
830
+ defaultMessage(args) {
831
+ return "invalid parameter ($property).";
832
+ }
833
+ }
834
+ });
835
+ };
836
+ }
837
+ /**
838
+ * Checks if value is a zipCode(chinese).
839
+ *
840
+ * @export
841
+ * @param {string} property
842
+ * @param {ValidationOptions} [validationOptions]
843
+ * @returns {PropertyDecorator}
844
+ */
845
+ function IsZipCode(validationOptions) {
846
+ return function (object, propertyName) {
847
+ setExpose(object, propertyName);
848
+ classValidator.registerDecorator({
849
+ name: "IsZipCode",
850
+ target: object.constructor,
851
+ propertyName,
852
+ options: validationOptions,
853
+ validator: {
854
+ validate(value, args) {
855
+ return zipCode(value);
856
+ },
857
+ defaultMessage(args) {
858
+ return "invalid parameter ($property).";
859
+ }
860
+ }
861
+ });
862
+ };
863
+ }
864
+ /**
865
+ * Checks if value is a mobile phone number(chinese).
866
+ *
867
+ * @export
868
+ * @param {string} property
869
+ * @param {ValidationOptions} [validationOptions]
870
+ * @returns {PropertyDecorator}
871
+ */
872
+ function IsMobile(validationOptions) {
873
+ return function (object, propertyName) {
874
+ setExpose(object, propertyName);
875
+ classValidator.registerDecorator({
876
+ name: "IsMobile",
877
+ target: object.constructor,
878
+ propertyName,
879
+ options: validationOptions,
880
+ validator: {
881
+ validate(value, args) {
882
+ return mobile(value);
883
+ },
884
+ defaultMessage(args) {
885
+ return "invalid parameter ($property).";
886
+ }
887
+ }
888
+ });
889
+ };
890
+ }
891
+ /**
892
+ * Checks if value is a plate number(chinese).
893
+ *
894
+ * @export
895
+ * @param {string} property
896
+ * @param {ValidationOptions} [validationOptions]
897
+ * @returns {PropertyDecorator}
898
+ */
899
+ function IsPlateNumber(validationOptions) {
900
+ return function (object, propertyName) {
901
+ setExpose(object, propertyName);
902
+ classValidator.registerDecorator({
903
+ name: "IsPlateNumber",
904
+ target: object.constructor,
905
+ propertyName,
906
+ options: validationOptions,
907
+ validator: {
908
+ validate(value, args) {
909
+ return plateNumber(value);
910
+ },
911
+ defaultMessage(args) {
912
+ return "invalid parameter ($property).";
913
+ }
914
+ }
915
+ });
916
+ };
917
+ }
918
+ /**
919
+ * Checks value is not empty, undefined, null, '', NaN, [], {} and any empty string(including spaces, tabs, formfeeds, etc.), returns false.
920
+ *
921
+ * @export
922
+ * @param {ValidationOptions} [validationOptions]
923
+ * @returns {PropertyDecorator}
924
+ */
925
+ function IsNotEmpty(validationOptions) {
926
+ return function (object, propertyName) {
927
+ setExpose(object, propertyName);
928
+ classValidator.registerDecorator({
929
+ name: "IsNotEmpty",
930
+ target: object.constructor,
931
+ propertyName,
932
+ options: validationOptions,
933
+ validator: {
934
+ validate(value, args) {
935
+ return !helper__namespace.isEmpty(value);
936
+ },
937
+ defaultMessage(args) {
938
+ return "invalid parameter ($property).";
939
+ }
940
+ }
941
+ });
942
+ };
943
+ }
944
+ /**
945
+ * Checks if value matches ("===") the comparison.
946
+ *
947
+ * @export
948
+ * @param {*} comparison
949
+ * @param {ValidationOptions} [validationOptions]
950
+ * @returns {PropertyDecorator}
951
+ */
952
+ function Equals(comparison, validationOptions) {
953
+ return function (object, propertyName) {
954
+ setExpose(object, propertyName);
955
+ classValidator.registerDecorator({
956
+ name: "vEquals",
957
+ target: object.constructor,
958
+ propertyName,
959
+ options: validationOptions,
960
+ validator: {
961
+ validate(value, args) {
962
+ return classValidator.equals(value, comparison);
963
+ },
964
+ defaultMessage(args) {
965
+ return `invalid parameter, ($property) must be equals ${comparison}.`;
966
+ }
967
+ }
968
+ });
969
+ };
970
+ }
971
+ /**
972
+ * Checks if value does not match ("!==") the comparison.
973
+ *
974
+ * @export
975
+ * @param {*} comparison
976
+ * @param {ValidationOptions} [validationOptions]
977
+ * @returns {PropertyDecorator}
978
+ */
979
+ function NotEquals(comparison, validationOptions) {
980
+ return function (object, propertyName) {
981
+ setExpose(object, propertyName);
982
+ classValidator.registerDecorator({
983
+ name: "vNotEquals",
984
+ target: object.constructor,
985
+ propertyName,
986
+ options: validationOptions,
987
+ validator: {
988
+ validate(value, args) {
989
+ return classValidator.notEquals(value, comparison);
990
+ },
991
+ defaultMessage(args) {
992
+ return `invalid parameter, ($property) must be not equals ${comparison}.`;
993
+ }
994
+ }
995
+ });
996
+ };
997
+ }
998
+ /**
999
+ * Checks if the string contains the seed.
1000
+ *
1001
+ * @export
1002
+ * @param {string} seed
1003
+ * @param {ValidationOptions} [validationOptions]
1004
+ * @returns {PropertyDecorator}
1005
+ */
1006
+ function Contains(seed, validationOptions) {
1007
+ return function (object, propertyName) {
1008
+ setExpose(object, propertyName);
1009
+ classValidator.registerDecorator({
1010
+ name: "vContains",
1011
+ target: object.constructor,
1012
+ propertyName,
1013
+ options: validationOptions,
1014
+ validator: {
1015
+ validate(value, args) {
1016
+ return classValidator.contains(value, seed);
1017
+ // return typeof value === "string" && (value.indexOf(seed) > -1);
1018
+ },
1019
+ defaultMessage(args) {
1020
+ return `invalid parameter, ($property) must be contains ${seed}.`;
1021
+ }
1022
+ }
1023
+ });
1024
+ };
1025
+ }
1026
+ /**
1027
+ * Checks if given value is in a array of allowed values.
1028
+ *
1029
+ * @export
1030
+ * @param {any[]} possibleValues
1031
+ * @param {ValidationOptions} [validationOptions]
1032
+ * @returns {PropertyDecorator}
1033
+ */
1034
+ function IsIn(possibleValues, validationOptions) {
1035
+ return function (object, propertyName) {
1036
+ setExpose(object, propertyName);
1037
+ classValidator.registerDecorator({
1038
+ name: "vIsIn",
1039
+ target: object.constructor,
1040
+ propertyName,
1041
+ options: validationOptions,
1042
+ validator: {
1043
+ validate(value, args) {
1044
+ return classValidator.isIn(value, possibleValues);
1045
+ },
1046
+ defaultMessage(args) {
1047
+ return `invalid parameter ($property).`;
1048
+ }
1049
+ }
1050
+ });
1051
+ };
1052
+ }
1053
+ /**
1054
+ * Checks if given value not in a array of allowed values.
1055
+ *
1056
+ * @export
1057
+ * @param {any[]} possibleValues
1058
+ * @param {ValidationOptions} [validationOptions]
1059
+ * @returns {PropertyDecorator}
1060
+ */
1061
+ function IsNotIn(possibleValues, validationOptions) {
1062
+ return function (object, propertyName) {
1063
+ setExpose(object, propertyName);
1064
+ classValidator.registerDecorator({
1065
+ name: "vIsNotIn",
1066
+ target: object.constructor,
1067
+ propertyName,
1068
+ options: validationOptions,
1069
+ validator: {
1070
+ validate(value, args) {
1071
+ return classValidator.isNotIn(value, possibleValues);
1072
+ },
1073
+ defaultMessage(args) {
1074
+ return `invalid parameter ($property).`;
1075
+ }
1076
+ }
1077
+ });
1078
+ };
1079
+ }
1080
+ /**
1081
+ * Checks if a given value is a real date.
1082
+ *
1083
+ * @export
1084
+ * @param {ValidationOptions} [validationOptions]
1085
+ * @returns {PropertyDecorator}
1086
+ */
1087
+ function IsDate(validationOptions) {
1088
+ return function (object, propertyName) {
1089
+ setExpose(object, propertyName);
1090
+ classValidator.registerDecorator({
1091
+ name: "vIsDate",
1092
+ target: object.constructor,
1093
+ propertyName,
1094
+ options: validationOptions,
1095
+ validator: {
1096
+ validate(value, args) {
1097
+ return classValidator.isDate(value);
1098
+ },
1099
+ defaultMessage(args) {
1100
+ return `invalid parameter ($property).`;
1101
+ }
1102
+ }
1103
+ });
1104
+ };
1105
+ }
1106
+ /**
1107
+ * Checks if the first number is greater than or equal to the min value.
1108
+ *
1109
+ * @export
1110
+ * @param {number} min
1111
+ * @param {ValidationOptions} [validationOptions]
1112
+ * @returns {PropertyDecorator}
1113
+ */
1114
+ function Gt(min, validationOptions) {
1115
+ return function (object, propertyName) {
1116
+ setExpose(object, propertyName);
1117
+ classValidator.registerDecorator({
1118
+ name: "vMin",
1119
+ target: object.constructor,
1120
+ propertyName,
1121
+ options: validationOptions,
1122
+ validator: {
1123
+ validate(value, args) {
1124
+ return helper__namespace.toNumber(value) > min;
1125
+ },
1126
+ defaultMessage(args) {
1127
+ return `invalid parameter ($property).`;
1128
+ }
1129
+ }
1130
+ });
1131
+ };
1132
+ }
1133
+ /**
1134
+ * Checks if the first number is less than or equal to the max value.
1135
+ *
1136
+ * @export
1137
+ * @param {number} max
1138
+ * @param {ValidationOptions} [validationOptions]
1139
+ * @returns {PropertyDecorator}
1140
+ */
1141
+ function Lt(max, validationOptions) {
1142
+ return function (object, propertyName) {
1143
+ setExpose(object, propertyName);
1144
+ classValidator.registerDecorator({
1145
+ name: "vMax",
1146
+ target: object.constructor,
1147
+ propertyName,
1148
+ options: validationOptions,
1149
+ validator: {
1150
+ validate(value, args) {
1151
+ return helper__namespace.toNumber(value) < max;
1152
+ },
1153
+ defaultMessage(args) {
1154
+ return `invalid parameter ($property).`;
1155
+ }
1156
+ }
1157
+ });
1158
+ };
1159
+ }
1160
+ /**
1161
+ * Checks if the first number is greater than or equal to the min value.
1162
+ *
1163
+ * @export
1164
+ * @param {number} min
1165
+ * @param {ValidationOptions} [validationOptions]
1166
+ * @returns {PropertyDecorator}
1167
+ */
1168
+ function Gte(min, validationOptions) {
1169
+ return function (object, propertyName) {
1170
+ setExpose(object, propertyName);
1171
+ classValidator.registerDecorator({
1172
+ name: "vMin",
1173
+ target: object.constructor,
1174
+ propertyName,
1175
+ options: validationOptions,
1176
+ validator: {
1177
+ validate(value, args) {
1178
+ return helper__namespace.toNumber(value) >= min;
1179
+ },
1180
+ defaultMessage(args) {
1181
+ return `invalid parameter ($property).`;
1182
+ }
1183
+ }
1184
+ });
1185
+ };
1186
+ }
1187
+ /**
1188
+ * Checks if the first number is less than or equal to the max value.
1189
+ *
1190
+ * @export
1191
+ * @param {number} max
1192
+ * @param {ValidationOptions} [validationOptions]
1193
+ * @returns {PropertyDecorator}
1194
+ */
1195
+ function Lte(max, validationOptions) {
1196
+ return function (object, propertyName) {
1197
+ setExpose(object, propertyName);
1198
+ classValidator.registerDecorator({
1199
+ name: "vMax",
1200
+ target: object.constructor,
1201
+ propertyName,
1202
+ options: validationOptions,
1203
+ validator: {
1204
+ validate(value, args) {
1205
+ return helper__namespace.toNumber(value) <= max;
1206
+ },
1207
+ defaultMessage(args) {
1208
+ return `invalid parameter ($property).`;
1209
+ }
1210
+ }
1211
+ });
1212
+ };
1213
+ }
1214
+ /**
1215
+ * Checks if the string's length falls in a range. Note: this function takes into account surrogate pairs.
1216
+ * If given value is not a string, then it returns false.
1217
+ *
1218
+ * @export
1219
+ * @param {number} min
1220
+ * @param {number} [max]
1221
+ * @param {ValidationOptions} [validationOptions]
1222
+ * @returns {PropertyDecorator}
1223
+ */
1224
+ function Length(min, max, validationOptions) {
1225
+ return function (object, propertyName) {
1226
+ setExpose(object, propertyName);
1227
+ classValidator.registerDecorator({
1228
+ name: "vLength",
1229
+ target: object.constructor,
1230
+ propertyName,
1231
+ options: validationOptions,
1232
+ validator: {
1233
+ validate(value, args) {
1234
+ return classValidator.length(value, min, max);
1235
+ },
1236
+ defaultMessage(args) {
1237
+ return `invalid parameter ($property).`;
1238
+ }
1239
+ }
1240
+ });
1241
+ };
1242
+ }
1243
+ /**
1244
+ * Checks if the string is an email. If given value is not a string, then it returns false.
1245
+ *
1246
+ * @export
1247
+ * @param {IsEmailOptions} [options]
1248
+ * @param {ValidationOptions} [validationOptions]
1249
+ * @returns {PropertyDecorator}
1250
+ */
1251
+ function IsEmail(options, validationOptions) {
1252
+ return function (object, propertyName) {
1253
+ setExpose(object, propertyName);
1254
+ classValidator.registerDecorator({
1255
+ name: "vIsEmail",
1256
+ target: object.constructor,
1257
+ propertyName,
1258
+ options: validationOptions,
1259
+ validator: {
1260
+ validate(value, args) {
1261
+ return classValidator.isEmail(value);
1262
+ },
1263
+ defaultMessage(args) {
1264
+ return `invalid parameter ($property).`;
1265
+ }
1266
+ }
1267
+ });
1268
+ };
1269
+ }
1270
+ /**
1271
+ * Checks if the string is an IP (version 4 or 6). If given value is not a string, then it returns false.
1272
+ *
1273
+ * @export
1274
+ * @param {number} [version]
1275
+ * @param {ValidationOptions} [validationOptions]
1276
+ * @returns {PropertyDecorator}
1277
+ */
1278
+ function IsIP(version, validationOptions) {
1279
+ return function (object, propertyName) {
1280
+ setExpose(object, propertyName);
1281
+ classValidator.registerDecorator({
1282
+ name: "vIsIP",
1283
+ target: object.constructor,
1284
+ propertyName,
1285
+ options: validationOptions,
1286
+ validator: {
1287
+ validate(value, args) {
1288
+ return classValidator.isIP(value, version);
1289
+ },
1290
+ defaultMessage(args) {
1291
+ return `invalid parameter ($property).`;
1292
+ }
1293
+ }
1294
+ });
1295
+ };
1296
+ }
1297
+ /**
1298
+ * Checks if the string is a valid phone number.
1299
+ *
1300
+ * @export
1301
+ * @param {string} {string} region 2 characters uppercase country code (e.g. DE, US, CH).
1302
+ * If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
1303
+ * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
1304
+ * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
1305
+ * @param {ValidationOptions} [validationOptions]
1306
+ * @returns {PropertyDecorator}
1307
+ */
1308
+ function IsPhoneNumber(region, validationOptions) {
1309
+ return function (object, propertyName) {
1310
+ setExpose(object, propertyName);
1311
+ classValidator.registerDecorator({
1312
+ name: "vIsPhoneNumber",
1313
+ target: object.constructor,
1314
+ propertyName,
1315
+ options: validationOptions,
1316
+ validator: {
1317
+ validate(value, args) {
1318
+ return classValidator.isPhoneNumber(value, region);
1319
+ },
1320
+ defaultMessage(args) {
1321
+ return `invalid parameter ($property).`;
1322
+ }
1323
+ }
1324
+ });
1325
+ };
1326
+ }
1327
+ /**
1328
+ * Checks if the string is an url.
1329
+ *
1330
+ * @export
1331
+ * @param {IsURLOptions} [options]
1332
+ * @param {ValidationOptions} [validationOptions]
1333
+ * @returns {PropertyDecorator}
1334
+ */
1335
+ function IsUrl(options, validationOptions) {
1336
+ return function (object, propertyName) {
1337
+ setExpose(object, propertyName);
1338
+ classValidator.registerDecorator({
1339
+ name: "vIsUrl",
1340
+ target: object.constructor,
1341
+ propertyName,
1342
+ options: validationOptions,
1343
+ validator: {
1344
+ validate(value, args) {
1345
+ return classValidator.isURL(value, options);
1346
+ },
1347
+ defaultMessage(args) {
1348
+ return `invalid parameter ($property).`;
1349
+ }
1350
+ }
1351
+ });
1352
+ };
1353
+ }
1354
+ /**
1355
+ * check if the string is a hash of type algorithm. Algorithm is one of ['md4', 'md5', 'sha1', 'sha256',
1356
+ * 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
1357
+ *
1358
+ * @export
1359
+ * @param {HashAlgorithm} algorithm
1360
+ * @param {ValidationOptions} [validationOptions]
1361
+ * @returns {PropertyDecorator}
1362
+ */
1363
+ function IsHash(algorithm, validationOptions) {
1364
+ return function (object, propertyName) {
1365
+ setExpose(object, propertyName);
1366
+ classValidator.registerDecorator({
1367
+ name: "vIsHash",
1368
+ target: object.constructor,
1369
+ propertyName,
1370
+ options: validationOptions,
1371
+ validator: {
1372
+ validate(value, args) {
1373
+ return classValidator.isHash(value, algorithm);
1374
+ },
1375
+ defaultMessage(args) {
1376
+ return `invalid parameter, ($property) must be is an ${algorithm} Hash string.`;
1377
+ }
1378
+ }
1379
+ });
1380
+ };
1383
1381
  }
1384
1382
 
1385
1383
  exports.ClassValidator = ClassValidator;