Skip to content

Commit 7f0595c

Browse files
authored
Remove deprecated except and proc in values validator (#2501)
1 parent 75e8c5b commit 7f0595c

File tree

3 files changed

+18
-59
lines changed

3 files changed

+18
-59
lines changed

CHANGELOG.md

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

55
* [#2497](https://github.com/ruby-grape/grape/pull/2497): Update RuboCop to 1.66.1 - [@ericproulx](https://github.com/ericproulx).
66
* [#2500](https://github.com/ruby-grape/grape/pull/2500): Remove deprecated `file` method - [@ericproulx](https://github.com/ericproulx).
7+
* [#2501](https://github.com/ruby-grape/grape/pull/2501): Remove deprecated `except` and `proc` options in values validator - [@ericproulx](https://github.com/ericproulx).
78
* Your contribution here.
89

910
#### Fixes

UPGRADING.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ Upgrading Grape
33

44
### Upgrading to >= 2.3.0
55

6-
#### Remove deprecated methods
6+
#### Remove Deprecated Methods and Options
77

8-
Deprecated `file` method has been removed. Use `send_file` or `stream`.
8+
- Deprecated `file` method has been removed. Use `send_file` or `stream`.
9+
See [#2500](https://github.com/ruby-grape/grape/pull/2500) for more information.
910

10-
See [#2500](https://github.com/ruby-grape/grape/pull/2500)
11+
- The `except` and `proc` options have been removed from the `values` validator. Use `except_values` validator or assign `proc` directly to `values`.
12+
See [#2501](https://github.com/ruby-grape/grape/pull/2501) for more information.
1113

1214
### Upgrading to >= 2.2.0
1315

lib/grape/validations/validators/values_validator.rb

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,7 @@ module Validations
55
module Validators
66
class ValuesValidator < Base
77
def initialize(attrs, options, required, scope, **opts)
8-
if options.is_a?(Hash)
9-
@excepts = options[:except]
10-
@values = options[:value]
11-
@proc = options[:proc]
12-
13-
Grape.deprecator.warn('The values validator except option is deprecated. Use the except validator instead.') if @excepts
14-
15-
raise ArgumentError, 'proc must be a Proc' if @proc && !@proc.is_a?(Proc)
16-
17-
Grape.deprecator.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc
18-
else
19-
@excepts = nil
20-
@values = nil
21-
@proc = nil
22-
@values = options
23-
end
8+
@values = options.is_a?(Hash) ? options[:value] : options
249
super
2510
end
2611

@@ -34,54 +19,29 @@ def validate_param!(attr_name, params)
3419
# don't forget that +false.blank?+ is true
3520
return if val != false && val.blank? && @allow_blank
3621

37-
param_array = val.nil? ? [nil] : Array.wrap(val)
38-
39-
raise validation_exception(attr_name, except_message) \
40-
unless check_excepts(param_array)
41-
42-
raise validation_exception(attr_name, message(:values)) \
43-
unless check_values(param_array, attr_name)
22+
return if check_values?(val, attr_name)
4423

45-
raise validation_exception(attr_name, message(:values)) \
46-
if @proc && !validate_proc(@proc, param_array)
24+
raise Grape::Exceptions::Validation.new(
25+
params: [@scope.full_name(attr_name)],
26+
message: message(:values)
27+
)
4728
end
4829

4930
private
5031

51-
def check_values(param_array, attr_name)
32+
def check_values?(val, attr_name)
5233
values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values
5334
return true if values.nil?
5435

36+
param_array = val.nil? ? [nil] : Array.wrap(val)
37+
return param_array.all? { |param| values.include?(param) } unless values.is_a?(Proc)
38+
5539
begin
56-
return param_array.all? { |param| values.call(param) } if values.is_a? Proc
40+
param_array.all? { |param| values.call(param) }
5741
rescue StandardError => e
5842
warn "Error '#{e}' raised while validating attribute '#{attr_name}'"
59-
return false
43+
false
6044
end
61-
param_array.all? { |param| values.include?(param) }
62-
end
63-
64-
def check_excepts(param_array)
65-
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts
66-
return true if excepts.nil?
67-
68-
param_array.none? { |param| excepts.include?(param) }
69-
end
70-
71-
def validate_proc(proc, param_array)
72-
case proc.arity
73-
when 0
74-
param_array.all? { |_param| proc.call }
75-
when 1
76-
param_array.all? { |param| proc.call(param) }
77-
else
78-
raise ArgumentError, 'proc arity must be 0 or 1'
79-
end
80-
end
81-
82-
def except_message
83-
options = instance_variable_get(:@option)
84-
options_key?(:except_message) ? options[:except_message] : message(:except_values)
8545
end
8646

8747
def required_for_root_scope?
@@ -92,10 +52,6 @@ def required_for_root_scope?
9252

9353
scope.root?
9454
end
95-
96-
def validation_exception(attr_name, message)
97-
Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
98-
end
9955
end
10056
end
10157
end

0 commit comments

Comments
 (0)