Search code examples
ruby-on-railsapipie

Rails apipie validation for boolean parameters failing


My delete api takes a boolean query params /users?force=true. In my controller, I added the following for apipie documentation

param :force, [true, false], required: false, desc: "query parameter."

When I make the api call, I get

Apipie::ParamInvalid (Invalid parameter 'force' value "true": Must be one of: <code>true</code>, <code>false</code>.).

I tried passing /users?force=1, /users?force, but 1 is treated as "1" and not passing anything is treated as nil and both calls fail. How do I make the validation pass?

Note: I am aware that the api definition is not restful.


Solution

  • The param you are passing through ends up being "true" as in string, not a boolean, that's why it's failing. Without any type casting, rails has got no idea that you're trying to pass boolean, not a string.

    You should whitelist "true", "false" as strings in valid options like:

    param :force, [true, false, "true", "false"], required: false, desc: "query parameter."