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