Introducing: Open Source NGRValidator

Photo of Patryk Kaczmarek

Patryk Kaczmarek

Updated Mar 7, 2023 • 5 min read

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!

Validation has never been this 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!

What does NGRValidator solve?

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?

Diving in with NGRValidator

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:

  • sign in
  • sign up

Here are some assumptions:

  • to sign in user must provide email and password
  • to sign up user must provide email, first name, last name, password, and repeated password
  • password should have at least 5 signs
  • password and repeated password should be the same
  • email must adhere to email syntax
  • first name and last name must have min 5 alphabetic signs

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.

Tell me what I did wrong

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:

  • using method chaining (ideal for fast development):
  • using a delegate (perfect for localization):

Both will have the same output:

If you want to know more, please read the robust documentation on GitHub.

Soon

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:

  • collection and assets validation rules
  • carthage support
  • conditional validators

Join in!

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.

Photo of Patryk Kaczmarek

More posts by this author

Patryk Kaczmarek

Graduated from the Poznan University of Technology, he started as a technology constructor and...
Lost with AI?  Get the most important news weekly, straight to your inbox, curated by our CEO  Subscribe to AI'm Informed

Read more on our Blog

Check out the knowledge base collected and distilled by experienced professionals.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business