RevoValidation provides a comprehensive set of validation rules that can be used individually or combined using the pipe (|
) separator.
Example Usage
Using String Literal Syntax:
// Combine multiple rules using pipe separator
let rules: Rules = "required|email|length:5"
// Validate a string
let result = rules.validate("user@example.com")
// Check if valid
if result.count == 0 {
print("Validation passed!")
} else {
print("Errors: \(result.errorMessage)")
}
Using Rule Classes Programmatically:
// Create a rule instance
let requiredRule = RuleRequired()
// Validate a string
let isValid = requiredRule.isValid("some text")
if isValid {
print("Validation passed!")
} else {
print("Error: \(requiredRule.errorMessage)")
}
// Combine multiple rules
let rules = Rules([
RuleRequired(),
RuleEmail(),
RuleLenght(6)
])
let validationResult = rules.validate("user@example.com")
Basic Rules
required
Ensures the field is not empty.
email
Validates that the value is a valid email address format.
url
Validates that the value is a valid URL format.
numeric
Validates that the value contains only numeric characters.
Length and Format Rules
length:6
Validates that the value has a minimum length. Replace 6
with your desired minimum length.
regexp:pattern
Validates that the value matches a custom regular expression pattern.
Character Rules
containsNumber
Validates that the value contains at least one numeric character.
containsSpecialChars
Validates that the value contains at least one special character (non-alphanumeric).
Date and Age Rules
age:18
Validates that the date value represents an age of at least the specified years. Replace 18
with your desired minimum age.
Uniqueness Rules
unique:value1,value2
Validates that the value is not in the provided comma-separated list of existing values.
National Identification Rules
nif:ES
Validates a National Identification Number (NIF) for the specified country. Supported countries: ES
(Spain), PT
(Portugal), DO
(Dominican Republic).
Combining Rules
required|email|length:5
Combine multiple rules using the pipe (|
) separator. All rules must pass for validation to succeed.
required+email
Use the plus (+
) separator to create combined rules that are evaluated together.