Introducing ATGValidator for Swift

A simple, easy to use, open-source validation framework written in Swift

Suraj Thomas K
Ounass
7 min readMar 11, 2020

--

When we started working on our first in-house eCommerce apps at Al Tayer Digital, one of the challenges we faced was performing user input validations efficiently and in a maintainable way. Our initial solution for validation was by adding ad-hoc solutions including length/character-set checks and regex string matching. Needless to say, in a few weeks, it became too difficult to track these pieces of code distributed across the project.

We started exploring open source frameworks to find if there is any that will match our requirements. Our main focus was on ease of use, low boilerplate code, and most importantly form validation. The main contenders for selection were SwiftValidator and Validator.

Both frameworks were rule-based but were functioning in different ways. SwiftValidator performed validations based on a Validator instance, and apps need to manually trigger validation by hooking up with UITextField and UITextView delegates. This introduced a lot of boilerplate code, and we wanted to avoid this.

Validator framework addressed this issue by adding targets to UITextField and UITextView in framework, and running validation when specific events occurred. This was looking promising, except one aspect. Validator was using generics extensively, and this made it impossible to club together different validation results. So no form validation was possible with it.

So we wanted to combine the best aspects of both frameworks and create a new framework that is easy to use, with minimal boiler-plate code, and form validation support. And that’s when we wrote ATGValidator.

ATGValidator is rule based validation framework written in a protocol based approach. You get a set of predefined rules, and can define new rules easily. UITextField and UITextView has build in support for validation, and it’s easy to add validation support for custom UI elements. You get a form validator which can group multiple UI elements together and give a unified result for validation. And you can run validations on individual data types like String, Int, Double etc independently without need for a UI element.

Let me show how easy it is to use the framework with an example below. Download the starter project from here.

Unzip the downloaded file, and go to the folder in Terminal, and run

Now open the generated xcworkspace file, and run the app. You can see a basic signup form implemented in the app. Now, let’s add some validation.

Import ATGValidator in ViewController.swift

Create an instance of FormValidator

Now in viewDidLoad, add the following lines of code

Now add a validation handler inside `didTapOnProceedButton` method as shown below;

Now when you tap on the proceed button, the framework validates the input with rules and provides a result object with an array of errors and the result is shown in an alert view.

Just showing if validation failed or succeeded will not be good enough to provide a good user experience. So, let’s add some meaningful error messages to the rules.

ValidationError is a framework level enumeration conforming to Error protocol and is used for identifying default errors. It has an additional custom case with error message to be used for easy error handling.

Rule protocol has 2 helper methods to associate an error object or an error message to itself. The methods being used in above code sample is the one with errorMessage.

And update the form validation handler as follow;

The snippet above goes through all the error objects passed in from the validation result and creates an alert message from it.

This will not be the best experience we can give to the user. Rather than showing it in an alert view, let’s update the experience to give more context to the specified errors.

Let’s move around and clean up the code a bit to make it more readable.

First, create a Constants enumeration inside the Viewcontroller class.

Now move all validation rule setting to a new method.

Create a new method to register validation handlers for individual fields and update the UI to display errors.

Above method has a lot of repeating code for displaying error messages, but I won’t be covering it’s refactor in this article.

Since we will be showing validation errors on individual fields, we don’t need to show the alert on tapping the proceed button. So, let’s update the FormValidator handler closure to update the state of the proceed button based on validation result. Add a new method as shown below;

And remove the unnecessary code in IBAction for the proceed button as shown below;

Now, let’s update the viewDidLoad method to call these methods;

Build and run the app. Voila, now we have a fully functional form validation in place. See it in action below;

You can download the complete sample project from here.

There are many more things under the hood in ATGValidator. And covering them all will not be possible in a single article.

If you want to learn in-depth about the framework head over to the readme section of the project in Github.

To learn more about using ATGValidator to validate credit card numbers, head over to this article.

If you found this article helpful, please don’t forget to clap. Cheers..!

--

--