Skip to content

Commit

Permalink
Fix usage of exactly_one_of in shared params
Browse files Browse the repository at this point in the history
  • Loading branch information
milgner committed Mar 28, 2018
1 parent a68d36d commit d3765bf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

* [#1755](https://github.com/ruby-grape/grape/pull/1755): Fix shared params with exactly_one_of - [@milgner](https://github.com/milgner).
* [#1740](https://github.com/ruby-grape/grape/pull/1740): Fix dependent parameter validation using `given` when parameter is a `Hash` - [@jvortmann](https://github.com/jvortmann).
* [#1737](https://github.com/ruby-grape/grape/pull/1737): Fix translating error when passing symbols as params in custom validations - [@mlzhuyi](https://github.com/mlzhuyi).
* [#1749](https://github.com/ruby-grape/grape/pull/1749): Allow rescue from non-`StandardError` exceptions - [@dm1try](https://github.com/dm1try).
Expand Down
4 changes: 2 additions & 2 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def initialize(opts, &block)
# validated
def should_validate?(parameters)
return false if @optional && (params(parameters).blank? || all_element_blank?(parameters))

return false unless meets_dependency?(params(parameters), parameters)
return true if parent.nil?
parent.should_validate?(parameters)
end
Expand All @@ -51,7 +51,7 @@ def meets_dependency?(params, request_params)
end

return true unless @dependent_on

return params.any? { |param| meets_dependency?(param, request_params) } if params.is_a?(Array)
params = params.with_indifferent_access

@dependent_on.each do |dependency|
Expand Down
47 changes: 47 additions & 0 deletions spec/grape/api/shared_helpers_exactly_one_of_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe Grape::API::Helpers do
subject do
shared_params = Module.new do
extend Grape::API::Helpers

params :drink do
optional :beer
optional :wine
exactly_one_of :beer, :wine
end
end

Class.new(Grape::API) do
helpers shared_params
format :json

params do
requires :orderType, type: String, values: %w[food drink]
given orderType: ->(val) { val == 'food' } do
optional :pasta
optional :pizza
exactly_one_of :pasta, :pizza
end
given orderType: ->(val) { val == 'drink' } do
use :drink
end
end
get do
declared(params, include_missing: true)
end
end
end

def app
subject
end

it 'defines parameters' do
get '/', orderType: 'food', pizza: 'mista'
expect(last_response.status).to eq 200
expect(last_response.body).to eq({ orderType: 'food',
pasta: nil, pizza: 'mista',
beer: nil, wine: nil }.to_json)
end
end

0 comments on commit d3765bf

Please sign in to comment.