Validation.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // RowValidationType.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. public struct ValidationError: Equatable {
  26. public let msg: String
  27. public init(msg: String) {
  28. self.msg = msg
  29. }
  30. }
  31. public func == (lhs: ValidationError, rhs: ValidationError) -> Bool {
  32. return lhs.msg == rhs.msg
  33. }
  34. public protocol BaseRuleType {
  35. var id: String? { get set }
  36. var validationError: ValidationError { get set }
  37. }
  38. public protocol RuleType: BaseRuleType {
  39. associatedtype RowValueType
  40. func isValid(value: RowValueType?) -> ValidationError?
  41. }
  42. public struct ValidationOptions: OptionSet {
  43. public let rawValue: Int
  44. public init(rawValue: Int) {
  45. self.rawValue = rawValue
  46. }
  47. public static let validatesOnDemand = ValidationOptions(rawValue: 1 << 0)
  48. public static let validatesOnChange = ValidationOptions(rawValue: 1 << 1)
  49. public static let validatesOnBlur = ValidationOptions(rawValue: 1 << 2)
  50. public static let validatesOnChangeAfterBlurred = ValidationOptions(rawValue: 1 << 3)
  51. public static let validatesAlways: ValidationOptions = [.validatesOnChange, .validatesOnBlur]
  52. }
  53. public struct ValidationRuleHelper<T> where T: Equatable {
  54. let validateFn: ((T?) -> ValidationError?)
  55. let rule: BaseRuleType
  56. }
  57. public struct RuleSet<T: Equatable> {
  58. internal var rules: [ValidationRuleHelper<T>] = []
  59. public init() {}
  60. /// Add a validation Rule to a Row
  61. /// - Parameter rule: RuleType object typed to the same type of the Row.value
  62. public mutating func add<Rule: RuleType>(rule: Rule) where T == Rule.RowValueType {
  63. let validFn: ((T?) -> ValidationError?) = { (val: T?) in
  64. return rule.isValid(value: val)
  65. }
  66. rules.append(ValidationRuleHelper(validateFn: validFn, rule: rule))
  67. }
  68. public mutating func remove(ruleWithIdentifier identifier: String) {
  69. if let index = rules.firstIndex(where: { (validationRuleHelper) -> Bool in
  70. return validationRuleHelper.rule.id == identifier
  71. }) {
  72. rules.remove(at: index)
  73. }
  74. }
  75. public mutating func removeAllRules() {
  76. rules.removeAll()
  77. }
  78. }