Grape: Boolean type not working as expected

Created on 5 Jan 2017  路  5Comments  路  Source: ruby-grape/grape

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

bug?

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 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'}.

All 5 comments

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

Was this page helpful?
0 / 5 - 0 ratings