grape icon indicating copy to clipboard operation
grape copied to clipboard

how to define multiple allowed types with values?

Open stanleyyuenyiu opened this issue 5 years ago • 2 comments

Let say I have an API parameter, I wanna the parameter accept a boolean or a string

optional :status, types: [Boolean, String]

As I wanna scoped the string value, but I found that I cant define as below optional :status, types: [Boolean, String], values: %w(active hidden draft)

May I know how to achieve this objective?

stanleyyuenyiu avatar Jan 16 '21 04:01 stanleyyuenyiu

Right now I think this should work:

optional :status, types: [Boolean, String], values: ->(v) { v.is_a?(Boolean) ? v : %w(active hidden draft) }

To separate values we would need some kind of other syntax. I'll mark this as a feature request. For example:

optional :status, types: { Boolean => {}, String => { values: %w(active hidden draft) }

We could allow re-declaration, too, that would be cumulative by default. I think this would be my preference. PRs welcome.

optional :status, type: String, values: %w(active hidden draft) 
optional :status, type: Boolean

dblock avatar Jan 16 '21 22:01 dblock

I would try to use dependent params. An example from the doc:

given type: ->(val) { val == 'foo' } do
  requires :description
end

dnesteryuk avatar Jan 20 '21 19:01 dnesteryuk