IsHashValid
A simple hash validator that allows you to do complex validations with custom error messages.
Installation
Add this line to your application's Gemfile:
gem 'is_hash_valid'
And then execute:
$ bundle
Or install it yourself as:
$ gem install is_hash_valid
Usage
Roll your own validator
class SnobbyValidator < IsHashValid::Validator
def initialize(hash)
super hash {
name: {
be_royal: "Must be of royalty"
},
age: {
over18: "No minors allowed"
}
}
end
def over18(value)
value > 18
end
def be_royal(value)
value.scan(/King|Queen/).empty? ? false : true
end
end
valid = SnobbyValidator.new({ name: 'King Richard', age: 9001 })
invalid = SnobbyValidator.new({ name: 'Timmy', age: 8 })
valid.valid? #true
valid.errors # {}
invalid.valid? #false
invalid.errors # { errors: { name: [ "Must be of royalty "], age: ["No minors allowed"] } }
Or use some of the baked in validators
invalid = IsHashValid::Validator.new({ name: 2 }, {
name: {
not_empty: "Name may not be empty",
be_string: "Name must be a string"
}
})
invalid.valid? #false
invalid.errors # { errors: { name: [ "Name may not be empty", "Name must be a string" ] } }
Current baked in validators are
- not_empty
- be_string
- required
Contributing
- Fork it ( https://github.com/BenJanecke/is_hash_valid/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Run the tests!
- Create a new Pull Request