jamesbrooks / hash_validator Goto Github PK
View Code? Open in Web Editor NEWRuby library to validate hashes (Hash) against user-defined requirements
License: MIT License
Ruby library to validate hashes (Hash) against user-defined requirements
License: MIT License
This library would benefit from having an option to prohibit a hash from having any keys not explicitly named in the validation hash. For example:
my_hash = { first_name: "John",
last_name: "Doe",
invalid: "data that should not be here" }
validation = { first_name: 'string',
last_name: 'string' }
# Fails because of presence of unexpected key "invalid"
HashValidator.validate(my_hash, validation, exclusive: true)
I can put together a PR if we can agree on the interface.
Hello james! I have a feature request. Most modern languages have a concept named union types:
The type is an element of a set. I know it's easy to write with as a lambda
. But it's way better to read, when you have a notation like HashValidator::Union[*typesl]
. Another name could be HashValidator::Or
.
class Email; attr_accessor :text; end
# Validations hash
validators = { email: HashValidator::Union[String, Email] }
# First case, it's a String
user1 = { email: '[email protected]' }
HashValidator.validate(user1, validations)
# Second case, it's an Email
email = Email.new
email.text = '[email protected]'
user2 = { email: email }
HashValidator.validate(user2, validations)
validator = HashValidator.validate({ age: 27 }, { age: ['odd', 'presence', 'integer'] })
Append validator doesn't work when adding custom validations. You have multiple people (myself included) forking the repository to add custom validators because when you append the validator using your method, it appends it to the list, but then can't find that validator. If you try and append it again, it indicates the validator has already been appended.
Hopefully you can check this out because I (and I'm sure many others) would be really happy to be able to use the custom validations without forking the repository.
...and it's also really annoying to debug so it should be fixed :)
valid = { "value" => "required" }
test1 = { "value" => false }
test2 = { "value" => true }
test3 = { "value" => "Fruitbat" }HashValidator.validate(test3,valid).valid?
=> true
HashValidator.validate(test2,valid).valid?
=> true
HashValidator.validate(test1,valid).valid?
=> false
All of the 3 evaluations should validate true.
Thanks for making this gem, it comes handy at multiple occasions. Did you consider allowing validators as symbols?
validations = {
user: {
first_name: :string,
last_name: :string,
age: :numeric,
likes: :array
}
}
Using symbols seems idiomatic (see http://stackoverflow.com/questions/16621073/when-to-use-symbols-instead-of-strings-in-ruby).
Suppose I have a hash that has optional keys, but the keys that are provided must be in a list if they are provided.
VALID_FOO = [ONE,TWO]
VALID_BAR = [THREE,FOUR]
valid_hash = { foo: [ONE,TWO], bar: [THREE,FOUR]}
also_valid = {}
also_valid = {foo: [ONE]}
also_valid = {bar: [THREE]}
invalid = {foo: ONE}
invalid_also = {bar: ONE}
invalid_also = {bar: [ONE]}
Thus, bar and foo keys are optional. However, if they exist, they must be an array, whose values are in VALID_FOO or VALID_BAR as appropriate.
How would we specify that validation?
First of all, very nice gem!
Use case: { access_token: 'string', action: 'string', params: 'hash' }
I want to make sure I only have these three keys in my hash, but the validation for the params will be done in another part of the code. Is it possible to implement this with the current API? I tried using a '{}' but it fails since it continue the validation on the inner hash.
This would be specially useful with the strict flag true. In a non strict validation, I could omit the params key, but in a strict validation I could not find a way to express this.
If you guys agree this is a good problem to solve, let me know and I'd love to implement the PR.
Cheers
I'm trying to use regexp validation with the following validation hash:
validations = {
'health_check' => {
'protocol' => /^(http|tcp)$/,
...
}
}
When I run validation I got the following error message:
/Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators.rb:18:in 'validator_for': Could not find valid validator for: (?-mix:^(http|tcp)$) (StandardError)
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:21:in `block in validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:20:in `each'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:20:in `validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:21:in `block in validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:20:in `each'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/validators/hash_validator.rb:20:in `validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/base.rb:28:in `validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/base.rb:10:in `initialize'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/base.rb:19:in `new'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator/base.rb:19:in `validate'
from /Users/lauri/.rvm/gems/ruby-2.3.1/gems/hash_validator-0.6.0/lib/hash_validator.rb:3:in `validate'
Am I doing something wrong or is this a bug?
Hello!
I'm playing around with this great gem but I had a question that I can't figure out yet.
let's say I have a hash like
events: ['foo', 'bar', 'baz']
and I want to validate that the events key is an array and that the values within that array are included in some permitted value how I could achieve that without a proc?
right now my validator looks something like
'events' => lambda { |value|
value.is_a?(Array) && (value - ['foo', 'bar', 'baz']).empty?
}
which works fine but I was wondering if the DSL allowed for something that could be translated to JSON and passed to my frontend as well.
How do you validate something like:
{
'married' => 'boolean',
'spouse_name' => 'string if married otherwise nil'
}
Or:
{
'pets' => 'array'
'favorite_pet' => 'string that is in "pets"'
}
I noticed that the ArrayValidator
was added (#18) but it's not available when installing from RubyGems.
Could you release it? Thanks!
It would be great if you could specify an enumeration for validation purposes, where the value must exist in the enumeration. You could use an array to specify the accepted values:
{
status: [:new, :open, :closed]
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.