-
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.
- Remove `AnyValidationRule` - Get rid of support for older operating systems
- Loading branch information
1 parent
38287fa
commit bb9a3ba
Showing
15 changed files
with
234 additions
and
122 deletions.
There are no files selected for viewing
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
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
12 changes: 0 additions & 12 deletions
12
Sources/ValidatorCore/Classes/Extensions/IValidationRule+Erase.swift
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
Sources/ValidatorCore/Classes/Rules/AnyValidationRule.swift
This file was deleted.
Oops, something went wrong.
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
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
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
29 changes: 29 additions & 0 deletions
29
Sources/ValidatorUI/Classes/Extensions/UITextField+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,29 @@ | ||
// | ||
// Validator | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
#if os(iOS) | ||
import UIKit | ||
|
||
extension UITextField: IUIValidatable { | ||
public var inputValue: String { text ?? "" } | ||
|
||
public typealias Input = String | ||
|
||
public func validateOnInputChange(isEnabled: Bool) { | ||
if isEnabled { | ||
addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged) | ||
} else { | ||
removeTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged) | ||
} | ||
} | ||
|
||
// MARK: Private | ||
|
||
@objc | ||
private func textFieldDidChange(_: UITextField) { | ||
validate(rules: validationRules) | ||
} | ||
} | ||
#endif |
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,89 @@ | ||
// | ||
// Validator | ||
// Copyright © 2023 Space Code. All rights reserved. | ||
// | ||
|
||
// swiftlint:disable prefixed_toplevel_constant | ||
|
||
import Foundation | ||
import ValidatorCore | ||
|
||
// MARK: - IUIValidatable | ||
|
||
public protocol IUIValidatable: AnyObject { | ||
associatedtype Input | ||
|
||
/// The input value. | ||
var inputValue: Input { get } | ||
|
||
/// Validates an input value. | ||
/// | ||
/// - Parameters: | ||
/// - rule: The validation rule. | ||
/// | ||
/// - Returns: A validation result. | ||
func validate<T>(rule: some IValidationRule<T>) -> ValidationResult where T == Input | ||
|
||
/// Validates an input value. | ||
/// | ||
/// - Parameters: | ||
/// - rules: The validation rules array. | ||
/// | ||
/// - Returns: A validation result. | ||
func validate<T>(rules: [any IValidationRule<T>]) -> ValidationResult where T == Input | ||
|
||
/// Validates an input value. | ||
/// | ||
/// - Parameter isEnabled: The | ||
func validateOnInputChange(isEnabled: Bool) | ||
} | ||
|
||
private var kValidationRules: UInt8 = 0 | ||
private var kValidationHandler: UInt8 = 0 | ||
|
||
private let validator = Validator() | ||
|
||
public extension IUIValidatable { | ||
@discardableResult | ||
func validate<T>(rule: some IValidationRule<T>) -> ValidationResult where T == Input { | ||
let result = validator.validate(input: inputValue, rule: rule) | ||
validationHandler?(result) | ||
return result | ||
} | ||
|
||
@discardableResult | ||
func validate<T>(rules: [any IValidationRule<T>]) -> ValidationResult where T == Input { | ||
let result = validator.validate(input: inputValue, rules: rules) | ||
validationHandler?(result) | ||
return result | ||
} | ||
|
||
func add<T>(rule: some IValidationRule<T>) where T == Input { | ||
validationRules.append(rule) | ||
} | ||
|
||
var validationRules: [any IValidationRule<Input>] { | ||
get { | ||
(objc_getAssociatedObject(self, &kValidationRules) as? AnyObject) as? [any IValidationRule<Input>] ?? [] | ||
} | ||
set { | ||
objc_setAssociatedObject( | ||
self, | ||
&kValidationRules, | ||
newValue as [any IValidationRule<Input>], | ||
.OBJC_ASSOCIATION_RETAIN_NONATOMIC | ||
) | ||
} | ||
} | ||
|
||
var validationHandler: ((ValidationResult) -> Void)? { | ||
get { | ||
objc_getAssociatedObject(self, &kValidationHandler) as? ((ValidationResult) -> Void) | ||
} | ||
set { | ||
if let newValue = newValue { | ||
objc_setAssociatedObject(self, &kValidationHandler, newValue as AnyObject, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.