grape icon indicating copy to clipboard operation
grape copied to clipboard

Why given block condition not match, but declared return block inside params?

Open YaEvan opened this issue 2 years ago • 1 comments

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

YaEvan avatar Aug 03 '22 06:08 YaEvan

This does look like a bug where we are not evaluating given in declared? Care to maybe PR some failing tests/fix?

dblock avatar Aug 04 '22 13:08 dblock

Closed via https://github.com/ruby-grape/grape/pull/2285

dblock avatar Nov 02 '22 18:11 dblock