Skip to content
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

Fix error messages for nested multiple params validations #1913

Merged

Conversation

bikolya
Copy link
Contributor

@bikolya bikolya commented Oct 4, 2019

Previously multiple params validators returned messages without considering the scope.
For example, in case of hash params:
{ "param_1,param_2": 'error message' }
instead of:
{ "hash[param_1],hash[param_2]": 'error message' }

Or in case of arrays, indexes were ignored completely:
{ "param_1,param_2": 'error message' }
instead of:
{ "array[1][param_1],array[1][param_2]": 'error message' }

This PR fixes it by using AttributesIterator in a similar way to Grape::Validations::Base.

Also the specs are refactored to work in a similar way to the ordinary single-parameter validations.

@bikolya bikolya force-pushed the fix-errors-for-multiple-params-validations branch from 170ee14 to a64401d Compare October 4, 2019 18:56
@@ -5,11 +5,12 @@ class AttributesIterator

attr_reader :scope

def initialize(validator, scope, params)
def initialize(validator, scope, params, multiple_params: false)
Copy link
Member

@dblock dblock Oct 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writing another attribute iterator, MutlipleParamsAttributeIterator and SingleParamsAttributeIterator instead of this additional parameter? Subclass from an AttributeIterator and we have something much cleaner.

Copy link
Contributor Author

@bikolya bikolya Oct 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, thanks! I changed this part in the last commit, please check it out. Also the build is green now.

@dblock
Copy link
Member

dblock commented Oct 5, 2019

This is solid work. See above for my comments. Needs a green build, please.

Previously all the multiple params validators returned messages
without considering the scope.
For example, in case of hash params:
`{ "param_1,param_2": 'error message' }`
instead of:
`{ "hash[param_1],hash[param_2]": 'error message' }`

Or in case of arrays indexes were ignored completely:
`{ "param_1,param_2": 'error message' }`
instead of:
`{ "array[1][param_1],array[1][param_2]": 'error message' }`

This commit fixes it by using AttributesIterator in a similar way to
Grape::Validations::Base.
TBH, no idea how they managed to pass before
@bikolya bikolya force-pushed the fix-errors-for-multiple-params-validations branch from a64401d to b9c1a32 Compare October 24, 2019 08:51
@dblock dblock merged commit e855504 into ruby-grape:master Oct 24, 2019
@dblock
Copy link
Member

dblock commented Oct 24, 2019

Perfect, merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants