grape
grape copied to clipboard
Fail on (or get list of) unknown parameters
Hi,
Prior to https://github.com/ruby-grape/grape/pull/2189 the params
object also contained the unknown parameters that weren't described in the route definition.
For instance:
requires :name,
type: String,
as: :company_name,
optional :bar,
type: String,
If then the following params where passed:
{
"name": "Grape",
"baz": "Bar",
}
The params object contained:
{
"company_name": "Grape",
"baz": "Bar",
}
This way our code could fail on the fact that "baz"
was passed in but doesn't exist. This is helpful because most probably the consumer of the api wanted to pass in bar
which is an optional field (this is a frequent occurring scenario btw in our production systems). I think it is good practice to fail fast in scenarios where parameters are passed in that are "unknown".
After this fix there is no easy way anymore to get these unknown parameters since declared
filters them out, declared(params)
yields to:
{
"company_name": "Grape",
}
Merging them with the original params
is also not an option because then we also get the name
parameter back again.
So my question is: Is there a way in grape to get the "unknown parameters" passed in? Or let Grape fail when such a parameter is passed?
This sounds like a valid scenario. Should we add an unknown
or undeclared
helper? Want to give this a try @lvonk?
I can give it a try. I'd opt for failing when such a parameter is passed in, perhaps via a setting? Like fail_on_undeclared_params = true|false
then when it is false
you could still use the helper to get a list of these params.
I think both features would be valuable, and failing would be built on top of extracting the names of such parameters.