-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implenent a validation extension for
SwiftUI
views
- Loading branch information
1 parent
8ed55e8
commit 44555a9
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Sources/ValidatorUI/Classes/SUI/Extensions/View+Validation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
File renamed without changes.