Skip to content

Commit ca65096

Browse files
committed
Merge pull request #40 from KVTaniguchi/refactorValidationRule
apply rules with map and filter
2 parents dc9224c + b959029 commit ca65096

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

Validator/ValidationRule.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ public class ValidationRule {
2121
}
2222

2323
public func validateField() -> ValidationError? {
24-
for rule in rules {
25-
if !rule.validate(textField.text) {
26-
return ValidationError(textField: self.textField, errorLabel:self.errorLabel, error: rule.errorMessage())
27-
}
28-
}
29-
return nil
24+
return rules.filter{ !$0.validate(self.textField.text) }
25+
.map{ rule -> ValidationError in return ValidationError(textField: self.textField, errorLabel:self.errorLabel, error: rule.errorMessage()) }.first
3026
}
3127
}

0 commit comments

Comments
 (0)