This gem and its validations have been implemented on top of the Notification Pattern
, which allows API to return all the validations at a single time, improving the user experience.
Validations of the day day for your rails project.
With this gem you will have several validations that are common on day day and that will make life much easier for dev. Validations can be returned in a simple and standardized way.
Add this line to your application's Gemfile:
gem 'validation_contract'
And then execute:
bundle
Or install it yourself as:
gem install validation_contract
require 'validation_contract'
validation_contract = ValidationContract::Validations.new
Verifying an invalid email, the return will be an array with message and its validation inside, this will occur for all the desired validations.
validation_contract.is_email('teste.com', 'This email is invalid') => "{message: 'This email is invalid'}"
By checking a valid email, nothing will be added in the return, leaving only the validations that are with incorrect values.
validation_contract.is_email('teste@teste.com', 'This email is invalid') => "nil"
Suggestion for the validations. Perform all the necessary validations and then before executing the desired process use the following:
if !validation_contract.is_valid
return validation_contract.errros
end
This will check if there is any invalid field, if there is an arry returned with all the validations. This type of validation is very good for APIs that are being used for a form with many fields, so you return to the FRONT all errors at one time.
Validation | Example | Done |
---|---|---|
required | validate if field is required | V |
has_min_len | check if the field has a minimum of characters | V |
has_max_len | checks if the field has a maximum of characters | V |
fixed_len | checks if there is an x quantity of | V |
check if email is valid | V | |
greater_than | valid if one number is greater than another | V |
lower_than | valid if one number is smaller than another | V |
errors | Return errors | V |
clear | Clears the arry error | V |
valid | Check whether this arry is valid or not | V |
url | check if url is valid | V |
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Added some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
ValidationContract is released under the MIT License.