@@ -26,6 +26,12 @@ public protocol IUIValidatable: AnyObject {
2626 /// The input value that needs to be validated.
2727 var inputValue : Input { get }
2828
29+ /// The most recent validation result, if any.
30+ ///
31+ /// This value is updated automatically when calling `validate(rules:)`.
32+ /// Implementations may use this property to drive UI updates.
33+ var validationResult : ValidationResult ? { get }
34+
2935 /// Validates the input value using a single rule.
3036 ///
3137 /// - Parameter rule: A validation rule conforming to `IValidationRule`.
@@ -48,9 +54,10 @@ public protocol IUIValidatable: AnyObject {
4854
4955// MARK: - Associated Object Keys
5056
51- // Keys used for storing associated objects (validation rules and handlers )
57+ // Keys used for storing associated objects (validation rules, handlers, and a validation result )
5258private nonisolated ( unsafe) var kValidationRules: UInt8 = 0
5359private nonisolated ( unsafe) var kValidationHandler: UInt8 = 0
60+ private nonisolated ( unsafe) var kValidationResult: UInt8 = 0
5461
5562// Validator instance shared for UI validation
5663// swiftlint:disable:next prefixed_toplevel_constant
@@ -78,6 +85,7 @@ public extension IUIValidatable {
7885 func validate( rules: [ any IValidationRule < Input > ] ) -> ValidationResult {
7986 let result = validator. validate ( input: inputValue, rules: rules)
8087 validationHandler ? ( result)
88+ validationResult = result
8189 return result
8290 }
8391
@@ -88,6 +96,24 @@ public extension IUIValidatable {
8896 validationRules. append ( rule)
8997 }
9098
99+ /// The most recent validation result, if any.
100+ ///
101+ /// This value is updated automatically when calling `validate(rules:)`.
102+ /// Implementations may use this property to drive UI updates.
103+ private( set) var validationResult : ValidationResult ? {
104+ get {
105+ ( objc_getAssociatedObject ( self , & kValidationResult) as? AnyObject ) as? ValidationResult
106+ }
107+ set {
108+ objc_setAssociatedObject (
109+ self ,
110+ & kValidationResult,
111+ newValue as ValidationResult ? ,
112+ . OBJC_ASSOCIATION_RETAIN_NONATOMIC
113+ )
114+ }
115+ }
116+
91117 /// The array of validation rules associated with this UI element.
92118 var validationRules : [ any IValidationRule < Input > ] {
93119 get {
0 commit comments