Simple Swift Validator
Allows you to check if some String
compare to ValidatorRule
you created by adding validated(with: ValidatorRule)
extension to String
.
Rules type:
ValidatorRuleLength(rule: .maximumLength, value: 10)
- word shorter than 10ValidatorRuleLength(rule: .minimumLength, value: 5)
- word longer than 5ValidatorRulePattern(pattern: .email)
- emailValidatorRulePattern(pattern: .phoneNumber)
- phone numberValidatorRulePattern(dynamicString: {return dynamicWord})
- is equal to dynamic word
Usage example
let word = "word"
var dynamicWord = "dynamic"
let dynamicRule = ValidatorRulePattern(dynamicString: {return dynamicWord})
dynamicRule.set(errorMessage: "This two words are not equal")
let result = word.validated(with: dynamicRule)
switch result {
case .invalid(let error):
print(error)
case .valid:
print("valid")
}