comments and formatting

This commit is contained in:
Kevin G Christiano 2020-04-10 14:29:40 -04:00
parent bb802af681
commit 15911265a7
6 changed files with 69 additions and 13 deletions

View File

@ -74,13 +74,13 @@ import MVMCore
/// Validates all rule groups. Returns if valid
public func validate() -> Bool {
var valid = true
guard let formRules = formRules else {
return valid
}
guard let formRules = formRules else { return valid }
for group in formRules {
let groupValid = validateGroup(group)
valid = valid && groupValid
}
return valid
}
@ -138,13 +138,16 @@ import MVMCore
// TODO: Temporary hacks, rewrite architecture to support this.
public extension FormValidator {
func getGroupName(forPageType pageType: String?) -> String? {
for actionItem in groupWatchers {
if let buttonModel = actionItem as? ButtonModel,
pageType == (buttonModel.action as? ActionOpenPageModel)?.pageType {
return buttonModel.groupName
}
}
return nil
}
}

View File

@ -11,19 +11,35 @@ import Foundation
open class FormGroupRule: Codable {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
var groupName: String
var rules: [RulesProtocol]
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
init(_ groupName: String, _ rules: [RulesProtocol]) {
self.groupName = groupName
self.rules = rules
}
//--------------------------------------------------
// MARK: - Keys
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case groupName
case rules
}
//--------------------------------------------------
// MARK: - Codec
//--------------------------------------------------
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
self.groupName = try typeContainer.decode(String.self, forKey: .groupName)

View File

@ -9,11 +9,18 @@
import Foundation
public class RuleAllValueChangedModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "allValueChanged"
public var type: String = RuleAllValueChangedModel.identifier
public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool {
return formField.baseValue != formField.formFieldValue()
}

View File

@ -10,31 +10,41 @@ import Foundation
public class RuleEqualsModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "equals"
public var type: String = RuleEqualsModel.identifier
public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool {
return false
}
public func isValid(_ fieldMolecules: [String: FormFieldProtocol]) -> Bool {
var valid = true
var compareValue: AnyHashable?
for formKey in fields {
guard let formField = fieldMolecules[formKey] else {
continue
}
guard let formField = fieldMolecules[formKey] else { continue }
if compareValue == nil {
compareValue = formField.formFieldValue()
continue
}
if compareValue != formField.formFieldValue(){
if compareValue != formField.formFieldValue() {
valid = false
break
}
}
return valid
}
}

View File

@ -8,16 +8,27 @@
import Foundation
public class RuleRegexModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "regex"
public var type: String = RuleRegexModel.identifier
public var fields: [String]
public var regex: String
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool {
if let stringToValidate = formField.formFieldValue() as? String {
return MVMCoreUIUtility.validate(stringToValidate, withRegularExpression: regex)
}
return false
}
}

View File

@ -10,21 +10,30 @@ import Foundation
public class RuleRequiredModel: RulesProtocol {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public static var identifier: String = "allRequired"
public var type: String = RuleRequiredModel.identifier
public var fields: [String]
//--------------------------------------------------
// MARK: - Validation
//--------------------------------------------------
public func isValid(_ formField: FormFieldProtocol) -> Bool {
guard let value = formField.formFieldValue() else {
return false
}
guard let value = formField.formFieldValue() else { return false }
var valid = true
if let valueString = value as? String {
valid = valueString.count > 0
} else if let valueBool = value as? Bool {
valid = valueBool
}
return valid
}
}