多个`给出的'块覆盖了GRAPE API中的参数

时间:2019-06-26 22:11:24

标签: ruby grape-api

在Grape API POST路由中,我希望数组参数actions会收到不同types的多个动作。我试图有条件地要求使用两个properties表达式来given,但是我发现第二个表达式总是会覆盖第一个表达式。

下面是代码示例:

class Examples < Grape::API
      namespace :examples do
        params do
          group :actions, type: Array[JSON] do
            requires :action_type, type: String
            given action_type: ->(val) { val == 'create' } do
              requires :properties, type: Hash do
                requires :category, type: String
              end
            end
            given action_type: ->(val) { val == 'update' } do
              requires :properties, type: Hash do
                requires :not_category, type: String
              end
            end
          end
        end
        post do
          declared_params = declared params
          ...
        end
      end
    end

在我看来,params是:

{\"actions\"=>[{\"action_type\"=>\"create_task\", \"properties\"=>{\"category\"=>\"to_do\"}}]}

但是,declared_params将始终返回

{\"actions\"=>[{\"action_type\"=>\"create_task\", \"properties\"=>{\"not_category\"=>nil}}]}

有没有一种方法可以使用given而不会覆盖参数?

0 个答案:

没有答案
相关问题