Skip to content
Kareem Gan edited this page Jan 16, 2016 · 5 revisions

Welcome to the PoroValidator Wiki where you'll find lots of additional information about PoroValidator and answers to the most frequently asked questions.

Author's POV

I always believed an object's validation is a separate concern and not have that functionality be embedded in the object which the validity is in question. (ActiveRecord objects validates itself.)

The library I created is framework agnostic and can be used on any plain old ruby objects (as long as the object responds to the attribute that requires validation it will validate it, this includes Hash Objects).

Here's an example scenario:

When working with Ruby on Rails because of ActiveRecord's integration we only validate an object after it's ActiveRecord object is loaded. But with this library it gives you the ability to validate an object at the boundary level of your application (when an incoming payload comes through your API - think of the parameters that come in when a request is made). Instead of initializing an full ActiveRecord object just to validate the object, you are have now the option of validating the incoming parameters directly!

How you handle the state of that validation (whether it's valid or not) is up to you (e.g, return the object so the user's form is still filled and not blank).

ActiveRecord/ActiveModel::Validations

ActiveModel::Validations is great for simple validation logic. However, as your application grows you want to have more complex validations (think of normalized tables where your application has numerous join tables) when processing the incoming parameters which are most likely going to be nested we would need a validator that can handle nested objects fast and return the associated errors for the invalid attributes properly. Another scenario is throughout the life cycle of an object we would also want different validations like if the validation is dependent on the state of an object. So we need a validator that's more flexible, enter PoroValidator.

Another issue with ActiveModel::Validations is that it hooks pretty deep into ActiveRecord. The main use case for ActiveModel::Validations within the ActiveRecord object is to prevent bad data hitting your database - which isn't always the case (sometimes we want to allow bad data to go through). PoroValidator decouples your validation logic from your object structure. With PoroValidator you can define different validation rules for different contexts. So instead of the object validating itself by definining the validation within the object, we create separate validation classes that gives us more flexibility while adhering the the Single Responsibility Principle.

Clone this wiki locally