koatty_validation 1.6.0 → 1.6.3
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/.turbo/turbo-build.log +120 -0
- package/.turbo/turbo-test.log +8 -0
- package/CHANGELOG.md +31 -26
- package/README.md +106 -3
- package/dist/README.md +106 -3
- package/dist/index.d.ts +94 -60
- package/dist/index.js +337 -284
- package/dist/index.mjs +338 -286
- package/dist/package.json +10 -14
- package/examples/validated-async-sync-example.ts +213 -0
- package/package.json +21 -26
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date:
|
|
3
|
+
* @Date: 2026-01-28 12:40:19
|
|
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 { CountryCode } from 'libphonenumber-js';
|
|
9
|
-
import { ValidationOptions } from 'class-validator';
|
|
9
|
+
import { ValidationOptions as ValidationOptions_2 } from 'class-validator';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* 缓存装饰器 - 用于缓存验证函数结果
|
|
@@ -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;
|
|
@@ -109,10 +120,10 @@ export declare function createValidationErrors(errors: Array<{
|
|
|
109
120
|
constraint: string;
|
|
110
121
|
message?: string;
|
|
111
122
|
context?: Record<string, any>;
|
|
112
|
-
}
|
|
123
|
+
}>, separator?: string): 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,31 +193,31 @@ 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
|
|
|
@@ -272,7 +283,7 @@ export declare const Gt: (...args: any[]) => (object: Object, propertyName: stri
|
|
|
272
283
|
export declare const Gte: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
273
284
|
|
|
274
285
|
/**
|
|
275
|
-
*
|
|
286
|
+
* Hash algorithm type
|
|
276
287
|
*/
|
|
277
288
|
export declare type HashAlgorithm = "md4" | "md5" | "sha1" | "sha256" | "sha384" | "sha512" | "ripemd128" | "ripemd160" | "tiger128" | "tiger160" | "tiger192" | "crc32" | "crc32b";
|
|
278
289
|
|
|
@@ -281,20 +292,20 @@ export declare const IsCnName: (...args: any[]) => (object: Object, propertyName
|
|
|
281
292
|
export declare const IsDate: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
282
293
|
|
|
283
294
|
/**
|
|
284
|
-
* Expose
|
|
295
|
+
* Alias for Expose
|
|
285
296
|
*/
|
|
286
297
|
export declare function IsDefined(): PropertyDecorator;
|
|
287
298
|
|
|
288
|
-
export declare function IsEmail(options?: IsEmailOptions, validationOptions?:
|
|
299
|
+
export declare function IsEmail(options?: IsEmailOptions | ValidationOptions_2, validationOptions?: ValidationOptions_2): (object: Object, propertyName: string) => void;
|
|
289
300
|
|
|
290
301
|
/**
|
|
291
|
-
* koatty_validation
|
|
302
|
+
* koatty_validation type definitions
|
|
292
303
|
* @author richen
|
|
293
304
|
* @copyright Copyright (c) - <richenlin(at)gmail.com>
|
|
294
305
|
* @license MIT
|
|
295
306
|
*/
|
|
296
307
|
/**
|
|
297
|
-
*
|
|
308
|
+
* Email validation options
|
|
298
309
|
*/
|
|
299
310
|
export declare interface IsEmailOptions {
|
|
300
311
|
allow_display_name?: boolean;
|
|
@@ -303,13 +314,13 @@ export declare interface IsEmailOptions {
|
|
|
303
314
|
require_tld?: boolean;
|
|
304
315
|
}
|
|
305
316
|
|
|
306
|
-
export declare function IsHash(algorithm: HashAlgorithm, validationOptions?:
|
|
317
|
+
export declare function IsHash(algorithm: HashAlgorithm, validationOptions?: ValidationOptions_2): (object: Object, propertyName: string) => void;
|
|
307
318
|
|
|
308
319
|
export declare const IsIdNumber: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
309
320
|
|
|
310
321
|
export declare const IsIn: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
311
322
|
|
|
312
|
-
export declare function IsIP(version?: any, validationOptions?:
|
|
323
|
+
export declare function IsIP(version?: any, validationOptions?: ValidationOptions_2): (object: Object, propertyName: string) => void;
|
|
313
324
|
|
|
314
325
|
export declare const IsMobile: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
315
326
|
|
|
@@ -317,14 +328,14 @@ export declare const IsNotEmpty: (...args: any[]) => (object: Object, propertyNa
|
|
|
317
328
|
|
|
318
329
|
export declare const IsNotIn: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
319
330
|
|
|
320
|
-
export declare function IsPhoneNumber(region?: CountryCode, validationOptions?:
|
|
331
|
+
export declare function IsPhoneNumber(region?: CountryCode, validationOptions?: ValidationOptions_2): (object: Object, propertyName: string) => void;
|
|
321
332
|
|
|
322
333
|
export declare const IsPlateNumber: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
323
334
|
|
|
324
|
-
export declare function IsUrl(options?: IsURLOptions, validationOptions?:
|
|
335
|
+
export declare function IsUrl(options?: IsURLOptions, validationOptions?: ValidationOptions_2): (object: Object, propertyName: string) => void;
|
|
325
336
|
|
|
326
337
|
/**
|
|
327
|
-
* URL
|
|
338
|
+
* URL validation options
|
|
328
339
|
*/
|
|
329
340
|
export declare interface IsURLOptions {
|
|
330
341
|
protocols?: string[];
|
|
@@ -343,7 +354,7 @@ export declare interface IsURLOptions {
|
|
|
343
354
|
export declare const IsZipCode: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
344
355
|
|
|
345
356
|
/**
|
|
346
|
-
*
|
|
357
|
+
* Enhanced validation error class
|
|
347
358
|
*/
|
|
348
359
|
export declare class KoattyValidationError extends Error {
|
|
349
360
|
readonly errors: ValidationErrorDetail[];
|
|
@@ -351,15 +362,15 @@ export declare class KoattyValidationError extends Error {
|
|
|
351
362
|
readonly timestamp: Date;
|
|
352
363
|
constructor(errors: ValidationErrorDetail[], message?: string);
|
|
353
364
|
/**
|
|
354
|
-
*
|
|
365
|
+
* Get the first error message
|
|
355
366
|
*/
|
|
356
367
|
getFirstError(): ValidationErrorDetail | undefined;
|
|
357
368
|
/**
|
|
358
|
-
*
|
|
369
|
+
* Get errors for a specific field
|
|
359
370
|
*/
|
|
360
371
|
getFieldErrors(field: string): ValidationErrorDetail[];
|
|
361
372
|
/**
|
|
362
|
-
*
|
|
373
|
+
* Convert to JSON format
|
|
363
374
|
*/
|
|
364
375
|
toJSON(): {
|
|
365
376
|
name: string;
|
|
@@ -375,28 +386,28 @@ export declare const Lt: (...args: any[]) => (object: Object, propertyName: stri
|
|
|
375
386
|
export declare const Lte: (...args: any[]) => (object: Object, propertyName: string) => void;
|
|
376
387
|
|
|
377
388
|
/**
|
|
378
|
-
*
|
|
389
|
+
* Metadata cache
|
|
379
390
|
*/
|
|
380
391
|
declare class MetadataCache {
|
|
381
392
|
static getInstance(): MetadataCache;
|
|
382
393
|
/**
|
|
383
|
-
*
|
|
394
|
+
* Get metadata cache for a class
|
|
384
395
|
*/
|
|
385
396
|
getClassCache(target: Function): Map<string, any>;
|
|
386
397
|
/**
|
|
387
|
-
*
|
|
398
|
+
* Cache metadata
|
|
388
399
|
*/
|
|
389
400
|
setMetadata(target: Function, key: string, value: any): void;
|
|
390
401
|
/**
|
|
391
|
-
*
|
|
402
|
+
* Get cached metadata
|
|
392
403
|
*/
|
|
393
404
|
getMetadata(target: Function, key: string): any;
|
|
394
405
|
/**
|
|
395
|
-
*
|
|
406
|
+
* Check if metadata is cached
|
|
396
407
|
*/
|
|
397
408
|
hasMetadata(target: Function, key: string): boolean;
|
|
398
409
|
/**
|
|
399
|
-
*
|
|
410
|
+
* Clear cache for a specific class
|
|
400
411
|
*/
|
|
401
412
|
clearClassCache(target: Function): void;
|
|
402
413
|
}
|
|
@@ -410,7 +421,7 @@ export declare const PARAM_CHECK_KEY = "PARAM_CHECK_KEY";
|
|
|
410
421
|
export declare const PARAM_RULE_KEY = "PARAM_RULE_KEY";
|
|
411
422
|
|
|
412
423
|
/**
|
|
413
|
-
*
|
|
424
|
+
* Parameter type key constant
|
|
414
425
|
*/
|
|
415
426
|
export declare const PARAM_TYPE_KEY = "PARAM_TYPE_KEY";
|
|
416
427
|
|
|
@@ -524,21 +535,21 @@ declare class RegexCache {
|
|
|
524
535
|
export declare const regexCache: RegexCache;
|
|
525
536
|
|
|
526
537
|
/**
|
|
527
|
-
*
|
|
538
|
+
* Set global language
|
|
528
539
|
*/
|
|
529
540
|
export declare function setValidationLanguage(language: SupportedLanguage): void;
|
|
530
541
|
|
|
531
542
|
/**
|
|
532
|
-
*
|
|
543
|
+
* Improved error handling mechanism
|
|
533
544
|
* @author richen
|
|
534
545
|
*/
|
|
535
546
|
/**
|
|
536
|
-
*
|
|
547
|
+
* Supported languages
|
|
537
548
|
*/
|
|
538
549
|
export declare type SupportedLanguage = 'zh' | 'en';
|
|
539
550
|
|
|
540
551
|
/**
|
|
541
|
-
*
|
|
552
|
+
* Parameter validation decorator
|
|
542
553
|
*/
|
|
543
554
|
export declare function Valid(rule: ValidRules | ValidRules[] | Function, options?: string | ValidOtpions): ParameterDecorator;
|
|
544
555
|
|
|
@@ -557,20 +568,24 @@ declare class ValidateClass {
|
|
|
557
568
|
* @param {*} Clazz
|
|
558
569
|
* @param {*} data
|
|
559
570
|
* @param {boolean} [convert=false] auto convert parameters type
|
|
571
|
+
* @param {ValidationOptions} [options] validation options (returnAllErrors, errorSeparator)
|
|
560
572
|
* @returns {Promise<any>}
|
|
561
573
|
* @memberof ValidateClass
|
|
562
574
|
*/
|
|
563
|
-
valid(Clazz: any, data: any, convert?: boolean): Promise<any>;
|
|
575
|
+
valid(Clazz: any, data: any, convert?: boolean, options?: ValidationOptions): Promise<any>;
|
|
564
576
|
}
|
|
565
577
|
|
|
566
578
|
/**
|
|
567
|
-
*
|
|
568
|
-
*
|
|
579
|
+
* Method validation decorator
|
|
580
|
+
* Automatically validates DTO objects in method parameters
|
|
581
|
+
* @param isAsync Whether to use async validation mode, default is true
|
|
582
|
+
* - true: Async mode, validation is handled by IOC container in the framework (suitable for scenarios where parameter values need to be obtained asynchronously)
|
|
583
|
+
* - false: Sync mode, validation is performed immediately when the method is called (suitable for scenarios where parameter values are already prepared)
|
|
569
584
|
*/
|
|
570
|
-
export declare function Validated(): MethodDecorator;
|
|
585
|
+
export declare function Validated(isAsync?: boolean): MethodDecorator;
|
|
571
586
|
|
|
572
587
|
/**
|
|
573
|
-
*
|
|
588
|
+
* Validation result cache
|
|
574
589
|
*/
|
|
575
590
|
declare class ValidationCache {
|
|
576
591
|
constructor(options?: CacheOptions);
|
|
@@ -623,7 +638,7 @@ declare class ValidationCache {
|
|
|
623
638
|
export declare const validationCache: ValidationCache;
|
|
624
639
|
|
|
625
640
|
/**
|
|
626
|
-
*
|
|
641
|
+
* Validation error details
|
|
627
642
|
*/
|
|
628
643
|
export declare interface ValidationErrorDetail {
|
|
629
644
|
field: string;
|
|
@@ -634,7 +649,26 @@ export declare interface ValidationErrorDetail {
|
|
|
634
649
|
}
|
|
635
650
|
|
|
636
651
|
/**
|
|
637
|
-
*
|
|
652
|
+
* Parameter type key constant
|
|
653
|
+
*/
|
|
654
|
+
/**
|
|
655
|
+
* Validation options for ClassValidator.valid()
|
|
656
|
+
*/
|
|
657
|
+
export declare interface ValidationOptions {
|
|
658
|
+
/**
|
|
659
|
+
* Whether to return all validation errors or only the first one
|
|
660
|
+
* @default false (backward compatibility - returns only first error)
|
|
661
|
+
*/
|
|
662
|
+
returnAllErrors?: boolean;
|
|
663
|
+
/**
|
|
664
|
+
* Separator for joining multiple error messages
|
|
665
|
+
* @default '; '
|
|
666
|
+
*/
|
|
667
|
+
errorSeparator?: string;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Validator function type definition
|
|
638
672
|
*/
|
|
639
673
|
export declare type ValidatorFunction = (value: any, ...args: any[]) => boolean;
|
|
640
674
|
|
|
@@ -736,7 +770,7 @@ export declare const ValidFuncs: {
|
|
|
736
770
|
};
|
|
737
771
|
|
|
738
772
|
/**
|
|
739
|
-
*
|
|
773
|
+
* Validation options
|
|
740
774
|
*/
|
|
741
775
|
export declare type ValidOtpions = {
|
|
742
776
|
message: string;
|