how to define multiple allowed types with values?
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?
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
I would try to use dependent params. An example from the doc:
given type: ->(val) { val == 'foo' } do
requires :description
end