Skip to content

Commit

Permalink
Implenent a validation extension for SwiftUI views
Browse files Browse the repository at this point in the history
  • Loading branch information
ns-vasilev committed Sep 23, 2023
1 parent 8ed55e8 commit 44555a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift
Original file line number Diff line number Diff line change
@@ -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<T>(
_ item: Binding<T>,
rules: [any IValidationRule<T>],
action: @escaping (ValidationResult) -> Void
) -> some View {
let result = validator.validate(input: item.wrappedValue, rules: rules)
action(result)
return self
}
}

0 comments on commit 44555a9

Please sign in to comment.