Hi,
I'm having trouble using the Boolean type with proper boolean values.
What I initially wanted to do:
optional :active, type: Boolean, default: true
Always returns active is invalid should I send { "active": true } or { "active": false }
What i had to do is actually add coerce_with using the identity lambda...
optional :active, type: Boolean, default: true, coerce_with: ->(v) { v }
Did I miss something here ?
I'm using version 0.18
Boolean is actually Virtus::Boolean if I am not mistaken. Either way try to write a spec for this, you obviously shouldn't need to coerce a boolean.
I just experienced the same problem when using type: Boolean, any value is considered invalid.
My workaround was to use the default coercion method, changing type for coerce in the param declaration.
optional :flag, coerce: Boolean
Then it works with any value that can be coerced into a Boolean, like {flag: true}, {flag: 'true'}, {flag: 1}, {flag: '1'}.
try to use Virtus::Attribute::Boolean instead, see: https://github.com/ruby-grape/grape/issues/1115
We alias Boolean to that in https://github.com/ruby-grape/grape/blob/master/lib/grape/validations/validators/coerce.rb#L3.
Closing via more tests in https://github.com/ruby-grape/grape/pull/1650.
Most helpful comment
I just experienced the same problem when using
type: Boolean, any value is considered invalid.My workaround was to use the default coercion method, changing
typeforcoercein the param declaration.Then it works with any value that can be coerced into a
Boolean, like{flag: true},{flag: 'true'},{flag: 1},{flag: '1'}.