rules error handling

This commit is contained in:
Suresh, Kamlesh 2020-05-06 12:16:33 -04:00
parent 89c6204003
commit 33c1c3f70e
8 changed files with 35 additions and 19 deletions

View File

@ -67,7 +67,10 @@ import Foundation
} }
public func setValidity(_ valid: Bool, rule: RulesProtocol) { public func setValidity(_ valid: Bool, rule: RulesProtocol) {
self.isValid = valid if let fieldKey = fieldKey {
self.errorMessage = rule.errorMessage?[fieldKey]
self.isValid = valid
}
} }
//-------------------------------------------------- //--------------------------------------------------

View File

@ -15,6 +15,7 @@ public class RuleAllValueChangedModel: RulesProtocol {
public static var identifier: String = "allValueChanged" public static var identifier: String = "allValueChanged"
public var type: String = RuleAllValueChangedModel.identifier public var type: String = RuleAllValueChangedModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,6 +17,7 @@ public class RuleAnyRequiredModel: RulesProtocol {
public static var identifier: String = "anyRequired" public static var identifier: String = "anyRequired"
public var type: String = RuleRequiredModel.identifier public var type: String = RuleRequiredModel.identifier
public var fields: [String] public var fields: [String]
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Methods // MARK: - Methods

View File

@ -16,6 +16,7 @@ public class RuleAnyValueChangedModel: RulesProtocol {
public static var identifier: String = "anyValueChanged" public static var identifier: String = "anyValueChanged"
public var type: String = RuleAnyValueChangedModel.identifier public var type: String = RuleAnyValueChangedModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -17,6 +17,7 @@ public class RuleEqualsModel: RulesProtocol {
public static var identifier: String = "equals" public static var identifier: String = "equals"
public var type: String = RuleEqualsModel.identifier public var type: String = RuleEqualsModel.identifier
public var fields: [String] public var fields: [String]
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Validation // MARK: - Validation
@ -27,23 +28,24 @@ public class RuleEqualsModel: RulesProtocol {
} }
public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool { public func validate(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
var valid = true var valid = true
var compareValue: AnyHashable? var compareValue: AnyHashable?
for formKey in fields {
guard let formField = fieldMolecules[formKey] else { continue }
if compareValue == nil { for formKey in fields {
compareValue = formField.formFieldValue() guard let formField = fieldMolecules[formKey] else { continue }
continue
} if compareValue == nil {
compareValue = formField.formFieldValue()
if compareValue != formField.formFieldValue() { continue
valid = false }
break
} if compareValue != formField.formFieldValue() {
} valid = false
(formField as? FormRuleWatcherFieldProtocol)?.setValidity(valid, rule: self)
return valid break
}
}
return valid
} }
} }

View File

@ -18,6 +18,7 @@ public class RuleRegexModel: RulesProtocol {
public var type: String = RuleRegexModel.identifier public var type: String = RuleRegexModel.identifier
public var fields: [String] public var fields: [String]
public var regex: String public var regex: String
public var errorMessage: [String: String]?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties

View File

@ -16,6 +16,7 @@ public class RuleRequiredModel: RulesProtocol {
public static var identifier: String = "allRequired" public static var identifier: String = "allRequired"
public var type: String = RuleRequiredModel.identifier public var type: String = RuleRequiredModel.identifier
public var errorMessage: [String: String]?
public var fields: [String] public var fields: [String]
//-------------------------------------------------- //--------------------------------------------------

View File

@ -16,7 +16,9 @@ public enum RulesCodingKey: String, CodingKey {
public protocol RulesProtocol: ModelProtocol { public protocol RulesProtocol: ModelProtocol {
// The type of rule // The type of rule
var type: String { get } var type: String { get }
var errorMessage: [String: String]? { get }
// The fields that this rule applies to. // The fields that this rule applies to.
var fields: [String] { get set } var fields: [String] { get set }
@ -33,6 +35,10 @@ public extension RulesProtocol {
get { return Self.identifier } get { return Self.identifier }
} }
// var errorMessage: String? {
// return nil
// }
static var categoryCodingKey: String { static var categoryCodingKey: String {
return "type" return "type"
} }