Skip to content

Commit f468c0f

Browse files
committed
Implement a validation SwiftUI extension
1 parent 44555a9 commit f468c0f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@ import ValidatorCore
1111
private let validator = Validator()
1212

1313
public extension View {
14+
/// Validate a binding item using a set of validation rules and perform an action based on
15+
/// the validation result.
16+
///
17+
/// This function takes a binding item, a validation rule, and a closure to handle
18+
/// the validation result.
19+
/// It validates the wrapped value of the binding item against the specified rule and then
20+
/// invokes the provided action with the result.
21+
///
22+
/// - Parameters:
23+
/// - item: The binding item to validate.
24+
/// - rule: A validation rule to apply to the item's value.
25+
/// - action: A closure that takes a `ValidationResult` as its parameter.
26+
/// This closure is called with the validation result after the validation is performed.
27+
///
28+
/// - Returns: A view that can be modified further or used in your SwiftUI hierarchy.
29+
func validation<T>(
30+
_ item: Binding<T>,
31+
rule: some IValidationRule<T>,
32+
action: @escaping (ValidationResult) -> Void
33+
) -> some View {
34+
validation(item, rules: [rule], action: action)
35+
}
36+
1437
/// Validate a binding item using a set of validation rules and perform an action based on
1538
/// the validation result.
1639
///

0 commit comments

Comments
 (0)