Extend `given` to check against the value
It would be nice to allow a value to be specified on a given so that different parameters could be included for example:
params do
requires :grant_type, type: String, values: [:password, :refresh_token]
requires :client_id, type: String
given :grant_type, value: ->(*) { |attr| attr == :password } do
requires :username, type: String
requires :password, type: String
end
given :grant_type, value: ->(*) { |attr| attr == :refresh_token } do
requires :refresh_token, type: String
end
end
I like it.
I'm happy to look into implementation, any pointers on accessing the value from https://github.com/ruby-grape/grape/blob/master/lib/grape/dsl/parameters.rb or should I be looking elsewhere?
@danieldraper that section of code is executed right when your API loads, and is what defines the new parameter scope with a dependent_on value. You'll need to update that to save the value lambda for later reference, and update ParamsScope#should_validate? to verify that, if the scope is dependent on an attribute, it also satisfies the value lambda.
Zooming out a bit, right now, given can take more than one parameter. You could either enforce the value check for all dependent parameters, or use a different format (maybe something like,
given foo: ->(val) { val == :bar }, baz: ->(val) { val == :qaz }
... though it's a bit limiting and not as clear).
@danieldraper can you check https://github.com/ruby-grape/grape/pull/1443 and close this issue please? Thanks.
@dblock Could you close this, please?
Closing via #1443.