vds_ios/VDS/Components/TextFields/Rules/CharacterCountRule.swift
Matt Bruce 978db64823 updated rules
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2024-05-08 15:59:43 -05:00

35 lines
884 B
Swift

//
// 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
}
}
}