grape
grape copied to clipboard
Why given block condition not match, but declared return block inside params?
I want declared not to allow unvalidated parameters if the given condition is not met
case1
desc 'Create T'
params do
optional :device_type, type: String, desc: 'device_type', values: ['type1', 'type2']
given device_type: ->(val) { val == 'type1' } do
requires :device_config, type: Hash do
requires :rtsp, type: String, regexp: %r{rtsp://}
end
end
given device_type: ->(val) { val == 'type2' } do
requires :device_config, type: Hash do
requires :number, type: Integer
end
end
end
put ':id' do
puts declared(params)
puts declared(params, include_missing: false)
end
params
{"device_config"=>{"rtsp"=>"fake_rtsp", number=>'fake_number'}}
result
{"device_type"=>nil, "device_config"=>{"rtsp"=>"fake_rtsp"}}
{"device_type"=>nil, "device_config"=>{"rtsp"=>"fake_rtsp"}} // requires not work, but declared return
case2
desc 'Create T'
params do
optional :device_type, type: String, desc: 'device_type', values: ['type1', 'type2']
given device_type: ->(val) { val == 'type1' } do
requires :device_config, type: Hash do
requires :rtsp, type: String, regexp: %r{rtsp://}
end
end
given device_type: ->(val) { val == 'type2' } do
requires :device_config, type: Hash do
requires :number, type: Integer
end
end
end
put ':id' do
puts declared(params)
puts declared(params, include_missing: false)
end
params
{"device_config"=>{"rtsp"=>"fake_rtsp", number=>"fake_rtsp"}}
result
{"device_type"=>nil, "device_config"=>{"number"=>"fake_rtsp"}}
{"device_config"=>{"number"=>"fake_rtsp"}} // requires not work, but declared return last delimit
This does look like a bug where we are not evaluating given
in declared
? Care to maybe PR some failing tests/fix?
Closed via https://github.com/ruby-grape/grape/pull/2285