// // CharacterCountRule.swift // VDS // // Created by Matt Bruce on 4/30/24. // import Foundation class CharacterCountRule: Rule, Withable { enum CompareType { case equals, greaterThanEquals, lessThan, lessThanEquals } var maxLength: Int? var errorMessage: String = "You have exceeded the character limit." var compareType: CompareType = .lessThanEquals func isValid(value: String?) -> Bool { guard let text = value, let maxLength, maxLength > 0 else { return true } switch compareType { case .equals: return text.count == maxLength case .greaterThanEquals: return text.count >= maxLength case .lessThan: return text.count < maxLength case .lessThanEquals: return text.count <= maxLength } } }