diff --git a/Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift b/Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift new file mode 100644 index 0000000..8d5b2b4 --- /dev/null +++ b/Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift @@ -0,0 +1,38 @@ +// +// Validator +// Copyright © 2023 Space Code. All rights reserved. +// + +// swiftlint:disable prefixed_toplevel_constant + +import SwiftUI +import ValidatorCore + +private let validator = Validator() + +public extension View { + /// Validate a binding item using a set of validation rules and perform an action based on + /// the validation result. + /// + /// This function takes a binding item, an array of validation rules, and a closure to handle + /// the validation result. + /// It validates the wrapped value of the binding item against the specified rules and then + /// invokes the provided action with the result. + /// + /// - Parameters: + /// - item: The binding item to validate. + /// - rules: An array of validation rules to apply to the item's value. + /// - action: A closure that takes a `ValidationResult` as its parameter. + /// This closure is called with the validation result after the validation is performed. + /// + /// - Returns: A view that can be modified further or used in your SwiftUI hierarchy. + func validation( + _ item: Binding, + rules: [any IValidationRule], + action: @escaping (ValidationResult) -> Void + ) -> some View { + let result = validator.validate(input: item.wrappedValue, rules: rules) + action(result) + return self + } +} diff --git a/Sources/ValidatorUI/Classes/Extensions/UITextField+Validation.swift b/Sources/ValidatorUI/Classes/UIKit/Extensions/UITextField+Validation.swift similarity index 100% rename from Sources/ValidatorUI/Classes/Extensions/UITextField+Validation.swift rename to Sources/ValidatorUI/Classes/UIKit/Extensions/UITextField+Validation.swift