Skip to content

Commit

Permalink
try to implement behaviour for default vaues in Hash/Array groupedparams
Browse files Browse the repository at this point in the history
  • Loading branch information
dm1try committed Jul 11, 2014
1 parent 06de7f8 commit d29c822
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
7 changes: 4 additions & 3 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def self.register_validator(short_name, klass)

class ParamsScope
attr_accessor :element, :parent
attr_reader :type

def initialize(opts, &block)
@element = opts[:element]
Expand Down Expand Up @@ -210,9 +211,9 @@ def validate_attributes(attrs, opts, &block)
end

def new_scope(attrs, optional = false, &block)
opts = attrs[1] || { type: Array }
raise ArgumentError unless opts.keys.to_set.subset? [:type].to_set
ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: opts[:type], &block)
opts = attrs[1] || {}
type = opts.delete(:type) || Array
ParamsScope.new(api: @api, element: attrs.first, parent: self, optional: optional, type: type, &block)
end

# Pushes declared params to parent or settings
Expand Down
3 changes: 1 addition & 2 deletions lib/grape/validations/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Validations
class DefaultValidator < Validator
def initialize(attrs, options, required, scope)
@default = options
@type = scope.type
super
end

Expand All @@ -12,11 +13,9 @@ def validate_param!(attr_name, params)

def validate!(params)
attrs = AttributesIterator.new(self, @scope, params)
parent_element = @scope.element
attrs.each do |resource_params, attr_name|
if resource_params[attr_name].nil?
validate_param!(attr_name, resource_params)
params[parent_element] = resource_params if parent_element && params[parent_element].nil?
end
end
end
Expand Down
69 changes: 63 additions & 6 deletions spec/grape/validations/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,42 @@ class API < Grape::API
get '/array' do
{ array: params[:array] }
end

params do
optional :foo, type: Array do
optional :bar, default: "bar"
end
end
get '/optional_with_default_inside_optional_array_without_default' do
{ foo: params[:foo] }
end

params do
optional :foo, type: Array, default: [] do
optional :bar, default: "bar"
end
end
get '/optional_with_default_inside_optional_array_with_default' do
{ foo: params[:foo] }
end

params do
optional :foo, type: Hash do
optional :bar, default: "bar"
end
end
get '/optional_with_default_inside_optional_hash_without_default' do
{ foo: params[:foo] }
end

params do
optional :foo, type: Hash, default: {} do
optional :bar, default: "bar"
end
end
get '/optional_with_default_inside_optional_hash_with_default' do
{ foo: params[:foo] }
end
end
end
end
Expand Down Expand Up @@ -108,16 +144,37 @@ def app
expect(before['random_number']).not_to eq(after['random_number'])
end

it 'set default values for optional grouped params' do
get('/group')
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ foo_bar: 'foo-bar' }.to_json)
end

it 'sets default values for grouped arrays' do
get('/array?array[][name]=name&array[][name]=name2&array[][with_default]=bar2')
expect(last_response.status).to eq(200)
expect(last_response.body).to eq({ array: [{ name: "name", with_default: "default" }, { name: "name2", with_default: "bar2" }] }.to_json)
end

context 'defaults for grouped params' do
context 'with array type' do
it 'sets nil if root param has no default value' do
get('/optional_with_default_inside_optional_array_without_default')
expect(last_response.body).to eq({ foo: nil }.to_json)
end

it 'sets nil if root param has no default value' do
get('/optional_with_default_inside_optional_array_with_default')
expect(last_response.body).to eq({ foo: [] }.to_json)
end
end

context 'with hash type' do
context 'optional with default value inside optional hash with default' do
it 'sets nil if root param has no default value' do
get('/optional_with_default_inside_optional_hash_without_default')
expect(last_response.body).to eq({ foo: nil }.to_json)
end

it 'sets default optional hash value if param unspecified' do
get('/optional_with_default_inside_optional_hash_with_default')
expect(last_response.body).to eq({ foo: {} }.to_json)
end
end
end
end
end
20 changes: 0 additions & 20 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@ def define_requires_none
expect(last_response.body).to eq('required works')
end

it "doesn't allow any key in the options hash other than type" do
expect {
subject.params do
requires(:items, desc: 'Foo') do
requires :key
end
end
}.to raise_error ArgumentError
end

it 'adds to declared parameters' do
subject.params do
requires :items do
Expand Down Expand Up @@ -236,16 +226,6 @@ def define_requires_none
expect(last_response.body).to eq('required works')
end

it "doesn't allow any key in the options hash other than type" do
expect {
subject.params do
requires(:items, desc: 'Foo') do
requires :key
end
end
}.to raise_error ArgumentError
end

it 'adds to declared parameters' do
subject.params do
requires :items do
Expand Down

0 comments on commit d29c822

Please sign in to comment.