koatty_validation 1.2.9 → 1.3.0

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