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