koatty_validation 1.6.0 → 1.6.1
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 +2 -0
- package/README.md +106 -3
- package/dist/README.md +106 -3
- package/dist/index.d.ts +66 -52
- package/dist/index.js +133 -104
- package/dist/index.mjs +134 -106
- package/dist/package.json +1 -1
- package/examples/validated-async-sync-example.ts +213 -0
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date: 2025-10-23
|
|
3
|
+
* @Date: 2025-10-23 20:54:39
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
7
7
|
*/
|
|
8
8
|
import * as helper from 'koatty_lib';
|
|
9
9
|
import 'reflect-metadata';
|
|
10
|
-
import { getOriginMetadata } from 'koatty_container';
|
|
10
|
+
import { getOriginMetadata, IOCContainer } from 'koatty_container';
|
|
11
11
|
import { LRUCache } from 'lru-cache';
|
|
12
12
|
import { validate, isNotIn, isIn, contains, notEquals, equals, isHash, isURL, isPhoneNumber, isIP, isEmail, registerDecorator } from 'class-validator';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
* koatty_validation
|
|
15
|
+
* koatty_validation type definitions
|
|
16
16
|
* @author richen
|
|
17
17
|
* @copyright Copyright (c) - <richenlin(at)gmail.com>
|
|
18
18
|
* @license MIT
|
|
19
19
|
*/
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Parameter type key constant
|
|
22
22
|
*/
|
|
23
23
|
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
26
|
+
* Performance cache module - Provides multi-level caching and performance monitoring
|
|
27
27
|
* @author richen
|
|
28
28
|
*/
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* Metadata cache
|
|
31
31
|
*/
|
|
32
32
|
class MetadataCache {
|
|
33
33
|
constructor() {
|
|
@@ -40,7 +40,7 @@ class MetadataCache {
|
|
|
40
40
|
return MetadataCache.instance;
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
|
-
*
|
|
43
|
+
* Get metadata cache for a class
|
|
44
44
|
*/
|
|
45
45
|
getClassCache(target) {
|
|
46
46
|
if (!this.cache.has(target)) {
|
|
@@ -49,28 +49,28 @@ class MetadataCache {
|
|
|
49
49
|
return this.cache.get(target);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* Cache metadata
|
|
53
53
|
*/
|
|
54
54
|
setMetadata(target, key, value) {
|
|
55
55
|
const classCache = this.getClassCache(target);
|
|
56
56
|
classCache.set(key, value);
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
59
|
+
* Get cached metadata
|
|
60
60
|
*/
|
|
61
61
|
getMetadata(target, key) {
|
|
62
62
|
const classCache = this.getClassCache(target);
|
|
63
63
|
return classCache.get(key);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
|
-
*
|
|
66
|
+
* Check if metadata is cached
|
|
67
67
|
*/
|
|
68
68
|
hasMetadata(target, key) {
|
|
69
69
|
const classCache = this.getClassCache(target);
|
|
70
70
|
return classCache.has(key);
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
|
-
*
|
|
73
|
+
* Clear cache for a specific class
|
|
74
74
|
*/
|
|
75
75
|
clearClassCache(target) {
|
|
76
76
|
if (this.cache.has(target)) {
|
|
@@ -79,7 +79,7 @@ class MetadataCache {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
82
|
+
* Validation result cache
|
|
83
83
|
*/
|
|
84
84
|
class ValidationCache {
|
|
85
85
|
constructor(options) {
|
|
@@ -87,7 +87,7 @@ class ValidationCache {
|
|
|
87
87
|
this.misses = 0;
|
|
88
88
|
this.cache = new LRUCache({
|
|
89
89
|
max: (options === null || options === void 0 ? void 0 : options.max) || 5000,
|
|
90
|
-
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 10, // 10
|
|
90
|
+
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 10, // 10 minutes
|
|
91
91
|
allowStale: (options === null || options === void 0 ? void 0 : options.allowStale) || false,
|
|
92
92
|
updateAgeOnGet: (options === null || options === void 0 ? void 0 : options.updateAgeOnGet) || true,
|
|
93
93
|
});
|
|
@@ -205,7 +205,7 @@ class RegexCache {
|
|
|
205
205
|
constructor(options) {
|
|
206
206
|
this.cache = new LRUCache({
|
|
207
207
|
max: (options === null || options === void 0 ? void 0 : options.max) || 200,
|
|
208
|
-
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 30, // 30
|
|
208
|
+
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 30, // 30 minutes
|
|
209
209
|
allowStale: (options === null || options === void 0 ? void 0 : options.allowStale) || false,
|
|
210
210
|
updateAgeOnGet: (options === null || options === void 0 ? void 0 : options.updateAgeOnGet) || true,
|
|
211
211
|
});
|
|
@@ -389,7 +389,7 @@ function cached(validator, ttl) {
|
|
|
389
389
|
try {
|
|
390
390
|
const result = originalMethod.apply(this, args);
|
|
391
391
|
validationCache.set(validator, value, result, ...additionalArgs);
|
|
392
|
-
//
|
|
392
|
+
// If TTL is specified, set expiration time
|
|
393
393
|
if (ttl && ttl > 0) {
|
|
394
394
|
validationCache.setTTL(validator, value, ttl, ...additionalArgs);
|
|
395
395
|
}
|
|
@@ -1114,24 +1114,24 @@ const FunctionValidator = {
|
|
|
1114
1114
|
};
|
|
1115
1115
|
|
|
1116
1116
|
/**
|
|
1117
|
-
*
|
|
1117
|
+
* Decorator Factory - Eliminate decorator code duplication
|
|
1118
1118
|
* @author richen
|
|
1119
1119
|
*/
|
|
1120
1120
|
/**
|
|
1121
|
-
*
|
|
1122
|
-
* @param options
|
|
1123
|
-
* @returns
|
|
1121
|
+
* Factory function to create validation decorators
|
|
1122
|
+
* @param options Decorator configuration options
|
|
1123
|
+
* @returns Decorator factory function
|
|
1124
1124
|
*/
|
|
1125
1125
|
function createValidationDecorator(options) {
|
|
1126
1126
|
const { name, validator, defaultMessage, requiresValue = false } = options;
|
|
1127
1127
|
return function decoratorFactory(...args) {
|
|
1128
|
-
//
|
|
1128
|
+
// Handle parameters: last parameter is ValidationOptions, previous ones are validator function parameters
|
|
1129
1129
|
const validationOptions = args[args.length - 1];
|
|
1130
1130
|
const validatorArgs = requiresValue ? args.slice(0, -1) : [];
|
|
1131
1131
|
return function propertyDecorator(object, propertyName) {
|
|
1132
|
-
//
|
|
1132
|
+
// Set property as exportable
|
|
1133
1133
|
setExpose(object, propertyName);
|
|
1134
|
-
//
|
|
1134
|
+
// Register validation decorator
|
|
1135
1135
|
registerDecorator({
|
|
1136
1136
|
name,
|
|
1137
1137
|
target: object.constructor,
|
|
@@ -1159,11 +1159,11 @@ function createValidationDecorator(options) {
|
|
|
1159
1159
|
};
|
|
1160
1160
|
}
|
|
1161
1161
|
/**
|
|
1162
|
-
*
|
|
1163
|
-
* @param name
|
|
1164
|
-
* @param validator
|
|
1165
|
-
* @param defaultMessage
|
|
1166
|
-
* @returns
|
|
1162
|
+
* Create simple validation decorator (no additional parameters required)
|
|
1163
|
+
* @param name Decorator name
|
|
1164
|
+
* @param validator Validation function
|
|
1165
|
+
* @param defaultMessage Default error message
|
|
1166
|
+
* @returns Decorator function
|
|
1167
1167
|
*/
|
|
1168
1168
|
function createSimpleDecorator(name, validator, defaultMessage) {
|
|
1169
1169
|
return createValidationDecorator({
|
|
@@ -1174,11 +1174,11 @@ function createSimpleDecorator(name, validator, defaultMessage) {
|
|
|
1174
1174
|
});
|
|
1175
1175
|
}
|
|
1176
1176
|
/**
|
|
1177
|
-
*
|
|
1178
|
-
* @param name
|
|
1179
|
-
* @param validator
|
|
1180
|
-
* @param defaultMessage
|
|
1181
|
-
* @returns
|
|
1177
|
+
* Create parameterized validation decorator
|
|
1178
|
+
* @param name Decorator name
|
|
1179
|
+
* @param validator Validation function
|
|
1180
|
+
* @param defaultMessage Default error message
|
|
1181
|
+
* @returns Decorator factory function
|
|
1182
1182
|
*/
|
|
1183
1183
|
function createParameterizedDecorator(name, validator, defaultMessage) {
|
|
1184
1184
|
return createValidationDecorator({
|
|
@@ -1190,21 +1190,21 @@ function createParameterizedDecorator(name, validator, defaultMessage) {
|
|
|
1190
1190
|
}
|
|
1191
1191
|
|
|
1192
1192
|
/**
|
|
1193
|
-
*
|
|
1193
|
+
* Improved error handling mechanism
|
|
1194
1194
|
* @author richen
|
|
1195
1195
|
*/
|
|
1196
1196
|
/**
|
|
1197
|
-
*
|
|
1197
|
+
* Error message internationalization
|
|
1198
1198
|
*/
|
|
1199
1199
|
const ERROR_MESSAGES = {
|
|
1200
1200
|
zh: {
|
|
1201
|
-
//
|
|
1201
|
+
// Chinese localization validation
|
|
1202
1202
|
IsCnName: '必须是有效的中文姓名',
|
|
1203
1203
|
IsIdNumber: '必须是有效的身份证号码',
|
|
1204
1204
|
IsZipCode: '必须是有效的邮政编码',
|
|
1205
1205
|
IsMobile: '必须是有效的手机号码',
|
|
1206
1206
|
IsPlateNumber: '必须是有效的车牌号码',
|
|
1207
|
-
//
|
|
1207
|
+
// Basic validation
|
|
1208
1208
|
IsNotEmpty: '不能为空',
|
|
1209
1209
|
IsDate: '必须是有效的日期',
|
|
1210
1210
|
IsEmail: '必须是有效的邮箱地址',
|
|
@@ -1212,7 +1212,7 @@ const ERROR_MESSAGES = {
|
|
|
1212
1212
|
IsPhoneNumber: '必须是有效的电话号码',
|
|
1213
1213
|
IsUrl: '必须是有效的URL地址',
|
|
1214
1214
|
IsHash: '必须是有效的哈希值',
|
|
1215
|
-
//
|
|
1215
|
+
// Comparison validation
|
|
1216
1216
|
Equals: '必须等于 {comparison}',
|
|
1217
1217
|
NotEquals: '不能等于 {comparison}',
|
|
1218
1218
|
Contains: '必须包含 {seed}',
|
|
@@ -1222,7 +1222,7 @@ const ERROR_MESSAGES = {
|
|
|
1222
1222
|
Gte: '必须大于或等于 {min}',
|
|
1223
1223
|
Lt: '必须小于 {max}',
|
|
1224
1224
|
Lte: '必须小于或等于 {max}',
|
|
1225
|
-
//
|
|
1225
|
+
// Common errors
|
|
1226
1226
|
invalidParameter: '参数 {field} 无效',
|
|
1227
1227
|
validationFailed: '验证失败',
|
|
1228
1228
|
},
|
|
@@ -1257,7 +1257,7 @@ const ERROR_MESSAGES = {
|
|
|
1257
1257
|
}
|
|
1258
1258
|
};
|
|
1259
1259
|
/**
|
|
1260
|
-
*
|
|
1260
|
+
* Enhanced validation error class
|
|
1261
1261
|
*/
|
|
1262
1262
|
class KoattyValidationError extends Error {
|
|
1263
1263
|
constructor(errors, message) {
|
|
@@ -1267,23 +1267,23 @@ class KoattyValidationError extends Error {
|
|
|
1267
1267
|
this.errors = errors;
|
|
1268
1268
|
this.statusCode = 400;
|
|
1269
1269
|
this.timestamp = new Date();
|
|
1270
|
-
//
|
|
1270
|
+
// Ensure correct prototype chain
|
|
1271
1271
|
Object.setPrototypeOf(this, KoattyValidationError.prototype);
|
|
1272
1272
|
}
|
|
1273
1273
|
/**
|
|
1274
|
-
*
|
|
1274
|
+
* Get the first error message
|
|
1275
1275
|
*/
|
|
1276
1276
|
getFirstError() {
|
|
1277
1277
|
return this.errors[0];
|
|
1278
1278
|
}
|
|
1279
1279
|
/**
|
|
1280
|
-
*
|
|
1280
|
+
* Get errors for a specific field
|
|
1281
1281
|
*/
|
|
1282
1282
|
getFieldErrors(field) {
|
|
1283
1283
|
return this.errors.filter(error => error.field === field);
|
|
1284
1284
|
}
|
|
1285
1285
|
/**
|
|
1286
|
-
*
|
|
1286
|
+
* Convert to JSON format
|
|
1287
1287
|
*/
|
|
1288
1288
|
toJSON() {
|
|
1289
1289
|
return {
|
|
@@ -1296,7 +1296,7 @@ class KoattyValidationError extends Error {
|
|
|
1296
1296
|
}
|
|
1297
1297
|
}
|
|
1298
1298
|
/**
|
|
1299
|
-
*
|
|
1299
|
+
* Error message formatter
|
|
1300
1300
|
*/
|
|
1301
1301
|
class ErrorMessageFormatter {
|
|
1302
1302
|
constructor(language = 'zh') {
|
|
@@ -1304,33 +1304,33 @@ class ErrorMessageFormatter {
|
|
|
1304
1304
|
this.language = language;
|
|
1305
1305
|
}
|
|
1306
1306
|
/**
|
|
1307
|
-
*
|
|
1307
|
+
* Set language
|
|
1308
1308
|
*/
|
|
1309
1309
|
setLanguage(language) {
|
|
1310
1310
|
this.language = language;
|
|
1311
1311
|
}
|
|
1312
1312
|
/**
|
|
1313
|
-
*
|
|
1313
|
+
* Format error message
|
|
1314
1314
|
*/
|
|
1315
1315
|
formatMessage(constraint, field, value, context) {
|
|
1316
1316
|
const messages = ERROR_MESSAGES[this.language];
|
|
1317
1317
|
let template = messages[constraint] || messages.invalidParameter;
|
|
1318
|
-
//
|
|
1318
|
+
// Replace placeholders
|
|
1319
1319
|
template = template.replace('{field}', field);
|
|
1320
|
-
//
|
|
1320
|
+
// Prioritize values from context, then use passed value
|
|
1321
1321
|
if (context) {
|
|
1322
1322
|
Object.entries(context).forEach(([key, val]) => {
|
|
1323
1323
|
template = template.replace(`{${key}}`, this.formatValue(val));
|
|
1324
1324
|
});
|
|
1325
1325
|
}
|
|
1326
|
-
//
|
|
1326
|
+
// If there's still a {value} placeholder and value was passed, replace it
|
|
1327
1327
|
if (value !== undefined && template.includes('{value}')) {
|
|
1328
1328
|
template = template.replace('{value}', this.formatValue(value));
|
|
1329
1329
|
}
|
|
1330
1330
|
return template;
|
|
1331
1331
|
}
|
|
1332
1332
|
/**
|
|
1333
|
-
*
|
|
1333
|
+
* Format value for message display
|
|
1334
1334
|
* @private
|
|
1335
1335
|
*/
|
|
1336
1336
|
formatValue(value) {
|
|
@@ -1349,7 +1349,7 @@ class ErrorMessageFormatter {
|
|
|
1349
1349
|
return JSON.stringify(value);
|
|
1350
1350
|
}
|
|
1351
1351
|
catch {
|
|
1352
|
-
//
|
|
1352
|
+
// Handle circular references
|
|
1353
1353
|
return '[Circular Reference]';
|
|
1354
1354
|
}
|
|
1355
1355
|
}
|
|
@@ -1357,17 +1357,17 @@ class ErrorMessageFormatter {
|
|
|
1357
1357
|
}
|
|
1358
1358
|
}
|
|
1359
1359
|
/**
|
|
1360
|
-
*
|
|
1360
|
+
* Global error message formatter instance
|
|
1361
1361
|
*/
|
|
1362
1362
|
const errorFormatter = new ErrorMessageFormatter();
|
|
1363
1363
|
/**
|
|
1364
|
-
*
|
|
1364
|
+
* Set global language
|
|
1365
1365
|
*/
|
|
1366
1366
|
function setValidationLanguage(language) {
|
|
1367
1367
|
errorFormatter.setLanguage(language);
|
|
1368
1368
|
}
|
|
1369
1369
|
/**
|
|
1370
|
-
*
|
|
1370
|
+
* Create validation error
|
|
1371
1371
|
*/
|
|
1372
1372
|
function createValidationError(field, value, constraint, customMessage, context) {
|
|
1373
1373
|
const message = customMessage || errorFormatter.formatMessage(constraint, field, value, context);
|
|
@@ -1380,7 +1380,7 @@ function createValidationError(field, value, constraint, customMessage, context)
|
|
|
1380
1380
|
};
|
|
1381
1381
|
}
|
|
1382
1382
|
/**
|
|
1383
|
-
*
|
|
1383
|
+
* Create validation errors in batch
|
|
1384
1384
|
*/
|
|
1385
1385
|
function createValidationErrors(errors) {
|
|
1386
1386
|
const validationErrors = errors.map(error => createValidationError(error.field, error.value, error.constraint, error.message, error.context));
|
|
@@ -1388,19 +1388,19 @@ function createValidationErrors(errors) {
|
|
|
1388
1388
|
}
|
|
1389
1389
|
|
|
1390
1390
|
/**
|
|
1391
|
-
*
|
|
1391
|
+
* Refactored decorator definitions - Using factory functions to eliminate duplication
|
|
1392
1392
|
* @author richen
|
|
1393
1393
|
*/
|
|
1394
|
-
//
|
|
1394
|
+
// Chinese localization validation decorators
|
|
1395
1395
|
const IsCnName = createSimpleDecorator('IsCnName', (value) => helper.isString(value) && cnName(value), 'must be a valid Chinese name');
|
|
1396
1396
|
const IsIdNumber = createSimpleDecorator('IsIdNumber', (value) => helper.isString(value) && idNumber(value), 'must be a valid ID number');
|
|
1397
1397
|
const IsZipCode = createSimpleDecorator('IsZipCode', (value) => helper.isString(value) && zipCode(value), 'must be a valid zip code');
|
|
1398
1398
|
const IsMobile = createSimpleDecorator('IsMobile', (value) => helper.isString(value) && mobile(value), 'must be a valid mobile number');
|
|
1399
1399
|
const IsPlateNumber = createSimpleDecorator('IsPlateNumber', (value) => helper.isString(value) && plateNumber(value), 'must be a valid plate number');
|
|
1400
|
-
//
|
|
1400
|
+
// Basic validation decorators
|
|
1401
1401
|
const IsNotEmpty = createSimpleDecorator('IsNotEmpty', (value) => !helper.isEmpty(value), 'should not be empty');
|
|
1402
1402
|
const IsDate = createSimpleDecorator('IsDate', (value) => helper.isDate(value), 'must be a valid date');
|
|
1403
|
-
//
|
|
1403
|
+
// Parameterized validation decorators
|
|
1404
1404
|
const Equals = createParameterizedDecorator('Equals', (value, comparison) => value === comparison, 'must equal to $constraint1');
|
|
1405
1405
|
const NotEquals = createParameterizedDecorator('NotEquals', (value, comparison) => value !== comparison, 'should not equal to $constraint1');
|
|
1406
1406
|
const Contains = createParameterizedDecorator('Contains', (value, seed) => helper.isString(value) && value.includes(seed), 'must contain $constraint1');
|
|
@@ -1427,9 +1427,9 @@ function IsUrl(options, validationOptions) {
|
|
|
1427
1427
|
function IsHash(algorithm, validationOptions) {
|
|
1428
1428
|
return createParameterizedDecorator('IsHash', (value) => isHash(value, algorithm), 'must be a valid hash')(validationOptions);
|
|
1429
1429
|
}
|
|
1430
|
-
//
|
|
1430
|
+
// Basic utility decorators (migrated from original decorator.ts)
|
|
1431
1431
|
/**
|
|
1432
|
-
*
|
|
1432
|
+
* Mark property as exportable
|
|
1433
1433
|
*/
|
|
1434
1434
|
function Expose() {
|
|
1435
1435
|
return function (object, propertyName) {
|
|
@@ -1437,7 +1437,7 @@ function Expose() {
|
|
|
1437
1437
|
};
|
|
1438
1438
|
}
|
|
1439
1439
|
/**
|
|
1440
|
-
* Expose
|
|
1440
|
+
* Alias for Expose
|
|
1441
1441
|
*/
|
|
1442
1442
|
function IsDefined() {
|
|
1443
1443
|
return function (object, propertyName) {
|
|
@@ -1445,63 +1445,91 @@ function IsDefined() {
|
|
|
1445
1445
|
};
|
|
1446
1446
|
}
|
|
1447
1447
|
/**
|
|
1448
|
-
*
|
|
1448
|
+
* Parameter validation decorator
|
|
1449
1449
|
*/
|
|
1450
1450
|
function Valid(rule, options) {
|
|
1451
1451
|
return function (object, propertyName, parameterIndex) {
|
|
1452
|
-
//
|
|
1452
|
+
// Keep consistent with original implementation
|
|
1453
1453
|
const existingRules = Reflect.getOwnMetadata("validate", object, propertyName) || {};
|
|
1454
1454
|
existingRules[parameterIndex] = { rule, options };
|
|
1455
1455
|
Reflect.defineMetadata("validate", existingRules, object, propertyName);
|
|
1456
1456
|
};
|
|
1457
1457
|
}
|
|
1458
1458
|
/**
|
|
1459
|
-
*
|
|
1460
|
-
*
|
|
1459
|
+
* Synchronous validation function - Executes the actual validation logic
|
|
1460
|
+
* @param args Method parameters
|
|
1461
|
+
* @param paramTypes Parameter type metadata
|
|
1462
|
+
* @returns Validated parameters and validation targets
|
|
1461
1463
|
*/
|
|
1462
|
-
function
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
constraint: Object.keys(e.constraints || {})[0] || 'unknown',
|
|
1489
|
-
message: Object.values(e.constraints || {})[0] || 'Validation failed',
|
|
1490
|
-
context: e.constraints
|
|
1491
|
-
})));
|
|
1492
|
-
}
|
|
1493
|
-
}
|
|
1494
|
-
catch (error) {
|
|
1495
|
-
// 如果验证失败,重新抛出错误
|
|
1496
|
-
throw error;
|
|
1497
|
-
}
|
|
1464
|
+
async function checkValidated(args, paramTypes) {
|
|
1465
|
+
const validationTargets = [];
|
|
1466
|
+
// Validate each parameter
|
|
1467
|
+
for (let i = 0; i < args.length; i++) {
|
|
1468
|
+
const arg = args[i];
|
|
1469
|
+
const paramType = paramTypes[i];
|
|
1470
|
+
// If it's a class type and not a basic type, perform validation
|
|
1471
|
+
if (paramType && typeof paramType === 'function' &&
|
|
1472
|
+
paramType !== String && paramType !== Number &&
|
|
1473
|
+
paramType !== Boolean && paramType !== Array &&
|
|
1474
|
+
paramType !== Object && paramType !== Date) {
|
|
1475
|
+
try {
|
|
1476
|
+
// If parameter is not an instance of the target type, convert it to an instance
|
|
1477
|
+
let validationTarget = arg;
|
|
1478
|
+
if (!(arg instanceof paramType)) {
|
|
1479
|
+
validationTarget = Object.assign(new paramType(), arg);
|
|
1480
|
+
}
|
|
1481
|
+
const errors = await validate(validationTarget);
|
|
1482
|
+
if (errors.length > 0) {
|
|
1483
|
+
throw createValidationErrors(errors.map(e => ({
|
|
1484
|
+
field: e.property,
|
|
1485
|
+
value: e.value,
|
|
1486
|
+
constraint: Object.keys(e.constraints || {})[0] || 'unknown',
|
|
1487
|
+
message: Object.values(e.constraints || {})[0] || 'Validation failed',
|
|
1488
|
+
context: e.constraints
|
|
1489
|
+
})));
|
|
1498
1490
|
}
|
|
1491
|
+
validationTargets.push(validationTarget);
|
|
1499
1492
|
}
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1493
|
+
catch (error) {
|
|
1494
|
+
// If validation fails, rethrow the error
|
|
1495
|
+
throw error;
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
else {
|
|
1499
|
+
validationTargets.push(arg);
|
|
1500
|
+
}
|
|
1501
|
+
}
|
|
1502
|
+
return { validatedArgs: args, validationTargets };
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* Method validation decorator
|
|
1506
|
+
* Automatically validates DTO objects in method parameters
|
|
1507
|
+
* @param isAsync Whether to use async validation mode, default is true
|
|
1508
|
+
* - true: Async mode, validation is handled by IOC container in the framework (suitable for scenarios where parameter values need to be obtained asynchronously)
|
|
1509
|
+
* - false: Sync mode, validation is performed immediately when the method is called (suitable for scenarios where parameter values are already prepared)
|
|
1510
|
+
*/
|
|
1511
|
+
function Validated(isAsync = true) {
|
|
1512
|
+
return function (target, propertyKey, descriptor) {
|
|
1513
|
+
if (isAsync) {
|
|
1514
|
+
// Async mode: Save metadata, validation will be performed by the framework after async parameter retrieval
|
|
1515
|
+
IOCContainer.savePropertyData(PARAM_CHECK_KEY, {
|
|
1516
|
+
dtoCheck: 1
|
|
1517
|
+
}, target, propertyKey);
|
|
1518
|
+
}
|
|
1519
|
+
else {
|
|
1520
|
+
// Sync mode: Perform validation immediately when the method is called
|
|
1521
|
+
const originalMethod = descriptor.value;
|
|
1522
|
+
descriptor.value = async function (...args) {
|
|
1523
|
+
// Get parameter type metadata
|
|
1524
|
+
const paramTypes = Reflect.getMetadata('design:paramtypes', target, propertyKey) || [];
|
|
1525
|
+
// Execute validation
|
|
1526
|
+
await checkValidated(args, paramTypes);
|
|
1527
|
+
// Execute original method
|
|
1528
|
+
return originalMethod.apply(this, args);
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1503
1531
|
return descriptor;
|
|
1504
1532
|
};
|
|
1505
1533
|
}
|
|
1506
1534
|
|
|
1507
|
-
export { ClassValidator, Contains, ENABLE_VALIDATED, ERROR_MESSAGES, Equals, ErrorMessageFormatter, Expose, FunctionValidator, Gt, Gte, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, KoattyValidationError, Lt, Lte, NotEquals, PARAM_CHECK_KEY, PARAM_RULE_KEY, PARAM_TYPE_KEY, Valid, ValidFuncs, Validated, cached, checkParamsType, clearAllCaches, configureCaches, convertDtoParamsType, convertParamsType, createParameterizedDecorator, createSimpleDecorator, createValidationDecorator, createValidationError, createValidationErrors, errorFormatter, getAllCacheStats, metadataCache, paramterTypes, performanceMonitor, plainToClass, regexCache, setValidationLanguage, validationCache, warmupCaches };
|
|
1535
|
+
export { ClassValidator, Contains, ENABLE_VALIDATED, ERROR_MESSAGES, Equals, ErrorMessageFormatter, Expose, FunctionValidator, Gt, Gte, IsCnName, IsDate, IsDefined, IsEmail, IsHash, IsIP, IsIdNumber, IsIn, IsMobile, IsNotEmpty, IsNotIn, IsPhoneNumber, IsPlateNumber, IsUrl, IsZipCode, KoattyValidationError, Lt, Lte, NotEquals, PARAM_CHECK_KEY, PARAM_RULE_KEY, PARAM_TYPE_KEY, Valid, ValidFuncs, Validated, cached, checkParamsType, checkValidated, clearAllCaches, configureCaches, convertDtoParamsType, convertParamsType, createParameterizedDecorator, createSimpleDecorator, createValidationDecorator, createValidationError, createValidationErrors, errorFormatter, getAllCacheStats, metadataCache, paramterTypes, performanceMonitor, plainToClass, regexCache, setValidationLanguage, validationCache, warmupCaches };
|
package/dist/package.json
CHANGED