Skip to content

Commit 503b809

Browse files
author
Joe Faber
committed
Fix for given block issues (#1433)
Move should_validate? check to validator base class so it gets called for all validators. This replaces the custom code in allow_blank, and adds it to validators where it didn't previously get called (like coerce).
1 parent 30720f2 commit 503b809

File tree

5 files changed

+2
-20
lines changed

5 files changed

+2
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#### Fixes
1111

12+
* [#1498](https://github.com/ruby-grape/grape/pull/1498): Skip validations in inactive given blocks - [@jlfaber](https://github.com/jlfaber).
1213
* [#1479](https://github.com/ruby-grape/grape/pull/1479): Support inserting middleware before/after anonymous classes in the middleware stack - [@rosa](https://github.com/rosa).
1314
* [#1488](https://github.com/ruby-grape/grape/pull/1488): Ensure calling before filters when receiving OPTIONS request - [@namusyaka](https://github.com/namusyaka), [@jlfaber](https://github.com/jlfaber).
1415
* [#1493](https://github.com/ruby-grape/grape/pull/1493): Coercion and lambda fails params validation - [@jonmchan](https://github.com/jonmchan).

lib/grape/validations/validators/allow_blank.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ def validate_param!(attr_name, params)
77
value = params[attr_name]
88
value = value.strip if value.respond_to?(:strip)
99

10-
key_exists = params.key?(attr_name)
11-
12-
should_validate = if @scope.root?
13-
# root scope. validate if it's a required param. if it's optional, validate only if key exists in hash
14-
@required || key_exists
15-
else # nested scope
16-
(@required && params.present?) ||
17-
# optional param but key inside scoping element exists
18-
(!@required && params.key?(attr_name))
19-
end
20-
21-
return unless should_validate
22-
2310
return if false == value || value.present?
2411

2512
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:blank)

lib/grape/validations/validators/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def initialize(attrs, options, required, scope)
2424
# @raise [Grape::Exceptions::Validation] if validation failed
2525
# @return [void]
2626
def validate(request)
27+
return unless @scope.should_validate?(request.params)
2728
validate!(request.params)
2829
end
2930

lib/grape/validations/validators/default.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ def validate_param!(attr_name, params)
1818
end
1919

2020
def validate!(params)
21-
return unless @scope.should_validate?(params)
22-
2321
attrs = AttributesIterator.new(self, @scope, params)
2422
attrs.each do |resource_params, attr_name|
2523
if resource_params.is_a?(Hash) && resource_params[attr_name].nil?

lib/grape/validations/validators/presence.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
module Grape
22
module Validations
33
class PresenceValidator < Base
4-
def validate!(params)
5-
return unless @scope.should_validate?(params)
6-
super
7-
end
8-
94
def validate_param!(attr_name, params)
105
return if params.respond_to?(:key?) && params.key?(attr_name)
116
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:presence)

0 commit comments

Comments
 (0)