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