📜 ⬆️ ⬇️

Framework for validating data in iOS applications

“In our company, we pay special attention to the quality of the product we produce,” is a pattern phrase that can often be seen on the websites of companies offering various services, and not only in the field of IT. However, the way the quality of a software product is achieved is hidden from users and only worries them when something went wrong. One of the qualitative indicators of the application is how it responds to non-standard user actions. For most client-server solutions, these actions are related to data entry. Next, we describe which data validation solutions are used among iOS developers in our team.

In a mobile application, a typical data validation problem often arises. The consequences of an error in this area of ​​the application may be noncritical, and may result in a slowdown of the entire system in the event that no restrictions were placed on the transmitted data. Do not forget about the specifics of mobile devices: if an application is used as a mobile client to display remote service data, the amount of this data should be limited to avoid long loading.

First, select the basic data that should be subject to verification. The first and most common type of verification is verification of the entered text data. Numbers, as a rule, are also displayed in text fields and will be a special case of a text representation. The second type of verification must be associated with the date entry. Often, an application needs to check whether the entered date is earlier than the current one. The third type that should be validated is raw data, or, more simply, the number of bytes with which the application works without a significant loss in performance.
')
All of the above items and methods for solving the tasks set in them were collected by us in a small framework, called by analogy with other standard iOS packages, - ValidationKit . In order to more clearly understand what this framework includes, its contents can be represented in the form of the following diagram:



Different colors indicate logically separate parts of the framework, aimed at solving a specific problem from the list of data to be checked. Solid lines indicate inheritance, dotted lines indicate the implementation of the protocol.
So, from the diagram you can see that the basic protocol for all classes of validators is <VLDValidator> . The protocol derived from it <VLDTextValidator> implements validators that work directly with text data. We briefly describe what each of the implemented validators represents:

VLDDateValidator - used for date validation. By default, the date passed to the validator should not be earlier than the current one.

VLDRegExpValidator - used as a base class for validators based on a regular expression. To implement a descendant validator of this class, you must override the static method + (NSString *) regExpPattern , which returns a string-rule.

VLDEmailValidator , VLDStrongPasswordValidator , VLDPhoneNumberValidator are regular expression-based validators that are used respectively for checking email addresses, password protection and telephone numbers.

VLDBoundaryNumberValidator - used to validate numbers within specified boundaries. Indicates the lower and upper bound of the falling under the check number. For example, the year cannot be earlier than 1900 and later than 2015.

VLDBoundaryCharactersValidator - used to validate the number of characters. Indicates the lower and upper limit of the number of characters. For example, a phone can be at least 4 and no more than 11 digits.

It is worth noting that in a large number of projects there is a limit on the length of processed lines. In order to solve this problem, without overloading the code by processing the delegates <UITextFieldDelegate> and <UITextViewDelegate> methods , the classes VLDTextField and VLDTextView were created, which do not allow entering more characters than required in the field. You can also set custom validators for these classes that implement the <VLDTextValidator> protocol. For VLDTextField, it is also possible at the initialization stage to set one of the default validation types.

The task of the NSString + VLDValidation and NSData + VLDValidation categories is to check the length of data used implicitly for the user in the application. The most common situation where this may be required is to check the size of the data before transmitting over the network to the remote service. If, for example, a picture weighs more than 10 MB, then it will take a significant amount of time in a slow Internet environment. Therefore, before sending a request to the server, you can make sure that all rows and data objects do not exceed the specified values.

Also, the default framework uses logging to the console, which can be disabled using the macro described in the VLDHelpers file.

In conclusion, it is worth mentioning again the tasks for the solution of which the developed framework was created:

  1. Restriction of characters entered into fields for editing.
  2. Validation of email, password and phone number.
  3. Creating custom validators and using them together with VLDTextField and VLDTextView .
  4. Date validation.
  5. Check the size of the data sent or saved.

PS The source code of the project can be downloaded here .

Source: https://habr.com/ru/post/264881/


All Articles