-
Notifications
You must be signed in to change notification settings - Fork 463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add nested validator to validate nested children #189
Conversation
You're on fire! Seems like it might solve the #151 issue, right? What it it worked this way (as suggested in #151): param :user_id, Integer
param :comments, Array do
param :name, String
param :comment, String
end Will try to find some time over the weekend to look though the rest of the PRs, with plan of having new version released next week with this changes (and perhaps some others that I manage to review) |
In fact, I'm not sure Array is a better name.
so the parameters definition will be like: param :comments, Array do
param nil, String # no name for the parameter
end
param :ids, Array do
param nil, Integer # no name for the parameter
end which is different from what I implemented which can validate this kind of structure:
But perhaps we could find a solution where Array could work for both structures (with and without nested hash). |
For the example above, it might be something like: param :ids, array_of(String) What do you thing about this? |
or why not just using brackets ? param :ids, [String] |
That looks good! |
I committed the changes to use |
end | ||
|
||
def self.build(param_description, argument, options, block) | ||
self.new(param_description, block, options[:param_group]) if block.is_a?(Proc) && block.arity <= 0 && argument == Array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpicker: could the block.arity
be less than 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know I just copied the one used for Hash
without thinking about it :(
https://github.com/Pajk/apipie-rails/blob/master/lib/apipie/validator.rb#L204
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm checking this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, arity can be negative, when optional args are given http://www.ruby-doc.org/core-2.1.0/Proc.html#method-i-arity. Anyway, I'm quite sure we want to have enough 0 there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, you are right, == 0 should be better.
Nice, I have one nitpicker comment left, but all looks great otherwise |
Fixes #151 |
Add nested validator to validate nested children
Thanks a lot for this patch! Great job |
Released in 0.1.0. Thank you for your help! |
The goal of this validator is to validate each element inside of the array.
It enable to validate this kind of parameters:
The definition of the parameters will be:
I think this validator could be useful for many people and so should be included by default in the gem.
Any remarks ?