koatty_validation 1.4.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/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-06-10 23:32:33
3
+ * @Date: 2025-10-23 20:54:44
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
@@ -14,7 +14,7 @@ import { ValidationOptions } from 'class-validator';
14
14
  export declare function cached(validator: string, ttl?: number): (target: any, propertyName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
15
15
 
16
16
  /**
17
- * 缓存配置选项
17
+ * Cache configuration options
18
18
  */
19
19
  export declare interface CacheOptions {
20
20
  max?: number;
@@ -32,6 +32,17 @@ export declare interface CacheOptions {
32
32
  */
33
33
  export declare function checkParamsType(value: any, type: string): any;
34
34
 
35
+ /**
36
+ * Synchronous validation function - Executes the actual validation logic
37
+ * @param args Method parameters
38
+ * @param paramTypes Parameter type metadata
39
+ * @returns Validated parameters and validation targets
40
+ */
41
+ export declare function checkValidated(args: any[], paramTypes: any[]): Promise<{
42
+ validatedArgs: any[];
43
+ validationTargets: any[];
44
+ }>;
45
+
35
46
  /**
36
47
  * ClassValidator for manual
37
48
  */
@@ -71,37 +82,37 @@ export declare function convertDtoParamsType(clazz: any, cls: any): any;
71
82
  export declare function convertParamsType(param: any, type: string): any;
72
83
 
73
84
  /**
74
- * 创建带参数的验证装饰器
75
- * @param name 装饰器名称
76
- * @param validator 验证函数
77
- * @param defaultMessage 默认错误信息
78
- * @returns 装饰器工厂函数
85
+ * Create parameterized validation decorator
86
+ * @param name Decorator name
87
+ * @param validator Validation function
88
+ * @param defaultMessage Default error message
89
+ * @returns Decorator factory function
79
90
  */
80
91
  export declare function createParameterizedDecorator(name: string, validator: ValidatorFunction, defaultMessage?: string): (...args: any[]) => (object: Object, propertyName: string) => void;
81
92
 
82
93
  /**
83
- * 创建简单验证装饰器(不需要额外参数)
84
- * @param name 装饰器名称
85
- * @param validator 验证函数
86
- * @param defaultMessage 默认错误信息
87
- * @returns 装饰器函数
94
+ * Create simple validation decorator (no additional parameters required)
95
+ * @param name Decorator name
96
+ * @param validator Validation function
97
+ * @param defaultMessage Default error message
98
+ * @returns Decorator function
88
99
  */
89
100
  export declare function createSimpleDecorator(name: string, validator: ValidatorFunction, defaultMessage?: string): (...args: any[]) => (object: Object, propertyName: string) => void;
90
101
 
91
102
  /**
92
- * 创建验证装饰器的工厂函数
93
- * @param options 装饰器配置选项
94
- * @returns 装饰器工厂函数
103
+ * Factory function to create validation decorators
104
+ * @param options Decorator configuration options
105
+ * @returns Decorator factory function
95
106
  */
96
107
  export declare function createValidationDecorator(options: DecoratorOptions): (...args: any[]) => (object: Object, propertyName: string) => void;
97
108
 
98
109
  /**
99
- * 创建验证错误
110
+ * Create validation error
100
111
  */
101
112
  export declare function createValidationError(field: string, value: any, constraint: string, customMessage?: string, context?: Record<string, any>): ValidationErrorDetail;
102
113
 
103
114
  /**
104
- * 批量创建验证错误
115
+ * Create validation errors in batch
105
116
  */
106
117
  export declare function createValidationErrors(errors: Array<{
107
118
  field: string;
@@ -112,7 +123,7 @@ export declare function createValidationErrors(errors: Array<{
112
123
  }>): KoattyValidationError;
113
124
 
114
125
  /**
115
- * 装饰器选项
126
+ * Decorator options
116
127
  */
117
128
  export declare interface DecoratorOptions {
118
129
  name: string;
@@ -126,7 +137,7 @@ export declare const ENABLE_VALIDATED = "ENABLE_VALIDATED";
126
137
  export declare const Equals: (...args: any[]) => (object: Object, propertyName: string) => void;
127
138
 
128
139
  /**
129
- * 错误信息国际化
140
+ * Error message internationalization
130
141
  */
131
142
  export declare const ERROR_MESSAGES: {
132
143
  readonly zh: {
@@ -182,45 +193,62 @@ export declare const ERROR_MESSAGES: {
182
193
  };
183
194
 
184
195
  /**
185
- * 全局错误信息格式化器实例
196
+ * Global error message formatter instance
186
197
  */
187
198
  export declare const errorFormatter: ErrorMessageFormatter;
188
199
 
189
200
  /**
190
- * 错误信息格式化器
201
+ * Error message formatter
191
202
  */
192
203
  export declare class ErrorMessageFormatter {
193
204
  constructor(language?: SupportedLanguage);
194
205
  /**
195
- * 设置语言
206
+ * Set language
196
207
  */
197
208
  setLanguage(language: SupportedLanguage): void;
198
209
  /**
199
- * 格式化错误消息
210
+ * Format error message
200
211
  */
201
212
  formatMessage(constraint: string, field: string, value?: any, context?: Record<string, any>): string;
202
213
  /**
203
- * 格式化值用于消息显示
214
+ * Format value for message display
204
215
  * @private
205
216
  */
206
217
  }
207
218
 
208
219
  /**
209
- * 标记属性为可导出
220
+ * Mark property as exportable
210
221
  */
211
222
  export declare function Expose(): PropertyDecorator;
212
223
 
213
224
  /**
214
225
  * Use functions or built-in rules for validation.
226
+ * Throws error if validation fails.
215
227
  *
216
228
  * @export
217
- * @param {ValidRules} rule
218
- * @param {unknown} value
219
- * @param {(string | ValidOtpions)} [options]
220
- * @returns {*}
221
229
  */
222
230
  export declare const FunctionValidator: {
223
- [key in ValidRules]: (value: unknown, options?: string | ValidOtpions) => void;
231
+ readonly IsNotEmpty: (value: unknown, options?: string | ValidOtpions) => void;
232
+ readonly IsDate: (value: unknown, options?: string | ValidOtpions) => void;
233
+ readonly IsEmail: (value: unknown, options?: string | ValidOtpions) => void;
234
+ readonly IsIP: (value: unknown, options?: string | ValidOtpions) => void;
235
+ readonly IsPhoneNumber: (value: unknown, options?: string | ValidOtpions) => void;
236
+ readonly IsUrl: (value: unknown, options?: string | ValidOtpions) => void;
237
+ readonly IsHash: (value: unknown, options?: string | ValidOtpions) => void;
238
+ readonly IsCnName: (value: unknown, options?: string | ValidOtpions) => void;
239
+ readonly IsIdNumber: (value: unknown, options?: string | ValidOtpions) => void;
240
+ readonly IsZipCode: (value: unknown, options?: string | ValidOtpions) => void;
241
+ readonly IsMobile: (value: unknown, options?: string | ValidOtpions) => void;
242
+ readonly IsPlateNumber: (value: unknown, options?: string | ValidOtpions) => void;
243
+ readonly Equals: (value: unknown, options?: string | ValidOtpions) => void;
244
+ readonly NotEquals: (value: unknown, options?: string | ValidOtpions) => void;
245
+ readonly Contains: (value: unknown, options?: string | ValidOtpions) => void;
246
+ readonly IsIn: (value: unknown, options?: string | ValidOtpions) => void;
247
+ readonly IsNotIn: (value: unknown, options?: string | ValidOtpions) => void;
248
+ readonly Gt: (value: unknown, options?: string | ValidOtpions) => void;
249
+ readonly Lt: (value: unknown, options?: string | ValidOtpions) => void;
250
+ readonly Gte: (value: unknown, options?: string | ValidOtpions) => void;
251
+ readonly Lte: (value: unknown, options?: string | ValidOtpions) => void;
224
252
  };
225
253
 
226
254
  /**
@@ -232,6 +260,10 @@ export declare function getAllCacheStats(): {
232
260
  max: number;
233
261
  calculatedSize: number;
234
262
  keyCount: number;
263
+ hits: number;
264
+ misses: number;
265
+ hitRate: number;
266
+ totalRequests: number;
235
267
  };
236
268
  regex: {
237
269
  size: number;
@@ -251,7 +283,7 @@ export declare const Gt: (...args: any[]) => (object: Object, propertyName: stri
251
283
  export declare const Gte: (...args: any[]) => (object: Object, propertyName: string) => void;
252
284
 
253
285
  /**
254
- * 哈希算法类型
286
+ * Hash algorithm type
255
287
  */
256
288
  export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
257
289
 
@@ -260,20 +292,20 @@ export declare const IsCnName: (...args: any[]) => (object: Object, propertyName
260
292
  export declare const IsDate: (...args: any[]) => (object: Object, propertyName: string) => void;
261
293
 
262
294
  /**
263
- * Expose的别名
295
+ * Alias for Expose
264
296
  */
265
297
  export declare function IsDefined(): PropertyDecorator;
266
298
 
267
299
  export declare function IsEmail(options?: IsEmailOptions, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
268
300
 
269
301
  /**
270
- * koatty_validation 类型定义
302
+ * koatty_validation type definitions
271
303
  * @author richen
272
304
  * @copyright Copyright (c) - <richenlin(at)gmail.com>
273
305
  * @license MIT
274
306
  */
275
307
  /**
276
- * 邮箱验证选项
308
+ * Email validation options
277
309
  */
278
310
  export declare interface IsEmailOptions {
279
311
  allow_display_name?: boolean;
@@ -303,7 +335,7 @@ export declare const IsPlateNumber: (...args: any[]) => (object: Object, propert
303
335
  export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions): (object: Object, propertyName: string) => void;
304
336
 
305
337
  /**
306
- * URL验证选项
338
+ * URL validation options
307
339
  */
308
340
  export declare interface IsURLOptions {
309
341
  protocols?: string[];
@@ -322,7 +354,7 @@ export declare interface IsURLOptions {
322
354
  export declare const IsZipCode: (...args: any[]) => (object: Object, propertyName: string) => void;
323
355
 
324
356
  /**
325
- * 增强的验证错误类
357
+ * Enhanced validation error class
326
358
  */
327
359
  export declare class KoattyValidationError extends Error {
328
360
  readonly errors: ValidationErrorDetail[];
@@ -330,15 +362,15 @@ export declare class KoattyValidationError extends Error {
330
362
  readonly timestamp: Date;
331
363
  constructor(errors: ValidationErrorDetail[], message?: string);
332
364
  /**
333
- * 获取第一个错误信息
365
+ * Get the first error message
334
366
  */
335
367
  getFirstError(): ValidationErrorDetail | undefined;
336
368
  /**
337
- * 获取指定字段的错误
369
+ * Get errors for a specific field
338
370
  */
339
371
  getFieldErrors(field: string): ValidationErrorDetail[];
340
372
  /**
341
- * 转换为JSON格式
373
+ * Convert to JSON format
342
374
  */
343
375
  toJSON(): {
344
376
  name: string;
@@ -354,28 +386,28 @@ export declare const Lt: (...args: any[]) => (object: Object, propertyName: stri
354
386
  export declare const Lte: (...args: any[]) => (object: Object, propertyName: string) => void;
355
387
 
356
388
  /**
357
- * 元数据缓存
389
+ * Metadata cache
358
390
  */
359
391
  declare class MetadataCache {
360
392
  static getInstance(): MetadataCache;
361
393
  /**
362
- * 获取类的元数据缓存
394
+ * Get metadata cache for a class
363
395
  */
364
396
  getClassCache(target: Function): Map<string, any>;
365
397
  /**
366
- * 缓存元数据
398
+ * Cache metadata
367
399
  */
368
400
  setMetadata(target: Function, key: string, value: any): void;
369
401
  /**
370
- * 获取缓存的元数据
402
+ * Get cached metadata
371
403
  */
372
404
  getMetadata(target: Function, key: string): any;
373
405
  /**
374
- * 检查是否已缓存
406
+ * Check if metadata is cached
375
407
  */
376
408
  hasMetadata(target: Function, key: string): boolean;
377
409
  /**
378
- * 清空指定类的缓存
410
+ * Clear cache for a specific class
379
411
  */
380
412
  clearClassCache(target: Function): void;
381
413
  }
@@ -388,6 +420,11 @@ export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
388
420
 
389
421
  export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
390
422
 
423
+ /**
424
+ * Parameter type key constant
425
+ */
426
+ export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
427
+
391
428
  /**
392
429
  * paramterTypes
393
430
  *
@@ -498,21 +535,21 @@ declare class RegexCache {
498
535
  export declare const regexCache: RegexCache;
499
536
 
500
537
  /**
501
- * 设置全局语言
538
+ * Set global language
502
539
  */
503
540
  export declare function setValidationLanguage(language: SupportedLanguage): void;
504
541
 
505
542
  /**
506
- * 改进的错误处理机制
543
+ * Improved error handling mechanism
507
544
  * @author richen
508
545
  */
509
546
  /**
510
- * 支持的语言
547
+ * Supported languages
511
548
  */
512
549
  export declare type SupportedLanguage = 'zh' | 'en';
513
550
 
514
551
  /**
515
- * 参数验证装饰器
552
+ * Parameter validation decorator
516
553
  */
517
554
  export declare function Valid(rule: ValidRules | ValidRules[] | Function, options?: string | ValidOtpions): ParameterDecorator;
518
555
 
@@ -538,12 +575,16 @@ declare class ValidateClass {
538
575
  }
539
576
 
540
577
  /**
541
- * 方法验证装饰器
578
+ * Method validation decorator
579
+ * Automatically validates DTO objects in method parameters
580
+ * @param isAsync Whether to use async validation mode, default is true
581
+ * - true: Async mode, validation is handled by IOC container in the framework (suitable for scenarios where parameter values need to be obtained asynchronously)
582
+ * - false: Sync mode, validation is performed immediately when the method is called (suitable for scenarios where parameter values are already prepared)
542
583
  */
543
- export declare function Validated(): MethodDecorator;
584
+ export declare function Validated(isAsync?: boolean): MethodDecorator;
544
585
 
545
586
  /**
546
- * 验证结果缓存
587
+ * Validation result cache
547
588
  */
548
589
  declare class ValidationCache {
549
590
  constructor(options?: CacheOptions);
@@ -582,6 +623,10 @@ declare class ValidationCache {
582
623
  max: number;
583
624
  calculatedSize: number;
584
625
  keyCount: number;
626
+ hits: number;
627
+ misses: number;
628
+ hitRate: number;
629
+ totalRequests: number;
585
630
  };
586
631
  /**
587
632
  * 设置缓存TTL
@@ -592,7 +637,7 @@ declare class ValidationCache {
592
637
  export declare const validationCache: ValidationCache;
593
638
 
594
639
  /**
595
- * 验证错误详情
640
+ * Validation error details
596
641
  */
597
642
  export declare interface ValidationErrorDetail {
598
643
  field: string;
@@ -603,7 +648,7 @@ export declare interface ValidationErrorDetail {
603
648
  }
604
649
 
605
650
  /**
606
- * 验证函数类型定义
651
+ * Validator function type definition
607
652
  */
608
653
  export declare type ValidatorFunction = (value: any, ...args: any[]) => boolean;
609
654
 
@@ -619,7 +664,7 @@ export declare const ValidFuncs: {
619
664
  /**
620
665
  * Checks if a given value is a real date.
621
666
  */
622
- IsDate: (value: unknown) => value is Date;
667
+ IsDate: (value: unknown) => boolean;
623
668
  /**
624
669
  * Checks if the string is an email. If given value is not a string, then it returns false.
625
670
  */
@@ -636,11 +681,11 @@ export declare const ValidFuncs: {
636
681
  * See [google-libphonenumber, metadata.js:countryCodeToRegionCodeMap on github]
637
682
  * {@link https://github.com/ruimarinho/google-libphonenumber/blob/1e46138878cff479aafe2ce62175c6c49cb58720/src/metadata.js#L33}
638
683
  */
639
- IsPhoneNumber: (value: string, region?: CountryCode) => boolean;
684
+ IsPhoneNumber: (value: unknown, region?: CountryCode) => boolean;
640
685
  /**
641
686
  * Checks if the string is an url. If given value is not a string, then it returns false.
642
687
  */
643
- IsUrl: (value: string, options?: IsURLOptions) => boolean;
688
+ IsUrl: (value: unknown, options?: IsURLOptions) => boolean;
644
689
  /**
645
690
  * check if the string is a hash of type algorithm. Algorithm is one of
646
691
  * ['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']
@@ -649,23 +694,23 @@ export declare const ValidFuncs: {
649
694
  /**
650
695
  * Checks if value is a chinese name.
651
696
  */
652
- IsCnName: (value: any) => boolean;
697
+ IsCnName: (value: unknown) => boolean;
653
698
  /**
654
699
  * Checks if value is a idcard number.
655
700
  */
656
- IsIdNumber: (value: any) => boolean;
701
+ IsIdNumber: (value: unknown) => boolean;
657
702
  /**
658
703
  * Checks if value is a zipCode.
659
704
  */
660
- IsZipCode: (value: any) => boolean;
705
+ IsZipCode: (value: unknown) => boolean;
661
706
  /**
662
707
  * Checks if value is a mobile phone number.
663
708
  */
664
- IsMobile: (value: any) => boolean;
709
+ IsMobile: (value: unknown) => boolean;
665
710
  /**
666
711
  * Checks if value is a plateNumber.
667
712
  */
668
- IsPlateNumber: (value: any) => boolean;
713
+ IsPlateNumber: (value: unknown) => boolean;
669
714
  /**
670
715
  * Checks if value matches ("===") the comparison.
671
716
  */
@@ -689,23 +734,23 @@ export declare const ValidFuncs: {
689
734
  /**
690
735
  * Checks if the first number is greater than or equal to the second.
691
736
  */
692
- Gt: (num: unknown, min: number) => boolean;
737
+ Gt: (value: unknown, min: number) => boolean;
693
738
  /**
694
739
  * Checks if the first number is less than or equal to the second.
695
740
  */
696
- Lt: (num: unknown, max: number) => boolean;
741
+ Lt: (value: unknown, max: number) => boolean;
697
742
  /**
698
743
  * Checks if the first number is greater than or equal to the second.
699
744
  */
700
- Gte: (num: unknown, min: number) => boolean;
745
+ Gte: (value: unknown, min: number) => boolean;
701
746
  /**
702
747
  * Checks if the first number is less than or equal to the second.
703
748
  */
704
- Lte: (num: unknown, max: number) => boolean;
749
+ Lte: (value: unknown, max: number) => boolean;
705
750
  };
706
751
 
707
752
  /**
708
- * 验证选项
753
+ * Validation options
709
754
  */
710
755
  export declare type ValidOtpions = {
711
756
  message: string;