grape icon indicating copy to clipboard operation
grape copied to clipboard

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

Open fjesus-ic opened this issue 2 years ago • 2 comments

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!

fjesus-ic avatar Mar 13 '24 12:03 fjesus-ic

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

dblock avatar Mar 14 '24 01:03 dblock

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!

fjesus-ic avatar Mar 14 '24 15:03 fjesus-ic