reslib 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/auth/errors.d.ts +4 -0
- package/build/auth/errors.js +5 -0
- package/build/auth/index.d.ts +65 -1963
- package/build/auth/index.js +2 -2
- package/build/auth/types.d.ts +42 -0
- package/build/exception/index.js +2 -2
- package/build/resources/index.d.ts +5 -5
- package/build/resources/index.js +3 -3
- package/build/validator/index.js +3 -3
- package/build/validator/rules/array.js +2 -2
- package/build/validator/rules/boolean.js +2 -2
- package/build/validator/rules/date.js +2 -2
- package/build/validator/rules/default.js +2 -2
- package/build/validator/rules/enum.js +2 -2
- package/build/validator/rules/file.js +2 -2
- package/build/validator/rules/format.d.ts +1 -1
- package/build/validator/rules/format.js +3 -3
- package/build/validator/rules/ifRule.d.ts +76 -0
- package/build/validator/rules/ifRule.js +5 -0
- package/build/validator/rules/index.d.ts +1 -0
- package/build/validator/rules/index.js +3 -3
- package/build/validator/rules/multiRules.js +2 -2
- package/build/validator/rules/numeric.js +2 -2
- package/build/validator/rules/object.js +2 -2
- package/build/validator/rules/string.js +2 -2
- package/build/validator/rules/target.js +2 -2
- package/build/validator/rulesMarkers.d.ts +1 -0
- package/build/validator/rulesMarkers.js +1 -1
- package/build/validator/types.d.ts +399 -48
- package/build/validator/validator.d.ts +376 -5
- package/build/validator/validator.js +2 -2
- package/package.json +251 -253
|
@@ -394,7 +394,7 @@ type ExtractOptionalOrEmptyKeys<T> = {
|
|
|
394
394
|
*
|
|
395
395
|
* @template TParams - The type of the rule parameters
|
|
396
396
|
*/
|
|
397
|
-
export interface ValidatorRuleConfig<TParams> {
|
|
397
|
+
export interface ValidatorRuleConfig<TParams> extends ValidatorMessageConfig {
|
|
398
398
|
/**
|
|
399
399
|
* The parameters for the validation rule.
|
|
400
400
|
*
|
|
@@ -402,11 +402,42 @@ export interface ValidatorRuleConfig<TParams> {
|
|
|
402
402
|
* The type of these parameters is defined in `ValidatorRuleParamTypes`.
|
|
403
403
|
*/
|
|
404
404
|
params: TParams;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* ## Validator Message Config
|
|
408
|
+
*
|
|
409
|
+
* Configuration interface for custom validation error messages.
|
|
410
|
+
* This interface is used as a mixin to add optional custom message support
|
|
411
|
+
* to validation rule configurations.
|
|
412
|
+
*
|
|
413
|
+
* ### Purpose
|
|
414
|
+
* Provides a standardized way to override default error messages in validation rules.
|
|
415
|
+
* Can be extended by other interfaces that need custom message capability.
|
|
416
|
+
*
|
|
417
|
+
* ### Message Types Supported
|
|
418
|
+
* 1. **Static strings**: Direct error messages
|
|
419
|
+
* 2. **Translation keys**: i18n keys for localized messages
|
|
420
|
+
* 3. **Dynamic functions**: Functions that generate messages based on validation context
|
|
421
|
+
*
|
|
422
|
+
* ### Usage
|
|
423
|
+
* This interface is typically extended by:
|
|
424
|
+
* - {@link ValidatorRuleConfig} - Rule configuration with parameters
|
|
425
|
+
* - {@link ValidatorSanitizedRuleObject} - Sanitized rule objects
|
|
426
|
+
* - {@link ValidatorMultiRuleOptions} - Multi-rule validation options
|
|
427
|
+
*
|
|
428
|
+
* @since 1.2.0
|
|
429
|
+
* @see {@link ValidatorRuleConfigMessage} - The message type definition
|
|
430
|
+
* @see {@link Validator.translateRuleConfigMessage} - Method to process these messages
|
|
431
|
+
*/
|
|
432
|
+
export interface ValidatorMessageConfig {
|
|
405
433
|
/**
|
|
406
|
-
*
|
|
434
|
+
* The error message associated with the validation rule.
|
|
407
435
|
*
|
|
408
436
|
* This property allows strict control over the error feedback. It accepts a string
|
|
409
437
|
* (static or i18n key) or a function for dynamic message generation.
|
|
438
|
+
|
|
439
|
+
* It overrides the default error message generated by the `ruleFunction` if validation fails.
|
|
440
|
+
* Can be a static string, a translation key, or a function that returns a string.
|
|
410
441
|
*
|
|
411
442
|
* **Important:** If specified, this message will be used if validation fails, ignoring any
|
|
412
443
|
* message returned by the rule function itself.
|
|
@@ -414,7 +445,26 @@ export interface ValidatorRuleConfig<TParams> {
|
|
|
414
445
|
* **Note:** If a function is provided, verify it does not throw. Exceptions are caught and ignored,
|
|
415
446
|
* causing a fallback to the default message.
|
|
416
447
|
*
|
|
448
|
+
* @type {ValidatorRuleConfigMessage}
|
|
449
|
+
* @since 1.2.0
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* // Static string
|
|
453
|
+
* message: "This field is required."
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* // Translation key
|
|
457
|
+
* message: "auth.email.required"
|
|
458
|
+
*
|
|
417
459
|
* @example
|
|
460
|
+
* // Dynamic function
|
|
461
|
+
* message: ({ value, ruleParams }) => `Value must be between ${ruleParams[0]} and ${ruleParams[1]}`
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* // Dynamic message using i18n from options
|
|
465
|
+
* message: ({ value, i18n }) => i18n.t('validation.min_length', { value, min: 5 })
|
|
466
|
+
*
|
|
467
|
+
* * @example
|
|
418
468
|
* // Static message
|
|
419
469
|
* message: "This field is required"
|
|
420
470
|
*
|
|
@@ -430,6 +480,7 @@ export interface ValidatorRuleConfig<TParams> {
|
|
|
430
480
|
* // Dynamic message using i18n from options
|
|
431
481
|
* message: ({ value, i18n }) => i18n.t('validation.custom_error', { value })
|
|
432
482
|
*
|
|
483
|
+
|
|
433
484
|
* @see {@link ValidatorRuleConfigMessage}
|
|
434
485
|
*/
|
|
435
486
|
message?: ValidatorRuleConfigMessage;
|
|
@@ -710,7 +761,7 @@ export type ValidatorSanitizedRule<TParams extends ValidatorRuleParams = Validat
|
|
|
710
761
|
* @see {@link ValidatorRuleFunction} - The validation function type
|
|
711
762
|
* @see {@link ValidatorRuleName} - Rule name type
|
|
712
763
|
*/
|
|
713
|
-
export interface ValidatorSanitizedRuleObject<TParams extends ValidatorRuleParams = ValidatorRuleParams, Context = unknown> {
|
|
764
|
+
export interface ValidatorSanitizedRuleObject<TParams extends ValidatorRuleParams = ValidatorRuleParams, Context = unknown> extends ValidatorMessageConfig {
|
|
714
765
|
/**
|
|
715
766
|
* The parsed name of the validation rule
|
|
716
767
|
*
|
|
@@ -749,40 +800,6 @@ export interface ValidatorSanitizedRuleObject<TParams extends ValidatorRuleParam
|
|
|
749
800
|
* @see {@link ValidatorRuleFunction}
|
|
750
801
|
*/
|
|
751
802
|
ruleFunction: ValidatorRuleFunction<TParams, Context>;
|
|
752
|
-
/**
|
|
753
|
-
* The error message associated with the validation rule.
|
|
754
|
-
*
|
|
755
|
-
* Overrides the default error message generated by the `ruleFunction` if validation fails.
|
|
756
|
-
* Can be a static string, a translation key, or a function that returns a string.
|
|
757
|
-
*
|
|
758
|
-
* **Important:** If specified, this message will be used if validation fails, ignoring any
|
|
759
|
-
* message returned by the rule function itself.
|
|
760
|
-
*
|
|
761
|
-
* **Note:** If a function is provided, verify it does not throw. Exceptions are caught and ignored,
|
|
762
|
-
* causing a fallback to the default message.
|
|
763
|
-
*
|
|
764
|
-
* @type {ValidatorRuleConfigMessage}
|
|
765
|
-
* @since 1.2.0
|
|
766
|
-
*
|
|
767
|
-
* @example
|
|
768
|
-
* // Static string
|
|
769
|
-
* message: "This field is required."
|
|
770
|
-
*
|
|
771
|
-
* @example
|
|
772
|
-
* // Translation key
|
|
773
|
-
* message: "auth.email.required"
|
|
774
|
-
*
|
|
775
|
-
* @example
|
|
776
|
-
* // Dynamic function
|
|
777
|
-
* message: ({ value, ruleParams }) => `Value must be between ${ruleParams[0]} and ${ruleParams[1]}`
|
|
778
|
-
*
|
|
779
|
-
* @example
|
|
780
|
-
* // Dynamic message using i18n from options
|
|
781
|
-
* message: ({ value, i18n }) => i18n.t('validation.min_length', { value, min: 5 })
|
|
782
|
-
*
|
|
783
|
-
* @see {@link ValidatorRuleConfigMessage}
|
|
784
|
-
*/
|
|
785
|
-
message?: ValidatorRuleConfigMessage;
|
|
786
803
|
}
|
|
787
804
|
/**
|
|
788
805
|
* @typedef ValidatorSanitizedRules
|
|
@@ -1051,16 +1068,7 @@ export type ValidatorRuleParams<TParams extends Array<unknown> | ReadonlyArray<u
|
|
|
1051
1068
|
*
|
|
1052
1069
|
* @see {@link ClassConstructor} - Class constructor constraint
|
|
1053
1070
|
*/
|
|
1054
|
-
export interface ValidatorNestedRuleFunctionOptions<TClass extends ClassConstructor = ClassConstructor, Context = unknown> extends Omit<ValidatorClassOptions<TClass, Context, [
|
|
1055
|
-
classInstance: TClass,
|
|
1056
|
-
options?: {
|
|
1057
|
-
/**
|
|
1058
|
-
* The custom error message to use when validation fails.
|
|
1059
|
-
* It can be either a translation key or a custom message.
|
|
1060
|
-
*/
|
|
1061
|
-
message?: string;
|
|
1062
|
-
}
|
|
1063
|
-
]>, 'data'> {
|
|
1071
|
+
export interface ValidatorNestedRuleFunctionOptions<TClass extends ClassConstructor = ClassConstructor, Context = unknown> extends Omit<ValidatorClassOptions<TClass, Context, [classInstance: TClass]>, 'data'>, ValidatorMessageConfig {
|
|
1064
1072
|
value?: ValidatorClassInput<TClass>;
|
|
1065
1073
|
data?: Dictionary;
|
|
1066
1074
|
}
|
|
@@ -1271,6 +1279,7 @@ export interface ValidatorOptions<TParams extends ValidatorRuleParams = Validato
|
|
|
1271
1279
|
}
|
|
1272
1280
|
/**
|
|
1273
1281
|
* ## OneOf Rule Validation Options
|
|
1282
|
+
* @extends {ValidatorMessageConfig}
|
|
1274
1283
|
*
|
|
1275
1284
|
* Configuration interface for validating a value against an array of alternative validation rules
|
|
1276
1285
|
* where at least one rule must pass. This interface extends {@link ValidatorOptions}
|
|
@@ -1364,8 +1373,9 @@ export interface ValidatorOptions<TParams extends ValidatorRuleParams = Validato
|
|
|
1364
1373
|
* @see {@link ValidatorOptions} - Base options interface being extended
|
|
1365
1374
|
* @see {@link ValidatorRuleFunction} - Type of functions in ruleParams array
|
|
1366
1375
|
* @see {@link ValidatorResult} - Result type returned by validation
|
|
1376
|
+
* @see {@link ValidatorMessageConfig} - Message configuration interface
|
|
1367
1377
|
*/
|
|
1368
|
-
export interface ValidatorMultiRuleOptions<Context = unknown, RulesFunctions extends ValidatorDefaultMultiRule<Context> = ValidatorDefaultMultiRule<Context>> extends ValidatorOptions<RulesFunctions, Context
|
|
1378
|
+
export interface ValidatorMultiRuleOptions<Context = unknown, RulesFunctions extends ValidatorDefaultMultiRule<Context> = ValidatorDefaultMultiRule<Context>> extends ValidatorOptions<RulesFunctions, Context>, ValidatorMessageConfig {
|
|
1369
1379
|
startTime?: number;
|
|
1370
1380
|
}
|
|
1371
1381
|
/**
|
|
@@ -1471,6 +1481,347 @@ export type ValidatorDefaultMultiRule<Context = unknown, ParamsTypes extends Val
|
|
|
1471
1481
|
* @see {@link ValidatorRuleResult} - Return type
|
|
1472
1482
|
*/
|
|
1473
1483
|
export type ValidatorMultiRuleFunction<Context = unknown, RulesFunctions extends Array<ValidatorRule<Array<unknown>, Context>> = Array<ValidatorRule<Array<unknown>, Context>>> = ValidatorRuleFunction<RulesFunctions, Context>;
|
|
1484
|
+
/**
|
|
1485
|
+
* ## Validator If Rule Function
|
|
1486
|
+
*
|
|
1487
|
+
* Specialized validation function type for the conditional `@If` rule.
|
|
1488
|
+
* This type definition constrains the `ValidatorRuleFunction` to strictly accept
|
|
1489
|
+
* a single `ValidatorIfResolver` as its parameter source.
|
|
1490
|
+
*
|
|
1491
|
+
* ### Purpose
|
|
1492
|
+
* Standard validation rule functions are generic and can accept any array of parameters.
|
|
1493
|
+
* The `@If` rule specifically requires exactly one parameter: the resolver function.
|
|
1494
|
+
* This type alias enforces that constraint at the type system level.
|
|
1495
|
+
*
|
|
1496
|
+
* ### Structure
|
|
1497
|
+
* ```typescript
|
|
1498
|
+
* ValidatorRuleFunction<[ValidatorIfResolver<Context>], Context>
|
|
1499
|
+
* ```
|
|
1500
|
+
* - **Params**: Tuple containing exactly one `ValidatorIfResolver`
|
|
1501
|
+
* - **Context**: Generic shared validation context
|
|
1502
|
+
*
|
|
1503
|
+
* @template Context - The type of the validation context object (optional)
|
|
1504
|
+
*
|
|
1505
|
+
* @public
|
|
1506
|
+
* @see {@link Validator.if} - The factory method that returns this function type
|
|
1507
|
+
* @see {@link ValidatorIfResolver} - The resolver function used as the parameter
|
|
1508
|
+
*/
|
|
1509
|
+
export type ValidatorIfRuleFunction<Context = unknown> = ValidatorRuleFunction<[
|
|
1510
|
+
ValidatorIfResolver<Context>
|
|
1511
|
+
], Context>;
|
|
1512
|
+
/**
|
|
1513
|
+
* ## Validator If Resolver Result
|
|
1514
|
+
*
|
|
1515
|
+
* Defines the possible return types for a conditional validation resolver function.
|
|
1516
|
+
* This type allows the resolver to dynamically determine which rules to apply and
|
|
1517
|
+
* optionally specify a custom error message.
|
|
1518
|
+
*
|
|
1519
|
+
* ### Return Options
|
|
1520
|
+
*
|
|
1521
|
+
* 1. **`ValidatorRules`** (array): An array of validation rules to apply.
|
|
1522
|
+
* ```typescript
|
|
1523
|
+
* // Just return rules
|
|
1524
|
+
* return ['Required', 'Email'];
|
|
1525
|
+
* ```
|
|
1526
|
+
*
|
|
1527
|
+
* 2. **Object with `rules` and optional `message`**: Rules with a custom error message.
|
|
1528
|
+
* ```typescript
|
|
1529
|
+
* // Rules with custom message
|
|
1530
|
+
* return {
|
|
1531
|
+
* rules: ['Required', { MinLength: [8] }],
|
|
1532
|
+
* message: 'Password must be at least 8 characters',
|
|
1533
|
+
* };
|
|
1534
|
+
* ```
|
|
1535
|
+
*
|
|
1536
|
+
* 3. **Empty array `[]`**: Skip validation, return success.
|
|
1537
|
+
* ```typescript
|
|
1538
|
+
* // No validation needed
|
|
1539
|
+
* return [];
|
|
1540
|
+
* ```
|
|
1541
|
+
*
|
|
1542
|
+
* 4. **`undefined` or `null`**: Skip validation, return success.
|
|
1543
|
+
* ```typescript
|
|
1544
|
+
* // Skip validation
|
|
1545
|
+
* return undefined;
|
|
1546
|
+
* ```
|
|
1547
|
+
*
|
|
1548
|
+
* ### Examples
|
|
1549
|
+
*
|
|
1550
|
+
* #### Dynamic rules based on value
|
|
1551
|
+
* ```typescript
|
|
1552
|
+
* const resolver: ValidatorIfResolver = ({ value }) => {
|
|
1553
|
+
* if (!value) return []; // Skip if empty
|
|
1554
|
+
* return ['Email'];
|
|
1555
|
+
* };
|
|
1556
|
+
* ```
|
|
1557
|
+
*
|
|
1558
|
+
* #### Rules with dynamic message
|
|
1559
|
+
* ```typescript
|
|
1560
|
+
* const resolver: ValidatorIfResolver = ({ data, i18n }) => {
|
|
1561
|
+
* if (data?.role === 'admin') {
|
|
1562
|
+
* return {
|
|
1563
|
+
* rules: ['Required', 'StrongPassword'],
|
|
1564
|
+
* message: i18n.t('validation.admin_password_required'),
|
|
1565
|
+
* };
|
|
1566
|
+
* }
|
|
1567
|
+
* return ['Required'];
|
|
1568
|
+
* };
|
|
1569
|
+
* ```
|
|
1570
|
+
*
|
|
1571
|
+
* @template Context - Type of the optional validation context
|
|
1572
|
+
*
|
|
1573
|
+
* @public
|
|
1574
|
+
* @see {@link ValidatorIfResolver} - Function type that returns this result
|
|
1575
|
+
* @see {@link ValidatorRules} - Array of validation rules
|
|
1576
|
+
* @see {@link ValidatorRuleConfigMessage} - Custom message type
|
|
1577
|
+
*/
|
|
1578
|
+
export type ValidatorIfResolverResult<Context = unknown> = ValidatorRules<Context> | {
|
|
1579
|
+
/**
|
|
1580
|
+
* Array of validation rules to apply.
|
|
1581
|
+
*/
|
|
1582
|
+
rules: ValidatorRules<Context>;
|
|
1583
|
+
/**
|
|
1584
|
+
* Optional custom error message that overrides the default.
|
|
1585
|
+
* Can be a static string, i18n key, or dynamic function.
|
|
1586
|
+
*/
|
|
1587
|
+
message?: ValidatorRuleConfigMessage;
|
|
1588
|
+
} | undefined | null;
|
|
1589
|
+
/**
|
|
1590
|
+
* ## Validator If Resolver
|
|
1591
|
+
*
|
|
1592
|
+
* A function type that dynamically determines which validation rules should be applied
|
|
1593
|
+
* based on runtime conditions. This is the core of the conditional validation system,
|
|
1594
|
+
* providing a flexible way to implement context-aware validation logic.
|
|
1595
|
+
*
|
|
1596
|
+
* ### Purpose
|
|
1597
|
+
* Unlike traditional condition + then/otherwise approach, the resolver pattern combines
|
|
1598
|
+
* all decision logic into a single function that returns the rules to apply. This enables:
|
|
1599
|
+
* - **Complex conditional logic**: Multi-branch conditions, nested checks
|
|
1600
|
+
* - **Dynamic messages**: Different messages based on context
|
|
1601
|
+
* - **Rule composition**: Build rules dynamically from multiple sources
|
|
1602
|
+
* - **Early exit**: Return empty array to skip validation entirely
|
|
1603
|
+
*
|
|
1604
|
+
* ### Function Signature
|
|
1605
|
+
* The resolver receives an options object with:
|
|
1606
|
+
* - **value**: The value being validated
|
|
1607
|
+
* - **data**: Additional form/object data (other fields)
|
|
1608
|
+
* - **context**: Application-specific validation context
|
|
1609
|
+
* - **i18n**: Internationalization instance for localized logic
|
|
1610
|
+
* - **fieldName/propertyName**: Field identifiers
|
|
1611
|
+
* - **translatedPropertyName**: Localized field name
|
|
1612
|
+
*
|
|
1613
|
+
* ### Return Values
|
|
1614
|
+
* The resolver can return:
|
|
1615
|
+
* - **`ValidatorRules[]`**: Array of rules to apply
|
|
1616
|
+
* - **`{ rules, message? }`**: Rules with optional custom message
|
|
1617
|
+
* - **`[]` or `undefined`/`null`**: Skip validation (return success)
|
|
1618
|
+
*
|
|
1619
|
+
* ### Examples
|
|
1620
|
+
*
|
|
1621
|
+
* #### Optional Field Validation
|
|
1622
|
+
* ```typescript
|
|
1623
|
+
* // Only validate if value is present
|
|
1624
|
+
* const optionalEmail: ValidatorIfResolver = ({ value }) => {
|
|
1625
|
+
* if (value === undefined || value === '') {
|
|
1626
|
+
* return []; // Skip validation
|
|
1627
|
+
* }
|
|
1628
|
+
* return ['Email'];
|
|
1629
|
+
* };
|
|
1630
|
+
* ```
|
|
1631
|
+
*
|
|
1632
|
+
* #### Role-Based Validation
|
|
1633
|
+
* ```typescript
|
|
1634
|
+
* // Different rules based on user role
|
|
1635
|
+
* const passwordResolver: ValidatorIfResolver = ({ data }) => {
|
|
1636
|
+
* if (data?.role === 'admin') {
|
|
1637
|
+
* return ['Required', 'StrongPassword'];
|
|
1638
|
+
* }
|
|
1639
|
+
* if (data?.role === 'moderator') {
|
|
1640
|
+
* return ['Required', { MinLength: [8] }];
|
|
1641
|
+
* }
|
|
1642
|
+
* return ['Required', { MinLength: [6] }];
|
|
1643
|
+
* };
|
|
1644
|
+
* ```
|
|
1645
|
+
*
|
|
1646
|
+
* #### Context-Aware with Custom Message
|
|
1647
|
+
* ```typescript
|
|
1648
|
+
* // Strict mode validation with custom message
|
|
1649
|
+
* const strictResolver: ValidatorIfResolver = ({ context, i18n }) => {
|
|
1650
|
+
* if (context?.strictMode) {
|
|
1651
|
+
* return {
|
|
1652
|
+
* rules: ['Required', { MinLength: [12] }, 'StrongPassword'],
|
|
1653
|
+
* message: i18n.t('validation.strict_password_required'),
|
|
1654
|
+
* };
|
|
1655
|
+
* }
|
|
1656
|
+
* return ['Required'];
|
|
1657
|
+
* };
|
|
1658
|
+
* ```
|
|
1659
|
+
*
|
|
1660
|
+
* #### i18n-Aware Validation
|
|
1661
|
+
* ```typescript
|
|
1662
|
+
* // Different rules based on locale
|
|
1663
|
+
* const localeResolver: ValidatorIfResolver = ({ i18n, value }) => {
|
|
1664
|
+
* if (!value) return [];
|
|
1665
|
+
*
|
|
1666
|
+
* // French phone numbers have different format
|
|
1667
|
+
* if (i18n.locale === 'fr') {
|
|
1668
|
+
* return [{ Regex: [/^(?:(?:\\+|00)33|0)\\s*[1-9](?:[\\s.-]*\\d{2}){4}$/] }];
|
|
1669
|
+
* }
|
|
1670
|
+
* return ['PhoneNumber'];
|
|
1671
|
+
* };
|
|
1672
|
+
* ```
|
|
1673
|
+
*
|
|
1674
|
+
* #### Dependent Field Validation
|
|
1675
|
+
* ```typescript
|
|
1676
|
+
* // Validate based on another field's value
|
|
1677
|
+
* const confirmPasswordResolver: ValidatorIfResolver = ({ value, data }) => {
|
|
1678
|
+
* if (!data?.password) return []; // Skip if no password
|
|
1679
|
+
*
|
|
1680
|
+
* return {
|
|
1681
|
+
* rules: ['Required', { Equals: [data.password] }],
|
|
1682
|
+
* message: ({ i18n }) => i18n.t('validation.passwords_must_match'),
|
|
1683
|
+
* };
|
|
1684
|
+
* };
|
|
1685
|
+
* ```
|
|
1686
|
+
*
|
|
1687
|
+
* #### Async Resolver - Load Rules from Configuration
|
|
1688
|
+
* ```typescript
|
|
1689
|
+
* // Load validation rules from a database or configuration service
|
|
1690
|
+
* const dynamicRulesResolver: ValidatorIfResolver = async ({ context }) => {
|
|
1691
|
+
* const config = await loadValidationConfig(context?.tenantId);
|
|
1692
|
+
* if (config.strictMode) {
|
|
1693
|
+
* return config.strictRules;
|
|
1694
|
+
* }
|
|
1695
|
+
* return config.defaultRules;
|
|
1696
|
+
* };
|
|
1697
|
+
* ```
|
|
1698
|
+
*
|
|
1699
|
+
* #### Async Resolver - Feature Flag Check
|
|
1700
|
+
* ```typescript
|
|
1701
|
+
* // Conditional validation based on async feature flag
|
|
1702
|
+
* const betaFeatureResolver: ValidatorIfResolver = async ({ context }) => {
|
|
1703
|
+
* const isEnabled = await featureFlagService.isEnabled('advanced-validation');
|
|
1704
|
+
* if (isEnabled) {
|
|
1705
|
+
* return ['Required', 'AdvancedFormat'];
|
|
1706
|
+
* }
|
|
1707
|
+
* return ['Required'];
|
|
1708
|
+
* };
|
|
1709
|
+
* ```
|
|
1710
|
+
*
|
|
1711
|
+
* #### Async Resolver - External API Based
|
|
1712
|
+
* ```typescript
|
|
1713
|
+
* // Validation based on external API response
|
|
1714
|
+
* const countryBasedResolver: ValidatorIfResolver = async ({ value, i18n }) => {
|
|
1715
|
+
* const country = await geoService.detectCountry();
|
|
1716
|
+
* if (country === 'FR') {
|
|
1717
|
+
* return {
|
|
1718
|
+
* rules: [{ Regex: [/^[0-9]{5}$/] }],
|
|
1719
|
+
* message: i18n.t('validation.french_postal_code'),
|
|
1720
|
+
* };
|
|
1721
|
+
* }
|
|
1722
|
+
* return ['Required'];
|
|
1723
|
+
* };
|
|
1724
|
+
* ```
|
|
1725
|
+
*
|
|
1726
|
+
* ### Sync vs Async
|
|
1727
|
+
* The resolver supports both synchronous and asynchronous operations:
|
|
1728
|
+
* - **Sync**: Return the result directly for simple, non-blocking logic
|
|
1729
|
+
* - **Async**: Return a Promise for database lookups, API calls, or async configuration
|
|
1730
|
+
*
|
|
1731
|
+
* @template Context - Type of the optional validation context
|
|
1732
|
+
*
|
|
1733
|
+
* @public
|
|
1734
|
+
* @see {@link ValidatorIfResolverResult} - Return type for the resolver
|
|
1735
|
+
* @see {@link ValidatorIfRuleOptions} - Options interface for validateIfRule
|
|
1736
|
+
* @see {@link Validator.validateIfRule} - Method that executes this resolver
|
|
1737
|
+
* @see {@link Validator.if} - Factory method to create rule from resolver
|
|
1738
|
+
*/
|
|
1739
|
+
export type ValidatorIfResolver<Context = unknown> = (options: Pick<ValidatorOptions<ValidatorRuleParams, Context>, 'value' | 'data' | 'context' | 'fieldName' | 'propertyName' | 'translatedPropertyName' | 'i18n'>) => ValidatorIfResolverResult<Context> | Promise<ValidatorIfResolverResult<Context>>;
|
|
1740
|
+
/**
|
|
1741
|
+
* ## Validator If Rule Options
|
|
1742
|
+
*
|
|
1743
|
+
* Options interface for the `validateIfRule` method. Extends standard `ValidatorOptions`
|
|
1744
|
+
* with the resolver function for conditional validation logic.
|
|
1745
|
+
*
|
|
1746
|
+
* ### Purpose
|
|
1747
|
+
* Provides the complete set of options needed to execute conditional validation,
|
|
1748
|
+
* combining standard validation options with the resolver-based conditional logic.
|
|
1749
|
+
*
|
|
1750
|
+
* ### Key Properties
|
|
1751
|
+
* - **resolver**: The function that determines which rules to apply
|
|
1752
|
+
* - **value**: The value to validate
|
|
1753
|
+
* - **data**: Additional form/object data for dependent validation
|
|
1754
|
+
* - **context**: Application-specific context
|
|
1755
|
+
* - **i18n**: Internationalization instance
|
|
1756
|
+
* - **message**: Optional fallback message (used if resolver doesn't provide one)
|
|
1757
|
+
*
|
|
1758
|
+
* ### Examples
|
|
1759
|
+
*
|
|
1760
|
+
* #### Basic usage
|
|
1761
|
+
* ```typescript
|
|
1762
|
+
* const options: ValidatorIfRuleOptions = {
|
|
1763
|
+
* value: 'user@example.com',
|
|
1764
|
+
* resolver: ({ value }) => value ? ['Email'] : [],
|
|
1765
|
+
* fieldName: 'email',
|
|
1766
|
+
* };
|
|
1767
|
+
* ```
|
|
1768
|
+
*
|
|
1769
|
+
* #### With context and data
|
|
1770
|
+
* ```typescript
|
|
1771
|
+
* const options: ValidatorIfRuleOptions<MyContext> = {
|
|
1772
|
+
* value: 'password123',
|
|
1773
|
+
* data: { role: 'admin', username: 'john' },
|
|
1774
|
+
* context: { strictMode: true },
|
|
1775
|
+
* resolver: ({ data, context }) => {
|
|
1776
|
+
* if (context?.strictMode && data?.role === 'admin') {
|
|
1777
|
+
* return {
|
|
1778
|
+
* rules: ['Required', 'StrongPassword'],
|
|
1779
|
+
* message: 'Admin passwords must be strong',
|
|
1780
|
+
* };
|
|
1781
|
+
* }
|
|
1782
|
+
* return ['Required'];
|
|
1783
|
+
* },
|
|
1784
|
+
* fieldName: 'password',
|
|
1785
|
+
* };
|
|
1786
|
+
* ```
|
|
1787
|
+
*
|
|
1788
|
+
* ### Relationship to Other Types
|
|
1789
|
+
* - Extends {@link ValidatorOptions} for standard validation properties
|
|
1790
|
+
* - Extends {@link ValidatorMessageConfig} for fallback custom message support
|
|
1791
|
+
* - Uses {@link ValidatorIfResolver} for conditional logic
|
|
1792
|
+
*
|
|
1793
|
+
* @template Context - Type of the optional validation context
|
|
1794
|
+
*
|
|
1795
|
+
* @public
|
|
1796
|
+
* @see {@link ValidatorIfResolver} - Resolver function type
|
|
1797
|
+
* @see {@link Validator.validateIfRule} - Method that uses these options
|
|
1798
|
+
*/
|
|
1799
|
+
export interface ValidatorIfRuleOptions<Context = unknown> extends Omit<ValidatorOptions<ValidatorRuleParams, Context>, 'rules' | 'ruleParams'>, ValidatorMessageConfig {
|
|
1800
|
+
/**
|
|
1801
|
+
* Resolver function that determines which validation rules to apply.
|
|
1802
|
+
*
|
|
1803
|
+
* This function receives the validation context and returns:
|
|
1804
|
+
* - An array of rules to apply
|
|
1805
|
+
* - An object with `rules` and optional `message`
|
|
1806
|
+
* - Empty array, `undefined`, or `null` to skip validation
|
|
1807
|
+
*
|
|
1808
|
+
* @example
|
|
1809
|
+
* // Simple resolver
|
|
1810
|
+
* resolver: ({ value }) => value ? ['Required', 'Email'] : []
|
|
1811
|
+
*
|
|
1812
|
+
* @example
|
|
1813
|
+
* // Resolver with custom message
|
|
1814
|
+
* resolver: ({ data }) => ({
|
|
1815
|
+
* rules: ['Required'],
|
|
1816
|
+
* message: data?.isNew ? 'This field is required for new users' : undefined,
|
|
1817
|
+
* })
|
|
1818
|
+
*/
|
|
1819
|
+
resolver: ValidatorIfResolver<Context>;
|
|
1820
|
+
/**
|
|
1821
|
+
* Optional start time for performance tracking.
|
|
1822
|
+
*/
|
|
1823
|
+
startTime?: number;
|
|
1824
|
+
}
|
|
1474
1825
|
/**
|
|
1475
1826
|
* ## Validator Class Options
|
|
1476
1827
|
*
|