EnterpriseDS Documentation

Protocol DSValidation​Rule

public protocol DSValidationRule  

A validation framework for defining how text entered into a DSTextField can be validated and an appropriate error message provided if that validation fails. Multiple DSValidationRule objects can stack to perform multiple validations including the special DSRequiredTextValidationRule which is the only built-in rule validates empty fields.

Validation rules are added to DSTextField as follows:

let textField = DSTextField()
textField.add(validationRule: DSMaxTextLengthValidationRule(maxTextLength: 50, message: "Text too long."))

All validation rules, and any you create, should accept empty text as valid. This is so new forms don't show errors on all required fields right away. Once a form element is interacted with (didBeginEditing), you can then add a DSRequiredTextValidationRule so that if the field data is cleared it will display an error properly. Be careful to only add this rule once although there will be no issues if you do duplicate them. You can also call removeAllValidationRules if you want to reset everything.

DSValidationRule DSValidationRule DSMaxTextLengthValidationRule DSMaxTextLengthValidationRule DSMaxTextLengthValidationRule->DSValidationRule DSAdHocValidationRule DSAdHocValidationRule DSAdHocValidationRule->DSValidationRule DSCreditCardValidationRule DSCreditCardValidationRule DSCreditCardValidationRule->DSValidationRule DSCurrencyValidationRule DSCurrencyValidationRule DSCurrencyValidationRule->DSValidationRule DSEmailAddressValidationRule DSEmailAddressValidationRule DSEmailAddressValidationRule->DSValidationRule DSMinMaxValidationRule DSMinMaxValidationRule DSMinMaxValidationRule->DSValidationRule DSNumberValidationRule DSNumberValidationRule DSNumberValidationRule->DSValidationRule DSPhoneNumberValidationRule DSPhoneNumberValidationRule DSPhoneNumberValidationRule->DSValidationRule DSRequiredTextValidationRule DSRequiredTextValidationRule DSRequiredTextValidationRule->DSValidationRule

Types Conforming to DSValidation​Rule


A validation rule that allows for an error message to be expressly set. When the message is non-empty, the validation fails. This validation rule is useful for showing backend errors.


A validation rule for requiring that a credit card number matches a legal format as much as possible.


A validation rule for requiring a valid currency is entered.


A validation rule for email addresses. Returns an error when a proper email address has not been formed. Does not verify that an email address exists, only that it's legal.


A validation rule for capping the length of the text to a maximum amount.


A validation rule for requiring that text is a number and is between a min and max value. (Inclusive.)


A validation rule for requiring that only digits are entered.


A validation rule for requiring text to be a phone number of format XXX-XXX-XXXX.


A validation rule for requiring that text exists and is not an empty string. This is the only validation rule that enforces it and you should add it to your other validations to check for empty states. Other validations will only fire if data exists.



var errorMessage: String  

The error message to display if invalid.


func isValid(_ text: String) -> Bool

Returns true if the text is validated by this rule.