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

Add support for exclusive parameter groups (e.g. exactly_one_of) #2418

Open
fjesus-ic opened this issue Mar 13, 2024 · 2 comments
Open

Add support for exclusive parameter groups (e.g. exactly_one_of) #2418

fjesus-ic opened this issue Mar 13, 2024 · 2 comments

Comments

@fjesus-ic
Copy link

fjesus-ic commented Mar 13, 2024

grape 2.0.0

Having an issue when using nested params with exactly_one_of, here is my current setup

params :required_date do
  optional :date
  use :date_range
  exactly_one_of :date, :date_range
end

params :date_range do
  requires :start_date
  requires :end_date
  valid_interval :start_date, :end_date
end

On my endpoint I'm using the required_date param

when I run the following:
/example_endpoint?start_date=2024-03-11&end_date=2024-03-12
It returns:
{"error":"parameter validation failed","parameters":["date","date_range"],"values":[null,null],"reason":"are missing, exactly one parameter must be provided: date, date_range"}

Any way that grape will check the nested params(start_date and end_date) instead of looking for date_range?

Thanks!

@dblock
Copy link
Member

dblock commented Mar 14, 2024

In your example when you use, it just acts like an include, so use :date_rage just imports start_date and end_date, there's no such parameter as date_range. Then you say one of date or date_range, then pass neither, and you get this error.

The feature you want is to say "either date, or a range (start and end)", however I don't think it can be expressed today with groups as you did, so this would be a (great) feature request.

Does this cover all the scenarios? Maybe write out a spec for this so if we can improve the DSL we know it still works?

params do
   optional :date
   optional :start_date
   optional :end_date
   mutually_exclusive :date, :start_date
   mutually_exclusive :date, :end_date
   at_least_one_of :date, :start_date
   at_least_one_of :date, :end_date
   all_or_none_of :start_date, :end_date
end

@fjesus-ic
Copy link
Author

That covers my scenario, though I chose a different approach (irrelevant to this question). Thank you for your input and hope this feature gets implemented!

@dblock dblock changed the title Issues with nested params and exactly_one_of Add support for exclusive parameter groups (e.g. exactly_one_of) Mar 14, 2024
@dblock dblock removed the question label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants