Ruby/Grape 特定条件下的必需参数

Ruby/Grape required parameters on a certain condition

对于我的其中一种方法,以下方法不起作用。我几乎直接从官方文档中复制了所有内容:

params do
requires :authenticationType, type: Array[String], values: ['LOCAL', 'AD']
given authenticationType: ->(val) { val == 'LOCAL' } do
  requires :admin, type: String, allow_blank: false, regexp: /^[\w\.\@-]{1,64}$/
  requires :password, type: String, allow_blank: false, regexp: /^[\w\.\@-]{1,64}$/
end
end

"given" 行出错。任何人都知道出了什么问题。我的目标:仅当 'authenticationType' == 'LOCAL' 时,用户才应提供 'admin' 和 'password'

错误:

[ 2017-03-03 00:39:18.4848 14970/7f5d0603f700 age/Cor/App/Implementation.cpp:304 ]: Could not spawn process for application /vagrant/masterapi: An error occurred while starting up the preloader. Error ID: 0bd79149 Error details saved to: /tmp/passenger-error-3OYsdJ.html Message from application: Grape::Exceptions::UnknownParameter (Grape::Exceptions::UnknownParameter)
/usr/local/lib/ruby/gems/2.3.0/gems/grape-0.16.2/lib/grape/dsl/parameters.rb:170:in block in given'<br> /usr/local/lib/ruby/gems/2.3.0/gems/grape-0.16.2/lib/grape/dsl/parameters.rb:169:in each'
/usr/local/lib/ruby/gems/2.3.0/gems/grape-0.16.2/lib/grape/dsl/parameters.rb:169:in given' /vagrant/masterapi/controllers/papi_controller.rb:93:in block in '

'given' 自 grape 版本 0.17 起仅接受 proc,在合并请求 (MR) 1443 中实现。所以你应该更新,或者如果这不可行,请尝试将此 MR 向后移植到 0.16.2。

Here's 适用于您的版本的自述文件。

此外,在您的示例中,authenticationType 参数的类型为 Array[String],因此(至少在葡萄 0.17 中),proc 将收到 Hashie::Array

这意味着:

->(val) { val == 'LOCAL' }

应该是

->(val) { val.first == 'LOCAL' }