Read about our latest open source project: an easy to read, centralized, and comprehensive solution to validating any Objective-C model in just a few lines of code. Validation has never been that easy!
NGRValidator is an Objective-C 3rd party library for iOS. It allows you to validate the data in the way that you want. It's an easy to read, centralized, and comprehensive solution to validating any Objective-C model in just a few lines of code.
Imagine a world where everything is true. Horoscopes speak the truth, politicians speak the truth, even every single source of truth contains the genuine truth. Imagine application development for which every request succeeds, there are neither errors nor the need for exception handling. A beautiful world, isn’t it? Now try to calm down and take a deep breath. This world doesn’t exist, unfortunately...
Reality forces us to immediately validate any single piece of data which enters the app from outside. This extends development time and leaves me scratching my head if something goes wrong. Are you familiar with this? If so, NGRValidator is for you!
NGRValidator is built on top of method chaining. The power behind the chaining eliminates the need for creating intermediate local variables which obfuscates code readability and gives nothing in return. That's why, in only a few lines of code, NGRValidator is capable of validating quite complex models. But let's start with something simple.
Typically, every project which handles user accounts requires email validation. Let's take a look at how this is done:
Too much code? There are better ways to achieve this? And, what if you want to validate passwords as well? You can use a similar method to the one above. Nevertheless, in order to keep the MVC pattern, it would be much better to create a simple model with validation functionality:
Looks quite interesting, doesn’t it?
NGRValidator can do much more than this. Let's imagine a single model used for many actions across an application's life. A good example of this would be the user model which is used for:
Here are some assumptions:
Have you already imagined the volume of conditional statements? Surprise! You’re not going to see any of them!
That's it! To the user model above, you can add more properties and scenarios (e.g., change password, forgot password) and feel how powerful and flexible the validator is.
By default, NGRValidator contains simple messages indicating the reason for validation failure. However, it is possible to specify your own messages. There are two ways to achieve this:
Both will have the same output:
If you want to know more, please read the robust documentation on GitHub.
Due to a lack of similar solutions available in the open source space, NGRValidator is the apple of my eye. Although I'm dealing with new functionalities as they arise, my to-do list is still growing. Please stay tuned for:
And finally, feel free to contribute, comment, ask, and share your ideas. Let us know what you think of it!
We’re working on some more open source projects here - help yourself to what we’ve got on offer, or join us.
We'd love to meet you on Twitter too - follow @netguru for regular updates about Ruby on Rails, iOS, open source and more.