Advertisement
periclase_software

Swift String Validator

Dec 9th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.77 KB | None | 0 0
  1.  
  2. import Foundation
  3.  
  4. /// Provides an interface to validate that the given input matches a set of rules.
  5. struct TextFieldValidation {
  6.     /// Defines a set of rules that the input can be validated for.
  7.     enum Rule: Hashable {
  8.         /// Rule that the input is not empty.
  9.         case notEmpty
  10.         /// Rule that the input is part of the defined character set.
  11.         case isIn(CharacterSet)
  12.         /// Rule that the input can be converted into a double type.
  13.         case isDouble
  14.         /// Rule that the number of digits doesn't exceed the limit.
  15.         case limit(Int)
  16.         /// Combines `isIn(.decimalDeigits)`, and `isDouble` rules.
  17.         case isDecimal
  18.         /// Rule like `limit()` but with a hard limit of 60, `notEmpty`, and `isIn(.alphanumerics.union(.symbols))`.
  19.         /// Used to validate text inputs.
  20.         case limitText
  21.         /// Rule like `limit()` but with a hard limit of 6, `notEmpty`, `isIn(.decimalDigits)`, and `isDouble` rules.
  22.         /// Used to validate numeric field inputs.
  23.         case limitNumbers
  24.     }
  25.  
  26.     // MARK: - Business
  27.    
  28.     /// Returns true or false if the given input passes all of the rules.
  29.     func validate(_ input: String, against rules: Set<Rule>) -> Bool {
  30.         for rule in rules {
  31.             if validateRule(rule, input: input) == false {
  32.                 return false
  33.             }
  34.         }
  35.         return true
  36.     }
  37.  
  38.     private func validateRule(_ rule: Rule, input: String) -> Bool {
  39.         switch rule {
  40.         case .notEmpty:
  41.             return validateNotEmpty(input)
  42.         case .isIn(let set):
  43.             return validateIsInCharacterSet(set, input: input)
  44.         case .isDouble:
  45.             return validateIsDouble(input)
  46.         case .limit(let limit):
  47.             return validateLimit(limit, input: input)
  48.         case .isDecimal:
  49.             return validate(input, against: [.isIn(.decimalDigits), .isDouble])
  50.         case .limitText:
  51.             return validate(input, against: [.notEmpty, .limit(60), .isIn(.alphanumerics.union(.symbols))])
  52.         case .limitNumbers:
  53.             return validate(input, against: [.notEmpty, .isDouble, .limit(6), .isIn(.decimalDigits)])
  54.         }
  55.     }
  56.    
  57.     private func validateNotEmpty(_ input: String) -> Bool {
  58.         let trimmedInput = input.trimmingCharacters(in: .whitespacesAndNewlines)
  59.         return trimmedInput.isEmpty == false
  60.     }
  61.    
  62.     private func validateIsInCharacterSet(_ set: CharacterSet, input: String) -> Bool {
  63.         return input.rangeOfCharacter(from: set) != nil
  64.     }
  65.    
  66.     private func validateIsDouble(_ input: String) -> Bool {
  67.         return Double(input) != nil
  68.     }
  69.    
  70.     private func validateLimit(_ limit: Int, input: String) -> Bool {
  71.         return input.count <= limit
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement