You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's say I have a GET /users route that accepts 3 parameters to filter a list of users from a database. All parameters are not required:
param: name String
param: age Integer
param: is_active Boolean
But, I want to force the client to have a least 1 filter parameter, so I use:
any_of:name,:age,:is_active
The client wants to get all inactive users (of any name and age), so they send a request with a single parameter: is_active: false.
This will result in sinatra-param throwing an error like this:
"status": "error",
"details": "Invalid '[\"name\", \"age\", \"is_active\"]' param",
"class": "Sinatra::Param::InvalidParameterError",
"message": "One of parameters [name, age, is_active] is required",
This seems to occur because false does not respond to .empty?, in the definition for blank? in lib/sinatra/param.rb and therefor does not count as an existing parameter in the query:
...
defvalidate_any_of!(params,names,options)raiseInvalidParameterError,"One of parameters [#{names.join(', ')}] is required"ifnames.count{|name| present?(params[name])} < 1end
...
# ActiveSupport #present? and #blank? without patching Objectdefpresent?(object)
!blank?(object)enddefblank?(object)object.respond_to?(:empty?) ? object.empty? : !objectend
...
A boolean parameter set to false does not "count" as a valid parameter provided for the routes that use any_of, one_of and any_all_or_none_of. Is this the expected behaviour?
Am I looking at this the wrong way conceptually? Would it make sense to change the logic to accept false as a "valid" parameter value?
Thanks
The text was updated successfully, but these errors were encountered:
Hello! Here's my use case:
Let's say I have a GET /users route that accepts 3 parameters to filter a list of users from a database. All parameters are not required:
name
Stringage
Integeris_active
BooleanBut, I want to force the client to have a least 1 filter parameter, so I use:
The client wants to get all inactive users (of any name and age), so they send a request with a single parameter:
is_active: false
.This will result in sinatra-param throwing an error like this:
This seems to occur because
false
does not respond to.empty?
, in the definition forblank?
in lib/sinatra/param.rb and therefor does not count as an existing parameter in the query:A boolean parameter set to
false
does not "count" as a valid parameter provided for the routes that useany_of
,one_of
andany_all_or_none_of
. Is this the expected behaviour?Am I looking at this the wrong way conceptually? Would it make sense to change the logic to accept
false
as a "valid" parameter value?Thanks
The text was updated successfully, but these errors were encountered: