2017-02-16 91 views
2

我正在为Web应用程序开发API。在一个特定情况下,我面临的情况是仅当param1的值为false时才需要param2。我能够使用条件语句来实现它。我想知道在这种类型的验证中是否有葡萄内置的方法。在东西线下方基于rail-grape中的条件验证参数

params do 
requires :model_name, type: Hash do 
    optional :params1 
    if params1 == false 
    require :params2 
    end 
end 
end 

回答

0

你可能需要mutually_exclusive验证,以确保给予PARAMS不存在同时在请求

params do 
requires :model_name, type: Hash do 
    optional :params1 
    optional :params2 
    mutually_exclusive :params1, :params2 
end 
end 

葡萄支持它version 0.8.0

开始有也有其他选项,如exactly_one_of,all_or_none_of,at_least_one_ofRead more

+0

感谢Slava。但是我的请求包含了这两个参数。我正在使用ember.js作为前端,不允许选择要发送到rails的参数。 – sureshprasanna70