Skip to content

Commit

Permalink
specify type in group of required params (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrichter1 committed Jan 10, 2015
1 parent 3eea554 commit 0ca9feb
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module Exceptions
autoload :UnknownOptions, 'grape/exceptions/unknown_options'
autoload :InvalidWithOptionForRepresent, 'grape/exceptions/invalid_with_option_for_represent'
autoload :IncompatibleOptionValues, 'grape/exceptions/incompatible_option_values'
autoload :InvalidGroupType, 'grape/exceptions/invalid_group_type'
end

module ErrorFormatter
Expand Down
10 changes: 10 additions & 0 deletions lib/grape/exceptions/invalid_group_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# encoding: utf-8
module Grape
module Exceptions
class InvalidGroupType < Base
def initialize(group_type)
super(message: compose_message('invalid_group_type', group_type: group_type))
end
end
end
end
1 change: 1 addition & 0 deletions lib/grape/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ en:
at_least_one: 'are missing, at least one parameter must be provided'
exactly_one: 'are missing, exactly one parameter must be provided'
all_or_none: 'provide all or none of parameters'
invalid_group_type: 'group of required parameters must have a specified type of Array or Hash, type provided: %{group_type}'

6 changes: 6 additions & 0 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def validate_attributes(attrs, opts, &block)
end

def new_scope(attrs, optional = false, &block)
# if required params are grouped and no type or unsupported type is provided, raise an error
type = attrs[1] ? attrs[1][:type] : nil
if attrs.first && (type.nil? || (type != Array && type != Hash))
fail Grape::Exceptions::InvalidGroupType.new(type) unless optional
end

opts = attrs[1] || { type: Array }
ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2166,11 +2166,11 @@ def static
it 'groups nested params and prevents overwriting of params with same name in different groups' do
subject.desc 'method'
subject.params do
group :group1 do
group :group1, type: Array do
optional :param1, desc: 'group1 param1 desc'
requires :param2, desc: 'group1 param2 desc'
end
group :group2 do
group :group2, type: Array do
optional :param1, desc: 'group2 param1 desc'
requires :param2, desc: 'group2 param2 desc'
end
Expand All @@ -2190,7 +2190,7 @@ def static
subject.desc 'nesting'
subject.params do
requires :root_param, desc: 'root param'
group :nested do
group :nested, type: Array do
requires :nested_param, desc: 'nested param'
end
end
Expand Down
55 changes: 55 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,59 @@ def app
end.to raise_error Grape::Exceptions::IncompatibleOptionValues
end
end

context 'parameters in group' do
it 'errors when no type is provided' do
expect do
subject.params do
group :a do
requires :b
end
end
end.to raise_error Grape::Exceptions::InvalidGroupType
end

it 'allows Hash as type' do
subject.params do
group :a, type: Hash do
requires :b
end
end
subject.get('/group') { 'group works' }
get '/group', a: { b: true }
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('group works')
end

it 'allows Array as type' do
subject.params do
group :a, type: Array do
requires :b
end
end
subject.get('/group') { 'group works' }
get '/group', a: [{ b: true }]
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('group works')
end

it 'errors with an unsupported type' do
expect do
subject.params do
group :a, type: Set do
requires :b
end
end
end.to raise_error Grape::Exceptions::InvalidGroupType
end

it 'works with optional parameters' do
subject.params do
optional :user, type: Hash do
requires :name, allow_blank: false
end
end
get '/group'
end
end
end
22 changes: 11 additions & 11 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def define_requires_none

it 'adds to declared parameters' do
subject.params do
requires :items do
requires :items, type: Array do
requires :key
end
end
Expand Down Expand Up @@ -272,7 +272,7 @@ def define_requires_none

it 'adds to declared parameters' do
subject.params do
requires :items do
requires :items, type: Array do
requires :key
end
end
Expand All @@ -283,7 +283,7 @@ def define_requires_none
context 'group' do
before do
subject.params do
group :items do
group :items, type: Array do
requires :key
end
end
Expand All @@ -306,7 +306,7 @@ def define_requires_none

it 'adds to declared parameters' do
subject.params do
group :items do
group :items, type: Array do
requires :key
end
end
Expand Down Expand Up @@ -395,9 +395,9 @@ def validate_param!(attr_name, params)
context 'validation within arrays' do
before do
subject.params do
group :children do
group :children, type: Array do
requires :name
group :parents do
group :parents, type: Array do
requires :name
end
end
Expand Down Expand Up @@ -457,7 +457,7 @@ def validate_param!(attr_name, params)
context 'with block param' do
before do
subject.params do
requires :planets do
requires :planets, type: Array do
requires :name
end
end
Expand All @@ -469,7 +469,7 @@ def validate_param!(attr_name, params)
end

subject.params do
group :stars do
group :stars, type: Array do
requires :name
end
end
Expand Down Expand Up @@ -549,9 +549,9 @@ def validate_param!(attr_name, params)
context 'validation within arrays with JSON' do
before do
subject.params do
group :children do
group :children, type: Array do
requires :name
group :parents do
group :parents, type: Array do
requires :name
end
end
Expand Down Expand Up @@ -695,7 +695,7 @@ def validate_param!(attr_name, params)
optional :items do
requires :key
optional(:optional_subitems) { requires :value }
requires(:required_subitems) { requires :value }
requires(:required_subitems, type: Array) { requires :value }
end
end
expect(subject.route_setting(:declared_params)).to eq([items: [:key, { optional_subitems: [:value] }, { required_subitems: [:value] }]])
Expand Down

0 comments on commit 0ca9feb

Please sign in to comment.