Skip to content

Commit 44555a9

Browse files
committed
Implenent a validation extension for SwiftUI views
1 parent 8ed55e8 commit 44555a9

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Validator
3+
// Copyright © 2023 Space Code. All rights reserved.
4+
//
5+
6+
// swiftlint:disable prefixed_toplevel_constant
7+
8+
import SwiftUI
9+
import ValidatorCore
10+
11+
private let validator = Validator()
12+
13+
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, an array of validation rules, and a closure to handle
18+
/// the validation result.
19+
/// It validates the wrapped value of the binding item against the specified rules and then
20+
/// invokes the provided action with the result.
21+
///
22+
/// - Parameters:
23+
/// - item: The binding item to validate.
24+
/// - rules: An array of validation rules 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+
rules: [any IValidationRule<T>],
32+
action: @escaping (ValidationResult) -> Void
33+
) -> some View {
34+
let result = validator.validate(input: item.wrappedValue, rules: rules)
35+
action(result)
36+
return self
37+
}
38+
}

Sources/ValidatorUI/Classes/Extensions/UITextField+Validation.swift renamed to Sources/ValidatorUI/Classes/UIKit/Extensions/UITextField+Validation.swift

File renamed without changes.

0 commit comments

Comments
 (0)