Skip to content

Commit

Permalink
slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
asross committed Jul 23, 2013
1 parent ac72a48 commit 01f3b81
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ def initialize(opts, &block)
configure_declared_params
end

def optional?
@optional
def should_validate?(parameters)
return false if @optional && params(parameters).blank?
return true if parent.nil?
parent.should_validate?(parameters)
end

def requires(*attrs, &block)
Expand Down
8 changes: 1 addition & 7 deletions lib/grape/validations/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ module Grape
module Validations
class PresenceValidator < Validator
def validate!(params)
# If this validator is for a parameter inside an optional group
# for which params is blank, then we should skip validation
scope = @scope
while scope
return if scope.optional? && scope.params(params).blank?
scope = scope.parent
end
return unless @scope.should_validate?(params)
super
end

Expand Down

0 comments on commit 01f3b81

Please sign in to comment.