Skip to content

Commit

Permalink
Allow validation of nested parameters.
Browse files Browse the repository at this point in the history
Passing the scope to the validators, so they can take the relevant parameters.

Using the group syntax which allows for:

group :user do
  group :admin do
    requires :first_name, :last_name
  end
end
  • Loading branch information
Tim Vandecasteele committed Sep 4, 2012
1 parent 9bd4f23 commit d021e22
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
29 changes: 23 additions & 6 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ module Validations
# All validators must inherit from this class.
#
class Validator
def initialize(attrs, options, required)
def initialize(attrs, options, required, scope)
@attrs = Array(attrs)
@required = required
@scope = scope

if options.is_a?(Hash) && !options.empty?
raise "unknown options: #{options.keys}"
end
end

def validate!(params)
params = @scope.params(params)

@attrs.each do |attr_name|
if @required || params.has_key?(attr_name)
validate_param!(attr_name, params)
Expand All @@ -40,7 +43,7 @@ def self.convert_to_short_name(klass)
##
# Base class for all validators taking only one param.
class SingleOptionValidator < Validator
def initialize(attrs, options, required)
def initialize(attrs, options, required, scope)
@option = options
super
end
Expand All @@ -67,7 +70,11 @@ def self.register_validator(short_name, klass)
end

class ParamsScope
def initialize(api, &block)
attr_accessor :element, :parent

def initialize(api, element, parent, &block)
@element = element
@parent = parent
@api = api
instance_eval(&block)
end
Expand All @@ -89,7 +96,17 @@ def optional(*attrs)

validates(attrs, validations)
end


def group(element, &block)
scope = ParamsScope.new(@api, element, self, &block)
end

def params(params)
params = @parent.params(params) if @parent
params = params[@element] || {} if @element
params
end

private
def validates(attrs, validations)
doc_attrs = { :required => validations.keys.include?(:presence) }
Expand Down Expand Up @@ -130,7 +147,7 @@ def validates(attrs, validations)
def validate(type, options, attrs, doc_attrs)
validator_class = Validations::validators[type.to_s]
if validator_class
@api.settings[:validations] << validator_class.new(attrs, options, doc_attrs[:required])
@api.settings[:validations] << validator_class.new(attrs, options, doc_attrs[:required], self)
else
raise "unknown validator: #{type}"
end
Expand All @@ -145,7 +162,7 @@ def reset_validations!
end

def params(&block)
ParamsScope.new(self, &block)
ParamsScope.new(self, nil, nil, &block)
end

def document_attribute(names, opts)
Expand Down
13 changes: 13 additions & 0 deletions spec/grape/validations/coerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ class User
last_response.status.should == 201
last_response.body.should == File.basename(__FILE__).to_s
end

it 'Nests integers' do
subject.params do
group :integers do
requires :int, :coerce => Integer
end
end
subject.get '/int' do params[:integers][:int].class; end

get '/int', { :integers => { :int => "45" } }
last_response.status.should == 200
last_response.body.should == 'Fixnum'
end
end
end
end
69 changes: 68 additions & 1 deletion spec/grape/validations/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ class API < Grape::API
get do
"Hello"
end

params do
group :user do
requires :first_name, :last_name
end
end
get '/nested' do
"Nested"
end

params do
group :admin do
requires :admin_name
group :super do
group :user do
requires :first_name, :last_name
end
end
end
end
get '/nested_triple' do
"Nested triple"
end
end
end
end
Expand Down Expand Up @@ -67,5 +90,49 @@ def app
last_response.status.should == 200
last_response.body.should == "Hello"
end


it 'validates nested parameters' do
get('/nested')
last_response.status.should == 400
last_response.body.should == "missing parameter: first_name"

get('/nested', :user => {:first_name => "Billy"})
last_response.status.should == 400
last_response.body.should == "missing parameter: last_name"

get('/nested', :user => {:first_name => "Billy", :last_name => "Bob"})
last_response.status.should == 200
last_response.body.should == "Nested"
end

it 'validates triple nested parameters' do
get('/nested_triple')
last_response.status.should == 400
last_response.body.should == "missing parameter: admin_name"

get('/nested_triple', :user => {:first_name => "Billy"})
last_response.status.should == 400
last_response.body.should == "missing parameter: admin_name"

get('/nested_triple', :admin => {:super => {:first_name => "Billy"}})
last_response.status.should == 400
last_response.body.should == "missing parameter: admin_name"

get('/nested_triple', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}})
last_response.status.should == 400
last_response.body.should == "missing parameter: admin_name"

get('/nested_triple', :admin => {:super => {:user => {:first_name => "Billy"}}})
last_response.status.should == 400
last_response.body.should == "missing parameter: admin_name"

get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy"}}})
last_response.status.should == 400
last_response.body.should == "missing parameter: last_name"

get('/nested_triple', :admin => { :admin_name => 'admin', :super => {:user => {:first_name => "Billy", :last_name => "Bob"}}})
last_response.status.should == 200
last_response.body.should == "Nested triple"
end

end

0 comments on commit d021e22

Please sign in to comment.