koatty_validation 1.3.4 → 1.4.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/.rollup.config.js +62 -62
- package/.vscode/launch.json +81 -0
- package/CHANGELOG.md +83 -73
- package/LICENSE +29 -29
- package/README.md +363 -116
- package/coverage.lcov +1607 -0
- package/dist/LICENSE +29 -29
- package/dist/README.md +363 -116
- package/dist/index.d.ts +421 -219
- package/dist/index.js +785 -651
- package/dist/index.mjs +767 -649
- package/dist/package.json +91 -91
- package/examples/README.md +90 -0
- package/examples/basic-usage.ts +239 -0
- package/examples/custom-decorators-example.ts +230 -0
- package/examples/usage-example.ts +284 -0
- package/package.json +91 -91
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date:
|
|
3
|
+
* @Date: 2025-06-10 23:32:27
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
7
7
|
*/
|
|
8
|
-
import { validate, isEmail, isIP, isPhoneNumber, isURL, isHash, equals, notEquals, contains, isIn, isNotIn, registerDecorator, isDate, length } from 'class-validator';
|
|
9
8
|
import * as helper from 'koatty_lib';
|
|
10
|
-
import { getOriginMetadata, IOCContainer } from 'koatty_container';
|
|
11
9
|
import 'reflect-metadata';
|
|
10
|
+
import { getOriginMetadata } from 'koatty_container';
|
|
11
|
+
import { validate, isNotIn, isIn, contains, notEquals, equals, isHash, isURL, isPhoneNumber, isIP, isEmail, registerDecorator } from 'class-validator';
|
|
12
|
+
import { LRUCache } from 'lru-cache';
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* @ author: richen
|
|
@@ -17,6 +18,8 @@ import 'reflect-metadata';
|
|
|
17
18
|
* @ version: 2020-03-20 11:34:38
|
|
18
19
|
*/
|
|
19
20
|
// tslint:disable-next-line: no-import-side-effect
|
|
21
|
+
// 参数类型键常量
|
|
22
|
+
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
20
23
|
/**
|
|
21
24
|
* Set property as included in the process of transformation.
|
|
22
25
|
*
|
|
@@ -185,7 +188,7 @@ function convertParamsType(param, type) {
|
|
|
185
188
|
}
|
|
186
189
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
187
190
|
}
|
|
188
|
-
catch (
|
|
191
|
+
catch (_err) {
|
|
189
192
|
return param;
|
|
190
193
|
}
|
|
191
194
|
}
|
|
@@ -333,10 +336,9 @@ function plateNumber(value) {
|
|
|
333
336
|
* @Usage:
|
|
334
337
|
* @Author: richen
|
|
335
338
|
* @Date: 2021-11-25 10:47:04
|
|
336
|
-
* @LastEditTime: 2024-
|
|
339
|
+
* @LastEditTime: 2024-01-03 14:32:49
|
|
337
340
|
*/
|
|
338
341
|
// constant
|
|
339
|
-
const PARAM_TYPE_KEY = 'PARAM_TYPE_KEY';
|
|
340
342
|
const PARAM_RULE_KEY = 'PARAM_RULE_KEY';
|
|
341
343
|
const PARAM_CHECK_KEY = 'PARAM_CHECK_KEY';
|
|
342
344
|
const ENABLE_VALIDATED = "ENABLE_VALIDATED";
|
|
@@ -392,10 +394,20 @@ class ValidateClass {
|
|
|
392
394
|
* @memberof ValidateClass
|
|
393
395
|
*/
|
|
394
396
|
async valid(Clazz, data, convert = false) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
397
|
+
let obj = {};
|
|
398
|
+
if (data instanceof Clazz) {
|
|
399
|
+
obj = data;
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
obj = plainToClass(Clazz, data, convert);
|
|
403
|
+
}
|
|
404
|
+
let errors = [];
|
|
405
|
+
if (convert) {
|
|
406
|
+
errors = await validate(obj);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
errors = await validate(obj, { skipMissingProperties: true });
|
|
410
|
+
}
|
|
399
411
|
if (errors.length > 0) {
|
|
400
412
|
throw new Error(Object.values(errors[0].constraints)[0]);
|
|
401
413
|
}
|
|
@@ -568,744 +580,850 @@ const ValidFuncs = {
|
|
|
568
580
|
* @param {(string | ValidOtpions)} [options]
|
|
569
581
|
* @returns {*}
|
|
570
582
|
*/
|
|
571
|
-
|
|
583
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
584
|
+
const FunctionValidator = {
|
|
585
|
+
IsNotEmpty: function (_value, _options) {
|
|
586
|
+
throw new Error("Function not implemented.");
|
|
587
|
+
},
|
|
588
|
+
IsDate: function (_value, _options) {
|
|
589
|
+
throw new Error("Function not implemented.");
|
|
590
|
+
},
|
|
591
|
+
IsEmail: function (_value, _options) {
|
|
592
|
+
throw new Error("Function not implemented.");
|
|
593
|
+
},
|
|
594
|
+
IsIP: function (_value, _options) {
|
|
595
|
+
throw new Error("Function not implemented.");
|
|
596
|
+
},
|
|
597
|
+
IsPhoneNumber: function (_value, _options) {
|
|
598
|
+
throw new Error("Function not implemented.");
|
|
599
|
+
},
|
|
600
|
+
IsUrl: function (_value, _options) {
|
|
601
|
+
throw new Error("Function not implemented.");
|
|
602
|
+
},
|
|
603
|
+
IsHash: function (_value, _options) {
|
|
604
|
+
throw new Error("Function not implemented.");
|
|
605
|
+
},
|
|
606
|
+
IsCnName: function (_value, _options) {
|
|
607
|
+
throw new Error("Function not implemented.");
|
|
608
|
+
},
|
|
609
|
+
IsIdNumber: function (_value, _options) {
|
|
610
|
+
throw new Error("Function not implemented.");
|
|
611
|
+
},
|
|
612
|
+
IsZipCode: function (_value, _options) {
|
|
613
|
+
throw new Error("Function not implemented.");
|
|
614
|
+
},
|
|
615
|
+
IsMobile: function (_value, _options) {
|
|
616
|
+
throw new Error("Function not implemented.");
|
|
617
|
+
},
|
|
618
|
+
IsPlateNumber: function (_value, _options) {
|
|
619
|
+
throw new Error("Function not implemented.");
|
|
620
|
+
},
|
|
621
|
+
Equals: function (_value, _options) {
|
|
622
|
+
throw new Error("Function not implemented.");
|
|
623
|
+
},
|
|
624
|
+
NotEquals: function (_value, _options) {
|
|
625
|
+
throw new Error("Function not implemented.");
|
|
626
|
+
},
|
|
627
|
+
Contains: function (_value, _options) {
|
|
628
|
+
throw new Error("Function not implemented.");
|
|
629
|
+
},
|
|
630
|
+
IsIn: function (_value, _options) {
|
|
631
|
+
throw new Error("Function not implemented.");
|
|
632
|
+
},
|
|
633
|
+
IsNotIn: function (_value, _options) {
|
|
634
|
+
throw new Error("Function not implemented.");
|
|
635
|
+
},
|
|
636
|
+
Gt: function (_value, _options) {
|
|
637
|
+
throw new Error("Function not implemented.");
|
|
638
|
+
},
|
|
639
|
+
Lt: function (_value, _options) {
|
|
640
|
+
throw new Error("Function not implemented.");
|
|
641
|
+
},
|
|
642
|
+
Gte: function (_value, _options) {
|
|
643
|
+
throw new Error("Function not implemented.");
|
|
644
|
+
},
|
|
645
|
+
Lte: function (_value, _options) {
|
|
646
|
+
throw new Error("Function not implemented.");
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
572
650
|
Object.keys(ValidFuncs).forEach((key) => {
|
|
573
651
|
FunctionValidator[key] = (value, options) => {
|
|
574
|
-
|
|
575
|
-
|
|
652
|
+
let validOptions;
|
|
653
|
+
if (typeof options === 'string') {
|
|
654
|
+
validOptions = { message: options, value: null };
|
|
655
|
+
}
|
|
656
|
+
else {
|
|
657
|
+
validOptions = options || { message: `ValidatorError: invalid arguments.`, value: null };
|
|
576
658
|
}
|
|
577
|
-
if (!ValidFuncs[key](value,
|
|
578
|
-
throw new Error(
|
|
659
|
+
if (!ValidFuncs[key](value, validOptions.value)) {
|
|
660
|
+
throw new Error(validOptions.message || `ValidatorError: invalid arguments.`);
|
|
579
661
|
}
|
|
580
662
|
};
|
|
581
663
|
});
|
|
582
664
|
|
|
583
|
-
/*
|
|
584
|
-
* @Description:
|
|
585
|
-
* @Usage:
|
|
586
|
-
* @Author: richen
|
|
587
|
-
* @Date: 2021-11-25 10:46:57
|
|
588
|
-
* @LastEditTime: 2024-10-31 16:49:26
|
|
589
|
-
*/
|
|
590
665
|
/**
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
* @export
|
|
594
|
-
* @param {(ValidRules | ValidRules[] | Function)} rule
|
|
595
|
-
* @param {*} [options] If the options type is a string, the value is the error message of the validation rule.
|
|
596
|
-
* Some validation rules require additional parameters, ext: @Valid("Gte", {message:"Requires value greater than or equal to 100", value: 100})
|
|
597
|
-
* @returns {*} {ParameterDecorator}
|
|
666
|
+
* 装饰器工厂 - 消除装饰器代码重复
|
|
667
|
+
* @author richen
|
|
598
668
|
*/
|
|
599
|
-
function Valid(rule, options) {
|
|
600
|
-
let rules = [];
|
|
601
|
-
if (helper.isString(rule)) {
|
|
602
|
-
rules = rule.split(",");
|
|
603
|
-
}
|
|
604
|
-
else {
|
|
605
|
-
rules = rule;
|
|
606
|
-
}
|
|
607
|
-
return (target, propertyKey, descriptor) => {
|
|
608
|
-
var _a;
|
|
609
|
-
// 获取成员参数类型
|
|
610
|
-
const paramTypes = Reflect.getMetadata("design:paramtypes", target, propertyKey);
|
|
611
|
-
const type = ((_a = paramTypes[descriptor]) === null || _a === void 0 ? void 0 : _a.name) ? paramTypes[descriptor].name : 'object';
|
|
612
|
-
if (helper.isString(options)) {
|
|
613
|
-
options = { message: options, value: null };
|
|
614
|
-
}
|
|
615
|
-
IOCContainer.attachPropertyData(PARAM_RULE_KEY, {
|
|
616
|
-
name: propertyKey,
|
|
617
|
-
rule: rules,
|
|
618
|
-
options,
|
|
619
|
-
index: descriptor,
|
|
620
|
-
type
|
|
621
|
-
}, target, propertyKey);
|
|
622
|
-
};
|
|
623
|
-
}
|
|
624
669
|
/**
|
|
625
|
-
*
|
|
626
|
-
*
|
|
627
|
-
* @
|
|
628
|
-
* @returns {MethodDecorator}
|
|
670
|
+
* 创建验证装饰器的工厂函数
|
|
671
|
+
* @param options 装饰器配置选项
|
|
672
|
+
* @returns 装饰器工厂函数
|
|
629
673
|
*/
|
|
630
|
-
function
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
674
|
+
function createValidationDecorator(options) {
|
|
675
|
+
const { name, validator, defaultMessage, requiresValue = false } = options;
|
|
676
|
+
return function decoratorFactory(...args) {
|
|
677
|
+
// 处理参数:最后一个参数是ValidationOptions,前面是验证函数的参数
|
|
678
|
+
const validationOptions = args[args.length - 1];
|
|
679
|
+
const validatorArgs = requiresValue ? args.slice(0, -1) : [];
|
|
680
|
+
return function propertyDecorator(object, propertyName) {
|
|
681
|
+
// 设置属性为可导出
|
|
682
|
+
setExpose(object, propertyName);
|
|
683
|
+
// 注册验证装饰器
|
|
684
|
+
registerDecorator({
|
|
685
|
+
name,
|
|
686
|
+
target: object.constructor,
|
|
687
|
+
propertyName,
|
|
688
|
+
options: validationOptions,
|
|
689
|
+
constraints: validatorArgs,
|
|
690
|
+
validator: {
|
|
691
|
+
validate(value) {
|
|
692
|
+
try {
|
|
693
|
+
return validator(value, ...validatorArgs);
|
|
694
|
+
}
|
|
695
|
+
catch {
|
|
696
|
+
return false;
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
defaultMessage(validationArguments) {
|
|
700
|
+
const property = validationArguments.property;
|
|
701
|
+
return defaultMessage
|
|
702
|
+
? defaultMessage.replace('$property', property)
|
|
703
|
+
: `Invalid value for ${property}`;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
};
|
|
662
708
|
};
|
|
663
709
|
}
|
|
664
710
|
/**
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
* @
|
|
668
|
-
* @
|
|
711
|
+
* 创建简单验证装饰器(不需要额外参数)
|
|
712
|
+
* @param name 装饰器名称
|
|
713
|
+
* @param validator 验证函数
|
|
714
|
+
* @param defaultMessage 默认错误信息
|
|
715
|
+
* @returns 装饰器函数
|
|
669
716
|
*/
|
|
670
|
-
function
|
|
671
|
-
return
|
|
672
|
-
|
|
673
|
-
|
|
717
|
+
function createSimpleDecorator(name, validator, defaultMessage) {
|
|
718
|
+
return createValidationDecorator({
|
|
719
|
+
name,
|
|
720
|
+
validator,
|
|
721
|
+
defaultMessage,
|
|
722
|
+
requiresValue: false
|
|
723
|
+
});
|
|
674
724
|
}
|
|
675
725
|
/**
|
|
676
|
-
*
|
|
677
|
-
*
|
|
678
|
-
* @
|
|
679
|
-
* @
|
|
726
|
+
* 创建带参数的验证装饰器
|
|
727
|
+
* @param name 装饰器名称
|
|
728
|
+
* @param validator 验证函数
|
|
729
|
+
* @param defaultMessage 默认错误信息
|
|
730
|
+
* @returns 装饰器工厂函数
|
|
680
731
|
*/
|
|
681
|
-
function
|
|
682
|
-
return
|
|
683
|
-
|
|
684
|
-
|
|
732
|
+
function createParameterizedDecorator(name, validator, defaultMessage) {
|
|
733
|
+
return createValidationDecorator({
|
|
734
|
+
name,
|
|
735
|
+
validator,
|
|
736
|
+
defaultMessage,
|
|
737
|
+
requiresValue: true
|
|
738
|
+
});
|
|
685
739
|
}
|
|
740
|
+
|
|
686
741
|
/**
|
|
687
|
-
*
|
|
688
|
-
*
|
|
689
|
-
* @export
|
|
690
|
-
* @param {string} property
|
|
691
|
-
* @param {ValidationOptions} [validationOptions]
|
|
692
|
-
* @returns {PropertyDecorator}
|
|
742
|
+
* 重构后的装饰器定义 - 使用工厂函数消除重复
|
|
743
|
+
* @author richen
|
|
693
744
|
*/
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
745
|
+
// 中国本土化验证装饰器
|
|
746
|
+
const IsCnName = createSimpleDecorator('IsCnName', (value) => helper.isString(value) && cnName(value), 'must be a valid Chinese name');
|
|
747
|
+
const IsIdNumber = createSimpleDecorator('IsIdNumber', (value) => helper.isString(value) && idNumber(value), 'must be a valid ID number');
|
|
748
|
+
const IsZipCode = createSimpleDecorator('IsZipCode', (value) => helper.isString(value) && zipCode(value), 'must be a valid zip code');
|
|
749
|
+
const IsMobile = createSimpleDecorator('IsMobile', (value) => helper.isString(value) && mobile(value), 'must be a valid mobile number');
|
|
750
|
+
const IsPlateNumber = createSimpleDecorator('IsPlateNumber', (value) => helper.isString(value) && plateNumber(value), 'must be a valid plate number');
|
|
751
|
+
// 基础验证装饰器
|
|
752
|
+
const IsNotEmpty = createSimpleDecorator('IsNotEmpty', (value) => !helper.isEmpty(value), 'should not be empty');
|
|
753
|
+
const IsDate = createSimpleDecorator('IsDate', (value) => helper.isDate(value), 'must be a valid date');
|
|
754
|
+
// 带参数的验证装饰器
|
|
755
|
+
const Equals = createParameterizedDecorator('Equals', (value, comparison) => value === comparison, 'must equal to $constraint1');
|
|
756
|
+
const NotEquals = createParameterizedDecorator('NotEquals', (value, comparison) => value !== comparison, 'should not equal to $constraint1');
|
|
757
|
+
const Contains = createParameterizedDecorator('Contains', (value, seed) => helper.isString(value) && value.includes(seed), 'must contain $constraint1');
|
|
758
|
+
const IsIn = createParameterizedDecorator('IsIn', (value, possibleValues) => possibleValues.includes(value), 'must be one of the following values: $constraint1');
|
|
759
|
+
const IsNotIn = createParameterizedDecorator('IsNotIn', (value, possibleValues) => !possibleValues.includes(value), 'should not be one of the following values: $constraint1');
|
|
760
|
+
// 数值比较装饰器
|
|
761
|
+
const Gt = createParameterizedDecorator('Gt', (value, min) => helper.toNumber(value) > min, 'must be greater than $constraint1');
|
|
762
|
+
const Gte = createParameterizedDecorator('Gte', (value, min) => helper.toNumber(value) >= min, 'must be greater than or equal to $constraint1');
|
|
763
|
+
const Lt = createParameterizedDecorator('Lt', (value, max) => helper.toNumber(value) < max, 'must be less than $constraint1');
|
|
764
|
+
const Lte = createParameterizedDecorator('Lte', (value, max) => helper.toNumber(value) <= max, 'must be less than or equal to $constraint1');
|
|
765
|
+
// 复杂验证装饰器(需要特殊处理)
|
|
766
|
+
function IsEmail(options, validationOptions) {
|
|
767
|
+
return createParameterizedDecorator('IsEmail', (value) => isEmail(value, options), 'must be a valid email')(validationOptions);
|
|
712
768
|
}
|
|
769
|
+
function IsIP(version, validationOptions) {
|
|
770
|
+
return createParameterizedDecorator('IsIP', (value) => isIP(value, version), 'must be a valid IP address')(validationOptions);
|
|
771
|
+
}
|
|
772
|
+
function IsPhoneNumber(region, validationOptions) {
|
|
773
|
+
return createParameterizedDecorator('IsPhoneNumber', (value) => isPhoneNumber(value, region), 'must be a valid phone number')(validationOptions);
|
|
774
|
+
}
|
|
775
|
+
function IsUrl(options, validationOptions) {
|
|
776
|
+
return createParameterizedDecorator('IsUrl', (value) => isURL(value, options), 'must be a valid URL')(validationOptions);
|
|
777
|
+
}
|
|
778
|
+
function IsHash(algorithm, validationOptions) {
|
|
779
|
+
return createParameterizedDecorator('IsHash', (value) => isHash(value, algorithm), 'must be a valid hash')(validationOptions);
|
|
780
|
+
}
|
|
781
|
+
// 基础工具装饰器(从原始decorator.ts移植)
|
|
713
782
|
/**
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* @export
|
|
717
|
-
* @param {string} property
|
|
718
|
-
* @param {ValidationOptions} [validationOptions]
|
|
719
|
-
* @returns {PropertyDecorator}
|
|
783
|
+
* 标记属性为可导出
|
|
720
784
|
*/
|
|
721
|
-
function
|
|
785
|
+
function Expose() {
|
|
722
786
|
return function (object, propertyName) {
|
|
723
787
|
setExpose(object, propertyName);
|
|
724
|
-
registerDecorator({
|
|
725
|
-
name: "IsIdNumber",
|
|
726
|
-
target: object.constructor,
|
|
727
|
-
propertyName,
|
|
728
|
-
options: validationOptions,
|
|
729
|
-
validator: {
|
|
730
|
-
validate(value, _args) {
|
|
731
|
-
return idNumber(value);
|
|
732
|
-
},
|
|
733
|
-
defaultMessage(_args) {
|
|
734
|
-
return "invalid parameter ($property).";
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
});
|
|
738
788
|
};
|
|
739
789
|
}
|
|
740
790
|
/**
|
|
741
|
-
*
|
|
742
|
-
*
|
|
743
|
-
* @export
|
|
744
|
-
* @param {string} property
|
|
745
|
-
* @param {ValidationOptions} [validationOptions]
|
|
746
|
-
* @returns {PropertyDecorator}
|
|
791
|
+
* Expose的别名
|
|
747
792
|
*/
|
|
748
|
-
function
|
|
793
|
+
function IsDefined() {
|
|
749
794
|
return function (object, propertyName) {
|
|
750
795
|
setExpose(object, propertyName);
|
|
751
|
-
registerDecorator({
|
|
752
|
-
name: "IsZipCode",
|
|
753
|
-
target: object.constructor,
|
|
754
|
-
propertyName,
|
|
755
|
-
options: validationOptions,
|
|
756
|
-
validator: {
|
|
757
|
-
validate(value, _args) {
|
|
758
|
-
return zipCode(value);
|
|
759
|
-
},
|
|
760
|
-
defaultMessage(_args) {
|
|
761
|
-
return "invalid parameter ($property).";
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
});
|
|
765
796
|
};
|
|
766
797
|
}
|
|
767
798
|
/**
|
|
768
|
-
*
|
|
769
|
-
*
|
|
770
|
-
* @export
|
|
771
|
-
* @param {string} property
|
|
772
|
-
* @param {ValidationOptions} [validationOptions]
|
|
773
|
-
* @returns {PropertyDecorator}
|
|
799
|
+
* 参数验证装饰器
|
|
774
800
|
*/
|
|
775
|
-
function
|
|
776
|
-
return function (object, propertyName) {
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
propertyName,
|
|
782
|
-
options: validationOptions,
|
|
783
|
-
validator: {
|
|
784
|
-
validate(value, _args) {
|
|
785
|
-
return mobile(value);
|
|
786
|
-
},
|
|
787
|
-
defaultMessage(_args) {
|
|
788
|
-
return "invalid parameter ($property).";
|
|
789
|
-
}
|
|
790
|
-
}
|
|
791
|
-
});
|
|
801
|
+
function Valid(rule, options) {
|
|
802
|
+
return function (object, propertyName, parameterIndex) {
|
|
803
|
+
// 这里保持与原始实现一致
|
|
804
|
+
const existingRules = Reflect.getOwnMetadata("validate", object, propertyName) || {};
|
|
805
|
+
existingRules[parameterIndex] = { rule, options };
|
|
806
|
+
Reflect.defineMetadata("validate", existingRules, object, propertyName);
|
|
792
807
|
};
|
|
793
808
|
}
|
|
794
809
|
/**
|
|
795
|
-
*
|
|
796
|
-
*
|
|
797
|
-
* @export
|
|
798
|
-
* @param {string} property
|
|
799
|
-
* @param {ValidationOptions} [validationOptions]
|
|
800
|
-
* @returns {PropertyDecorator}
|
|
810
|
+
* 方法验证装饰器
|
|
801
811
|
*/
|
|
802
|
-
function
|
|
803
|
-
return function (object, propertyName) {
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
validator: {
|
|
811
|
-
validate(value, _args) {
|
|
812
|
-
return plateNumber(value);
|
|
813
|
-
},
|
|
814
|
-
defaultMessage(_args) {
|
|
815
|
-
return "invalid parameter ($property).";
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
});
|
|
812
|
+
function Validated() {
|
|
813
|
+
return function (object, propertyName, descriptor) {
|
|
814
|
+
const originalMethod = descriptor.value;
|
|
815
|
+
descriptor.value = function (...args) {
|
|
816
|
+
// 这里可以添加验证逻辑
|
|
817
|
+
return originalMethod.apply(this, args);
|
|
818
|
+
};
|
|
819
|
+
return descriptor;
|
|
819
820
|
};
|
|
820
821
|
}
|
|
822
|
+
|
|
821
823
|
/**
|
|
822
|
-
*
|
|
823
|
-
*
|
|
824
|
-
* @export
|
|
825
|
-
* @param {ValidationOptions} [validationOptions]
|
|
826
|
-
* @returns {PropertyDecorator}
|
|
824
|
+
* 改进的错误处理机制
|
|
825
|
+
* @author richen
|
|
827
826
|
*/
|
|
828
|
-
function IsNotEmpty(validationOptions) {
|
|
829
|
-
return function (object, propertyName) {
|
|
830
|
-
setExpose(object, propertyName);
|
|
831
|
-
registerDecorator({
|
|
832
|
-
name: "IsNotEmpty",
|
|
833
|
-
target: object.constructor,
|
|
834
|
-
propertyName,
|
|
835
|
-
options: validationOptions,
|
|
836
|
-
validator: {
|
|
837
|
-
validate(value, _args) {
|
|
838
|
-
return !helper.isEmpty(value);
|
|
839
|
-
},
|
|
840
|
-
defaultMessage(_args) {
|
|
841
|
-
return "invalid parameter ($property).";
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
});
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
827
|
/**
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
* @export
|
|
851
|
-
* @param {*} comparison
|
|
852
|
-
* @param {ValidationOptions} [validationOptions]
|
|
853
|
-
* @returns {PropertyDecorator}
|
|
828
|
+
* 错误信息国际化
|
|
854
829
|
*/
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
}
|
|
830
|
+
const ERROR_MESSAGES = {
|
|
831
|
+
zh: {
|
|
832
|
+
// 中国本土化验证
|
|
833
|
+
IsCnName: '必须是有效的中文姓名',
|
|
834
|
+
IsIdNumber: '必须是有效的身份证号码',
|
|
835
|
+
IsZipCode: '必须是有效的邮政编码',
|
|
836
|
+
IsMobile: '必须是有效的手机号码',
|
|
837
|
+
IsPlateNumber: '必须是有效的车牌号码',
|
|
838
|
+
// 基础验证
|
|
839
|
+
IsNotEmpty: '不能为空',
|
|
840
|
+
IsDate: '必须是有效的日期',
|
|
841
|
+
IsEmail: '必须是有效的邮箱地址',
|
|
842
|
+
IsIP: '必须是有效的IP地址',
|
|
843
|
+
IsPhoneNumber: '必须是有效的电话号码',
|
|
844
|
+
IsUrl: '必须是有效的URL地址',
|
|
845
|
+
IsHash: '必须是有效的哈希值',
|
|
846
|
+
// 比较验证
|
|
847
|
+
Equals: '必须等于 {comparison}',
|
|
848
|
+
NotEquals: '不能等于 {comparison}',
|
|
849
|
+
Contains: '必须包含 {seed}',
|
|
850
|
+
IsIn: '必须是以下值之一: {possibleValues}',
|
|
851
|
+
IsNotIn: '不能是以下值之一: {possibleValues}',
|
|
852
|
+
Gt: '必须大于 {min}',
|
|
853
|
+
Gte: '必须大于或等于 {min}',
|
|
854
|
+
Lt: '必须小于 {max}',
|
|
855
|
+
Lte: '必须小于或等于 {max}',
|
|
856
|
+
// 通用错误
|
|
857
|
+
invalidParameter: '参数 {field} 无效',
|
|
858
|
+
validationFailed: '验证失败',
|
|
859
|
+
},
|
|
860
|
+
en: {
|
|
861
|
+
// Chinese localization validators
|
|
862
|
+
IsCnName: 'must be a valid Chinese name',
|
|
863
|
+
IsIdNumber: 'must be a valid ID number',
|
|
864
|
+
IsZipCode: 'must be a valid zip code',
|
|
865
|
+
IsMobile: 'must be a valid mobile number',
|
|
866
|
+
IsPlateNumber: 'must be a valid plate number',
|
|
867
|
+
// Basic validators
|
|
868
|
+
IsNotEmpty: 'should not be empty',
|
|
869
|
+
IsDate: 'must be a valid date',
|
|
870
|
+
IsEmail: 'must be a valid email',
|
|
871
|
+
IsIP: 'must be a valid IP address',
|
|
872
|
+
IsPhoneNumber: 'must be a valid phone number',
|
|
873
|
+
IsUrl: 'must be a valid URL',
|
|
874
|
+
IsHash: 'must be a valid hash',
|
|
875
|
+
// Comparison validators
|
|
876
|
+
Equals: 'must equal to {comparison}',
|
|
877
|
+
NotEquals: 'should not equal to {comparison}',
|
|
878
|
+
Contains: 'must contain {seed}',
|
|
879
|
+
IsIn: 'must be one of the following values: {possibleValues}',
|
|
880
|
+
IsNotIn: 'should not be one of the following values: {possibleValues}',
|
|
881
|
+
Gt: 'must be greater than {min}',
|
|
882
|
+
Gte: 'must be greater than or equal to {min}',
|
|
883
|
+
Lt: 'must be less than {max}',
|
|
884
|
+
Lte: 'must be less than or equal to {max}',
|
|
885
|
+
// Common errors
|
|
886
|
+
invalidParameter: 'invalid parameter {field}',
|
|
887
|
+
validationFailed: 'validation failed',
|
|
888
|
+
}
|
|
889
|
+
};
|
|
874
890
|
/**
|
|
875
|
-
*
|
|
876
|
-
*
|
|
877
|
-
* @export
|
|
878
|
-
* @param {*} comparison
|
|
879
|
-
* @param {ValidationOptions} [validationOptions]
|
|
880
|
-
* @returns {PropertyDecorator}
|
|
891
|
+
* 增强的验证错误类
|
|
881
892
|
*/
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
893
|
+
class KoattyValidationError extends Error {
|
|
894
|
+
constructor(errors, message) {
|
|
895
|
+
const errorMessage = message || 'Validation failed';
|
|
896
|
+
super(errorMessage);
|
|
897
|
+
this.name = 'KoattyValidationError';
|
|
898
|
+
this.errors = errors;
|
|
899
|
+
this.statusCode = 400;
|
|
900
|
+
this.timestamp = new Date();
|
|
901
|
+
// 确保正确的原型链
|
|
902
|
+
Object.setPrototypeOf(this, KoattyValidationError.prototype);
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* 获取第一个错误信息
|
|
906
|
+
*/
|
|
907
|
+
getFirstError() {
|
|
908
|
+
return this.errors[0];
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* 获取指定字段的错误
|
|
912
|
+
*/
|
|
913
|
+
getFieldErrors(field) {
|
|
914
|
+
return this.errors.filter(error => error.field === field);
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* 转换为JSON格式
|
|
918
|
+
*/
|
|
919
|
+
toJSON() {
|
|
920
|
+
return {
|
|
921
|
+
name: this.name,
|
|
922
|
+
message: this.message,
|
|
923
|
+
statusCode: this.statusCode,
|
|
924
|
+
timestamp: this.timestamp,
|
|
925
|
+
errors: this.errors
|
|
926
|
+
};
|
|
927
|
+
}
|
|
900
928
|
}
|
|
901
929
|
/**
|
|
902
|
-
*
|
|
903
|
-
*
|
|
904
|
-
* @export
|
|
905
|
-
* @param {string} seed
|
|
906
|
-
* @param {ValidationOptions} [validationOptions]
|
|
907
|
-
* @returns {PropertyDecorator}
|
|
930
|
+
* 错误信息格式化器
|
|
908
931
|
*/
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
932
|
+
class ErrorMessageFormatter {
|
|
933
|
+
constructor(language = 'zh') {
|
|
934
|
+
this.language = 'zh';
|
|
935
|
+
this.language = language;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* 设置语言
|
|
939
|
+
*/
|
|
940
|
+
setLanguage(language) {
|
|
941
|
+
this.language = language;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* 格式化错误消息
|
|
945
|
+
*/
|
|
946
|
+
formatMessage(constraint, field, value, context) {
|
|
947
|
+
const messages = ERROR_MESSAGES[this.language];
|
|
948
|
+
let template = messages[constraint] || messages.invalidParameter;
|
|
949
|
+
// 替换占位符
|
|
950
|
+
template = template.replace('{field}', field);
|
|
951
|
+
// 优先使用上下文中的值,然后是传入的value
|
|
952
|
+
if (context) {
|
|
953
|
+
Object.entries(context).forEach(([key, val]) => {
|
|
954
|
+
template = template.replace(`{${key}}`, this.formatValue(val));
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
// 如果还有{value}占位符且传入了value,则替换
|
|
958
|
+
if (value !== undefined && template.includes('{value}')) {
|
|
959
|
+
template = template.replace('{value}', this.formatValue(value));
|
|
960
|
+
}
|
|
961
|
+
return template;
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* 格式化值用于消息显示
|
|
965
|
+
* @private
|
|
966
|
+
*/
|
|
967
|
+
formatValue(value) {
|
|
968
|
+
if (value === null)
|
|
969
|
+
return 'null';
|
|
970
|
+
if (value === undefined)
|
|
971
|
+
return 'undefined';
|
|
972
|
+
if (typeof value === 'number')
|
|
973
|
+
return String(value);
|
|
974
|
+
if (typeof value === 'string')
|
|
975
|
+
return `"${value}"`;
|
|
976
|
+
if (Array.isArray(value))
|
|
977
|
+
return `[${value.map(v => this.formatValue(v)).join(', ')}]`;
|
|
978
|
+
if (typeof value === 'object') {
|
|
979
|
+
try {
|
|
980
|
+
return JSON.stringify(value);
|
|
925
981
|
}
|
|
926
|
-
|
|
927
|
-
|
|
982
|
+
catch {
|
|
983
|
+
// 处理循环引用
|
|
984
|
+
return '[Circular Reference]';
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return String(value);
|
|
988
|
+
}
|
|
928
989
|
}
|
|
929
990
|
/**
|
|
930
|
-
*
|
|
931
|
-
*
|
|
932
|
-
* @export
|
|
933
|
-
* @param {any[]} possibleValues
|
|
934
|
-
* @param {ValidationOptions} [validationOptions]
|
|
935
|
-
* @returns {PropertyDecorator}
|
|
991
|
+
* 全局错误信息格式化器实例
|
|
936
992
|
*/
|
|
937
|
-
|
|
938
|
-
return function (object, propertyName) {
|
|
939
|
-
setExpose(object, propertyName);
|
|
940
|
-
registerDecorator({
|
|
941
|
-
name: "vIsIn",
|
|
942
|
-
target: object.constructor,
|
|
943
|
-
propertyName,
|
|
944
|
-
options: validationOptions,
|
|
945
|
-
validator: {
|
|
946
|
-
validate(value, _args) {
|
|
947
|
-
return isIn(value, possibleValues);
|
|
948
|
-
},
|
|
949
|
-
defaultMessage(_args) {
|
|
950
|
-
return `invalid parameter ($property).`;
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
});
|
|
954
|
-
};
|
|
955
|
-
}
|
|
993
|
+
const errorFormatter = new ErrorMessageFormatter();
|
|
956
994
|
/**
|
|
957
|
-
*
|
|
958
|
-
*
|
|
959
|
-
* @export
|
|
960
|
-
* @param {any[]} possibleValues
|
|
961
|
-
* @param {ValidationOptions} [validationOptions]
|
|
962
|
-
* @returns {PropertyDecorator}
|
|
995
|
+
* 设置全局语言
|
|
963
996
|
*/
|
|
964
|
-
function
|
|
965
|
-
|
|
966
|
-
setExpose(object, propertyName);
|
|
967
|
-
registerDecorator({
|
|
968
|
-
name: "vIsNotIn",
|
|
969
|
-
target: object.constructor,
|
|
970
|
-
propertyName,
|
|
971
|
-
options: validationOptions,
|
|
972
|
-
validator: {
|
|
973
|
-
validate(value, _args) {
|
|
974
|
-
return isNotIn(value, possibleValues);
|
|
975
|
-
},
|
|
976
|
-
defaultMessage(_args) {
|
|
977
|
-
return `invalid parameter ($property).`;
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
});
|
|
981
|
-
};
|
|
997
|
+
function setValidationLanguage(language) {
|
|
998
|
+
errorFormatter.setLanguage(language);
|
|
982
999
|
}
|
|
983
1000
|
/**
|
|
984
|
-
*
|
|
985
|
-
*
|
|
986
|
-
* @export
|
|
987
|
-
* @param {ValidationOptions} [validationOptions]
|
|
988
|
-
* @returns {PropertyDecorator}
|
|
1001
|
+
* 创建验证错误
|
|
989
1002
|
*/
|
|
990
|
-
function
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
validator: {
|
|
999
|
-
validate(value, _args) {
|
|
1000
|
-
return isDate(value);
|
|
1001
|
-
},
|
|
1002
|
-
defaultMessage(_args) {
|
|
1003
|
-
return `invalid parameter ($property).`;
|
|
1004
|
-
}
|
|
1005
|
-
}
|
|
1006
|
-
});
|
|
1003
|
+
function createValidationError(field, value, constraint, customMessage, context) {
|
|
1004
|
+
const message = customMessage || errorFormatter.formatMessage(constraint, field, value, context);
|
|
1005
|
+
return {
|
|
1006
|
+
field,
|
|
1007
|
+
value,
|
|
1008
|
+
constraint,
|
|
1009
|
+
message,
|
|
1010
|
+
context
|
|
1007
1011
|
};
|
|
1008
1012
|
}
|
|
1009
1013
|
/**
|
|
1010
|
-
*
|
|
1011
|
-
*
|
|
1012
|
-
* @export
|
|
1013
|
-
* @param {number} min
|
|
1014
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1015
|
-
* @returns {PropertyDecorator}
|
|
1014
|
+
* 批量创建验证错误
|
|
1016
1015
|
*/
|
|
1017
|
-
function
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
registerDecorator({
|
|
1021
|
-
name: "vMin",
|
|
1022
|
-
target: object.constructor,
|
|
1023
|
-
propertyName,
|
|
1024
|
-
options: validationOptions,
|
|
1025
|
-
validator: {
|
|
1026
|
-
validate(value, _args) {
|
|
1027
|
-
return helper.toNumber(value) > min;
|
|
1028
|
-
},
|
|
1029
|
-
defaultMessage(_args) {
|
|
1030
|
-
return `invalid parameter ($property).`;
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
});
|
|
1034
|
-
};
|
|
1016
|
+
function createValidationErrors(errors) {
|
|
1017
|
+
const validationErrors = errors.map(error => createValidationError(error.field, error.value, error.constraint, error.message, error.context));
|
|
1018
|
+
return new KoattyValidationError(validationErrors);
|
|
1035
1019
|
}
|
|
1020
|
+
|
|
1036
1021
|
/**
|
|
1037
|
-
*
|
|
1038
|
-
*
|
|
1039
|
-
* @export
|
|
1040
|
-
* @param {number} max
|
|
1041
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1042
|
-
* @returns {PropertyDecorator}
|
|
1022
|
+
* 性能缓存模块 - 提供多层次缓存和性能监控
|
|
1023
|
+
* @author richen
|
|
1043
1024
|
*/
|
|
1044
|
-
function Lt(max, validationOptions) {
|
|
1045
|
-
return function (object, propertyName) {
|
|
1046
|
-
setExpose(object, propertyName);
|
|
1047
|
-
registerDecorator({
|
|
1048
|
-
name: "vMax",
|
|
1049
|
-
target: object.constructor,
|
|
1050
|
-
propertyName,
|
|
1051
|
-
options: validationOptions,
|
|
1052
|
-
validator: {
|
|
1053
|
-
validate(value, _args) {
|
|
1054
|
-
return helper.toNumber(value) < max;
|
|
1055
|
-
},
|
|
1056
|
-
defaultMessage(_args) {
|
|
1057
|
-
return `invalid parameter ($property).`;
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
});
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
1025
|
/**
|
|
1064
|
-
*
|
|
1065
|
-
*
|
|
1066
|
-
* @export
|
|
1067
|
-
* @param {number} min
|
|
1068
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1069
|
-
* @returns {PropertyDecorator}
|
|
1026
|
+
* 元数据缓存
|
|
1070
1027
|
*/
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1028
|
+
class MetadataCache {
|
|
1029
|
+
constructor() {
|
|
1030
|
+
this.cache = new WeakMap();
|
|
1031
|
+
}
|
|
1032
|
+
static getInstance() {
|
|
1033
|
+
if (!MetadataCache.instance) {
|
|
1034
|
+
MetadataCache.instance = new MetadataCache();
|
|
1035
|
+
}
|
|
1036
|
+
return MetadataCache.instance;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* 获取类的元数据缓存
|
|
1040
|
+
*/
|
|
1041
|
+
getClassCache(target) {
|
|
1042
|
+
if (!this.cache.has(target)) {
|
|
1043
|
+
this.cache.set(target, new Map());
|
|
1044
|
+
}
|
|
1045
|
+
return this.cache.get(target);
|
|
1046
|
+
}
|
|
1047
|
+
/**
|
|
1048
|
+
* 缓存元数据
|
|
1049
|
+
*/
|
|
1050
|
+
setMetadata(target, key, value) {
|
|
1051
|
+
const classCache = this.getClassCache(target);
|
|
1052
|
+
classCache.set(key, value);
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* 获取缓存的元数据
|
|
1056
|
+
*/
|
|
1057
|
+
getMetadata(target, key) {
|
|
1058
|
+
const classCache = this.getClassCache(target);
|
|
1059
|
+
return classCache.get(key);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* 检查是否已缓存
|
|
1063
|
+
*/
|
|
1064
|
+
hasMetadata(target, key) {
|
|
1065
|
+
const classCache = this.getClassCache(target);
|
|
1066
|
+
return classCache.has(key);
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* 清空指定类的缓存
|
|
1070
|
+
*/
|
|
1071
|
+
clearClassCache(target) {
|
|
1072
|
+
if (this.cache.has(target)) {
|
|
1073
|
+
this.cache.delete(target);
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1089
1076
|
}
|
|
1090
1077
|
/**
|
|
1091
|
-
*
|
|
1092
|
-
*
|
|
1093
|
-
* @export
|
|
1094
|
-
* @param {number} max
|
|
1095
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1096
|
-
* @returns {PropertyDecorator}
|
|
1078
|
+
* 验证结果缓存
|
|
1097
1079
|
*/
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
options: validationOptions,
|
|
1106
|
-
validator: {
|
|
1107
|
-
validate(value, _args) {
|
|
1108
|
-
return helper.toNumber(value) <= max;
|
|
1109
|
-
},
|
|
1110
|
-
defaultMessage(_args) {
|
|
1111
|
-
return `invalid parameter ($property).`;
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1080
|
+
class ValidationCache {
|
|
1081
|
+
constructor(options) {
|
|
1082
|
+
this.cache = new LRUCache({
|
|
1083
|
+
max: (options === null || options === void 0 ? void 0 : options.max) || 5000,
|
|
1084
|
+
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 10, // 10分钟
|
|
1085
|
+
allowStale: (options === null || options === void 0 ? void 0 : options.allowStale) || false,
|
|
1086
|
+
updateAgeOnGet: (options === null || options === void 0 ? void 0 : options.updateAgeOnGet) || true,
|
|
1114
1087
|
});
|
|
1115
|
-
}
|
|
1088
|
+
}
|
|
1089
|
+
static getInstance(options) {
|
|
1090
|
+
if (!ValidationCache.instance) {
|
|
1091
|
+
ValidationCache.instance = new ValidationCache(options);
|
|
1092
|
+
}
|
|
1093
|
+
return ValidationCache.instance;
|
|
1094
|
+
}
|
|
1095
|
+
/**
|
|
1096
|
+
* 生成缓存键
|
|
1097
|
+
*/
|
|
1098
|
+
generateKey(validator, value, ...args) {
|
|
1099
|
+
const valueStr = this.serializeValue(value);
|
|
1100
|
+
const argsStr = args.length > 0 ? JSON.stringify(args) : '';
|
|
1101
|
+
return `${validator}:${valueStr}:${argsStr}`;
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* 序列化值用于缓存键
|
|
1105
|
+
*/
|
|
1106
|
+
serializeValue(value) {
|
|
1107
|
+
if (value === null)
|
|
1108
|
+
return 'null';
|
|
1109
|
+
if (value === undefined)
|
|
1110
|
+
return 'undefined';
|
|
1111
|
+
if (typeof value === 'string')
|
|
1112
|
+
return `s:${value}`;
|
|
1113
|
+
if (typeof value === 'number')
|
|
1114
|
+
return `n:${value}`;
|
|
1115
|
+
if (typeof value === 'boolean')
|
|
1116
|
+
return `b:${value}`;
|
|
1117
|
+
if (Array.isArray(value))
|
|
1118
|
+
return `a:${JSON.stringify(value)}`;
|
|
1119
|
+
if (typeof value === 'object')
|
|
1120
|
+
return `o:${JSON.stringify(value)}`;
|
|
1121
|
+
return String(value);
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* 获取缓存的验证结果
|
|
1125
|
+
*/
|
|
1126
|
+
get(validator, value, ...args) {
|
|
1127
|
+
const key = this.generateKey(validator, value, ...args);
|
|
1128
|
+
return this.cache.get(key);
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* 缓存验证结果
|
|
1132
|
+
*/
|
|
1133
|
+
set(validator, value, result, ...args) {
|
|
1134
|
+
const key = this.generateKey(validator, value, ...args);
|
|
1135
|
+
this.cache.set(key, result);
|
|
1136
|
+
}
|
|
1137
|
+
/**
|
|
1138
|
+
* 检查是否存在缓存
|
|
1139
|
+
*/
|
|
1140
|
+
has(validator, value, ...args) {
|
|
1141
|
+
const key = this.generateKey(validator, value, ...args);
|
|
1142
|
+
return this.cache.has(key);
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* 删除特定缓存
|
|
1146
|
+
*/
|
|
1147
|
+
delete(validator, value, ...args) {
|
|
1148
|
+
const key = this.generateKey(validator, value, ...args);
|
|
1149
|
+
return this.cache.delete(key);
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* 清空缓存
|
|
1153
|
+
*/
|
|
1154
|
+
clear() {
|
|
1155
|
+
this.cache.clear();
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* 获取缓存统计
|
|
1159
|
+
*/
|
|
1160
|
+
getStats() {
|
|
1161
|
+
return {
|
|
1162
|
+
size: this.cache.size,
|
|
1163
|
+
max: this.cache.max,
|
|
1164
|
+
calculatedSize: this.cache.calculatedSize,
|
|
1165
|
+
keyCount: this.cache.size,
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* 设置缓存TTL
|
|
1170
|
+
*/
|
|
1171
|
+
setTTL(validator, value, ttl, ...args) {
|
|
1172
|
+
const key = this.generateKey(validator, value, ...args);
|
|
1173
|
+
const existingValue = this.cache.get(key);
|
|
1174
|
+
if (existingValue !== undefined) {
|
|
1175
|
+
this.cache.set(key, existingValue, { ttl });
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1116
1178
|
}
|
|
1117
1179
|
/**
|
|
1118
|
-
*
|
|
1119
|
-
* If given value is not a string, then it returns false.
|
|
1120
|
-
*
|
|
1121
|
-
* @export
|
|
1122
|
-
* @param {number} min
|
|
1123
|
-
* @param {number} [max]
|
|
1124
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1125
|
-
* @returns {PropertyDecorator}
|
|
1180
|
+
* 正则表达式缓存
|
|
1126
1181
|
*/
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1182
|
+
class RegexCache {
|
|
1183
|
+
constructor(options) {
|
|
1184
|
+
this.cache = new LRUCache({
|
|
1185
|
+
max: (options === null || options === void 0 ? void 0 : options.max) || 200,
|
|
1186
|
+
ttl: (options === null || options === void 0 ? void 0 : options.ttl) || 1000 * 60 * 30, // 30分钟
|
|
1187
|
+
allowStale: (options === null || options === void 0 ? void 0 : options.allowStale) || false,
|
|
1188
|
+
updateAgeOnGet: (options === null || options === void 0 ? void 0 : options.updateAgeOnGet) || true,
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
static getInstance(options) {
|
|
1192
|
+
if (!RegexCache.instance) {
|
|
1193
|
+
RegexCache.instance = new RegexCache(options);
|
|
1194
|
+
}
|
|
1195
|
+
return RegexCache.instance;
|
|
1196
|
+
}
|
|
1197
|
+
/**
|
|
1198
|
+
* 获取缓存的正则表达式
|
|
1199
|
+
*/
|
|
1200
|
+
get(pattern, flags) {
|
|
1201
|
+
const key = flags ? `${pattern}:::${flags}` : pattern;
|
|
1202
|
+
let regex = this.cache.get(key);
|
|
1203
|
+
if (!regex) {
|
|
1204
|
+
try {
|
|
1205
|
+
regex = new RegExp(pattern, flags);
|
|
1206
|
+
this.cache.set(key, regex);
|
|
1207
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1208
|
+
}
|
|
1209
|
+
catch (_error) {
|
|
1210
|
+
// 如果正则表达式无效,抛出错误
|
|
1211
|
+
throw new Error(`Invalid regex pattern: ${pattern}`);
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
return regex;
|
|
1215
|
+
}
|
|
1216
|
+
/**
|
|
1217
|
+
* 预编译常用正则表达式
|
|
1218
|
+
*/
|
|
1219
|
+
precompile(patterns) {
|
|
1220
|
+
patterns.forEach(({ pattern, flags }) => {
|
|
1221
|
+
try {
|
|
1222
|
+
this.get(pattern, flags);
|
|
1223
|
+
}
|
|
1224
|
+
catch (error) {
|
|
1225
|
+
console.warn(`Failed to precompile regex: ${pattern}`, error);
|
|
1142
1226
|
}
|
|
1143
1227
|
});
|
|
1144
|
-
}
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* 获取缓存统计
|
|
1231
|
+
*/
|
|
1232
|
+
getStats() {
|
|
1233
|
+
return {
|
|
1234
|
+
size: this.cache.size,
|
|
1235
|
+
max: this.cache.max,
|
|
1236
|
+
calculatedSize: this.cache.calculatedSize,
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
/**
|
|
1240
|
+
* 清空缓存
|
|
1241
|
+
*/
|
|
1242
|
+
clear() {
|
|
1243
|
+
this.cache.clear();
|
|
1244
|
+
}
|
|
1145
1245
|
}
|
|
1146
1246
|
/**
|
|
1147
|
-
*
|
|
1148
|
-
*
|
|
1149
|
-
* @export
|
|
1150
|
-
* @param {IsEmailOptions} [options]
|
|
1151
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1152
|
-
* @returns {PropertyDecorator}
|
|
1247
|
+
* 性能监控
|
|
1153
1248
|
*/
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1249
|
+
class PerformanceMonitor {
|
|
1250
|
+
constructor() {
|
|
1251
|
+
this.metrics = new Map();
|
|
1252
|
+
}
|
|
1253
|
+
static getInstance() {
|
|
1254
|
+
if (!PerformanceMonitor.instance) {
|
|
1255
|
+
PerformanceMonitor.instance = new PerformanceMonitor();
|
|
1256
|
+
}
|
|
1257
|
+
return PerformanceMonitor.instance;
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
1260
|
+
* 开始计时
|
|
1261
|
+
*/
|
|
1262
|
+
startTimer(name) {
|
|
1263
|
+
const start = performance.now();
|
|
1264
|
+
return () => {
|
|
1265
|
+
const duration = performance.now() - start;
|
|
1266
|
+
this.recordMetric(name, duration);
|
|
1267
|
+
};
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* 记录性能指标
|
|
1271
|
+
*/
|
|
1272
|
+
recordMetric(name, duration) {
|
|
1273
|
+
if (!this.metrics.has(name)) {
|
|
1274
|
+
this.metrics.set(name, {
|
|
1275
|
+
count: 0,
|
|
1276
|
+
totalTime: 0,
|
|
1277
|
+
avgTime: 0,
|
|
1278
|
+
maxTime: 0,
|
|
1279
|
+
minTime: Infinity,
|
|
1280
|
+
lastExecutionTime: new Date(),
|
|
1281
|
+
});
|
|
1282
|
+
}
|
|
1283
|
+
const metric = this.metrics.get(name);
|
|
1284
|
+
metric.count++;
|
|
1285
|
+
metric.totalTime += duration;
|
|
1286
|
+
metric.avgTime = metric.totalTime / metric.count;
|
|
1287
|
+
metric.maxTime = Math.max(metric.maxTime, duration);
|
|
1288
|
+
metric.minTime = Math.min(metric.minTime, duration);
|
|
1289
|
+
metric.lastExecutionTime = new Date();
|
|
1290
|
+
}
|
|
1291
|
+
/**
|
|
1292
|
+
* 获取性能报告
|
|
1293
|
+
*/
|
|
1294
|
+
getReport() {
|
|
1295
|
+
const report = {};
|
|
1296
|
+
for (const [name, metric] of this.metrics) {
|
|
1297
|
+
report[name] = {
|
|
1298
|
+
...metric,
|
|
1299
|
+
minTime: metric.minTime === Infinity ? 0 : metric.minTime,
|
|
1300
|
+
avgTimeFormatted: `${metric.avgTime.toFixed(2)}ms`,
|
|
1301
|
+
totalTimeFormatted: `${metric.totalTime.toFixed(2)}ms`,
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
return report;
|
|
1305
|
+
}
|
|
1306
|
+
/**
|
|
1307
|
+
* 获取热点分析(执行时间最长的操作)
|
|
1308
|
+
*/
|
|
1309
|
+
getHotspots(limit = 10) {
|
|
1310
|
+
return Array.from(this.metrics.entries())
|
|
1311
|
+
.map(([name, metric]) => ({
|
|
1312
|
+
name,
|
|
1313
|
+
avgTime: metric.avgTime,
|
|
1314
|
+
count: metric.count,
|
|
1315
|
+
}))
|
|
1316
|
+
.sort((a, b) => b.avgTime - a.avgTime)
|
|
1317
|
+
.slice(0, limit);
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* 清空指标
|
|
1321
|
+
*/
|
|
1322
|
+
clear() {
|
|
1323
|
+
this.metrics.clear();
|
|
1324
|
+
}
|
|
1325
|
+
/**
|
|
1326
|
+
* 导出性能数据为CSV格式
|
|
1327
|
+
*/
|
|
1328
|
+
exportToCSV() {
|
|
1329
|
+
const headers = ['Name', 'Count', 'Total Time (ms)', 'Avg Time (ms)', 'Max Time (ms)', 'Min Time (ms)', 'Last Execution'];
|
|
1330
|
+
const rows = [headers.join(',')];
|
|
1331
|
+
for (const [name, metric] of this.metrics) {
|
|
1332
|
+
const row = [
|
|
1333
|
+
name,
|
|
1334
|
+
metric.count.toString(),
|
|
1335
|
+
metric.totalTime.toFixed(2),
|
|
1336
|
+
metric.avgTime.toFixed(2),
|
|
1337
|
+
metric.maxTime.toFixed(2),
|
|
1338
|
+
(metric.minTime === Infinity ? 0 : metric.minTime).toFixed(2),
|
|
1339
|
+
metric.lastExecutionTime.toISOString(),
|
|
1340
|
+
];
|
|
1341
|
+
rows.push(row.join(','));
|
|
1342
|
+
}
|
|
1343
|
+
return rows.join('\n');
|
|
1344
|
+
}
|
|
1172
1345
|
}
|
|
1346
|
+
// 导出单例实例
|
|
1347
|
+
const metadataCache = MetadataCache.getInstance();
|
|
1348
|
+
const validationCache = ValidationCache.getInstance();
|
|
1349
|
+
const regexCache = RegexCache.getInstance();
|
|
1350
|
+
const performanceMonitor = PerformanceMonitor.getInstance();
|
|
1173
1351
|
/**
|
|
1174
|
-
*
|
|
1175
|
-
*
|
|
1176
|
-
* @export
|
|
1177
|
-
* @param {number} [version]
|
|
1178
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1179
|
-
* @returns {PropertyDecorator}
|
|
1352
|
+
* 缓存装饰器 - 用于缓存验证函数结果
|
|
1180
1353
|
*/
|
|
1181
|
-
function
|
|
1182
|
-
return function (
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1354
|
+
function cached(validator, ttl) {
|
|
1355
|
+
return function (target, propertyName, descriptor) {
|
|
1356
|
+
const originalMethod = descriptor.value;
|
|
1357
|
+
descriptor.value = function (...args) {
|
|
1358
|
+
const value = args[0];
|
|
1359
|
+
const additionalArgs = args.slice(1);
|
|
1360
|
+
// 尝试从缓存获取结果
|
|
1361
|
+
const cachedResult = validationCache.get(validator, value, ...additionalArgs);
|
|
1362
|
+
if (cachedResult !== undefined) {
|
|
1363
|
+
return cachedResult;
|
|
1364
|
+
}
|
|
1365
|
+
// 执行验证并缓存结果
|
|
1366
|
+
const endTimer = performanceMonitor.startTimer(validator);
|
|
1367
|
+
try {
|
|
1368
|
+
const result = originalMethod.apply(this, args);
|
|
1369
|
+
validationCache.set(validator, value, result, ...additionalArgs);
|
|
1370
|
+
// 如果指定了TTL,设置过期时间
|
|
1371
|
+
if (ttl && ttl > 0) {
|
|
1372
|
+
validationCache.setTTL(validator, value, ttl, ...additionalArgs);
|
|
1195
1373
|
}
|
|
1374
|
+
return result;
|
|
1196
1375
|
}
|
|
1197
|
-
|
|
1376
|
+
finally {
|
|
1377
|
+
endTimer();
|
|
1378
|
+
}
|
|
1379
|
+
};
|
|
1380
|
+
return descriptor;
|
|
1198
1381
|
};
|
|
1199
1382
|
}
|
|
1200
1383
|
/**
|
|
1201
|
-
*
|
|
1202
|
-
*
|
|
1203
|
-
* @export
|
|
1204
|
-
* @param {string} {string} region 2 characters uppercase country code (e.g. DE, US, CH).
|
|
1205
|
-
* If users must enter the intl. prefix (e.g. +41), then you may pass "ZZ" or null as region.
|
|
1206
|
-
* See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
|
|
1207
|
-
* {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
|
|
1208
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1209
|
-
* @returns {PropertyDecorator}
|
|
1384
|
+
* 获取所有缓存统计信息
|
|
1210
1385
|
*/
|
|
1211
|
-
function
|
|
1212
|
-
return
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
propertyName,
|
|
1218
|
-
options: validationOptions,
|
|
1219
|
-
validator: {
|
|
1220
|
-
validate(value, _args) {
|
|
1221
|
-
return isPhoneNumber(value, region);
|
|
1222
|
-
},
|
|
1223
|
-
defaultMessage(_args) {
|
|
1224
|
-
return `invalid parameter ($property).`;
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
});
|
|
1386
|
+
function getAllCacheStats() {
|
|
1387
|
+
return {
|
|
1388
|
+
validation: validationCache.getStats(),
|
|
1389
|
+
regex: regexCache.getStats(),
|
|
1390
|
+
performance: performanceMonitor.getReport(),
|
|
1391
|
+
hotspots: performanceMonitor.getHotspots(),
|
|
1228
1392
|
};
|
|
1229
1393
|
}
|
|
1230
1394
|
/**
|
|
1231
|
-
*
|
|
1232
|
-
*
|
|
1233
|
-
* @export
|
|
1234
|
-
* @param {IsURLOptions} [options]
|
|
1235
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1236
|
-
* @returns {PropertyDecorator}
|
|
1395
|
+
* 预热缓存 - 预编译常用正则表达式
|
|
1237
1396
|
*/
|
|
1238
|
-
function
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
validate(value, _args) {
|
|
1248
|
-
return isURL(value, options);
|
|
1249
|
-
},
|
|
1250
|
-
defaultMessage(_args) {
|
|
1251
|
-
return `invalid parameter ($property).`;
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
});
|
|
1255
|
-
};
|
|
1397
|
+
function warmupCaches() {
|
|
1398
|
+
// 预编译中文验证相关的正则表达式
|
|
1399
|
+
const commonPatterns = [
|
|
1400
|
+
{ pattern: '^[\u4e00-\u9fa5]{2,8}$' }, // 中文姓名
|
|
1401
|
+
{ pattern: '^1[3-9]\\d{9}$' }, // 手机号
|
|
1402
|
+
{ pattern: '^\\d{6}$' }, // 邮政编码
|
|
1403
|
+
{ pattern: '^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]' }, // 车牌号开头
|
|
1404
|
+
];
|
|
1405
|
+
regexCache.precompile(commonPatterns);
|
|
1256
1406
|
}
|
|
1257
1407
|
/**
|
|
1258
|
-
*
|
|
1259
|
-
* 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
|
|
1260
|
-
*
|
|
1261
|
-
* @export
|
|
1262
|
-
* @param {HashAlgorithm} algorithm
|
|
1263
|
-
* @param {ValidationOptions} [validationOptions]
|
|
1264
|
-
* @returns {PropertyDecorator}
|
|
1408
|
+
* 清空所有缓存
|
|
1265
1409
|
*/
|
|
1266
|
-
function
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
name: "vIsHash",
|
|
1271
|
-
target: object.constructor,
|
|
1272
|
-
propertyName,
|
|
1273
|
-
options: validationOptions,
|
|
1274
|
-
validator: {
|
|
1275
|
-
validate(value, _args) {
|
|
1276
|
-
return isHash(value, algorithm);
|
|
1277
|
-
},
|
|
1278
|
-
defaultMessage(_args) {
|
|
1279
|
-
return `invalid parameter, ($property) must be is an ${algorithm} Hash string.`;
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
});
|
|
1283
|
-
};
|
|
1410
|
+
function clearAllCaches() {
|
|
1411
|
+
validationCache.clear();
|
|
1412
|
+
regexCache.clear();
|
|
1413
|
+
performanceMonitor.clear();
|
|
1284
1414
|
}
|
|
1285
1415
|
/**
|
|
1286
|
-
*
|
|
1287
|
-
* @param func
|
|
1288
|
-
* @param validationOptions
|
|
1289
|
-
* @returns
|
|
1416
|
+
* 配置缓存设置
|
|
1290
1417
|
*/
|
|
1291
|
-
function
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
validate(value, _args) {
|
|
1301
|
-
return func(value);
|
|
1302
|
-
},
|
|
1303
|
-
defaultMessage(_args) {
|
|
1304
|
-
return `invalid parameter ($property).`;
|
|
1305
|
-
}
|
|
1306
|
-
}
|
|
1307
|
-
});
|
|
1308
|
-
};
|
|
1418
|
+
function configureCaches(options) {
|
|
1419
|
+
if (options.validation) {
|
|
1420
|
+
// 重新创建验证缓存实例
|
|
1421
|
+
ValidationCache.instance = new ValidationCache(options.validation);
|
|
1422
|
+
}
|
|
1423
|
+
if (options.regex) {
|
|
1424
|
+
// 重新创建正则缓存实例
|
|
1425
|
+
RegexCache.instance = new RegexCache(options.regex);
|
|
1426
|
+
}
|
|
1309
1427
|
}
|
|
1310
1428
|
|
|
1311
|
-
export {
|
|
1429
|
+
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, Valid, ValidFuncs, Validated, cached, checkParamsType, clearAllCaches, configureCaches, convertDtoParamsType, convertParamsType, createParameterizedDecorator, createSimpleDecorator, createValidationDecorator, createValidationError, createValidationErrors, errorFormatter, getAllCacheStats, metadataCache, paramterTypes, performanceMonitor, plainToClass, regexCache, setValidationLanguage, validationCache, warmupCaches };
|