-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove deprecated except and proc in values validator #2501
Merged
dblock
merged 5 commits into
ruby-grape:master
from
ericproulx:remove_except_in_values_validator
Sep 27, 2024
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,12 +3,14 @@ Upgrading Grape | |||||
|
||||||
### Upgrading to >= 2.3.0 | ||||||
|
||||||
#### Remove deprecated methods | ||||||
|
||||||
Deprecated `file` method has been removed. Use `send_file` or `stream`. | ||||||
#### Remove deprecated methods and options | ||||||
|
||||||
- Deprecated `file` method has been removed. Use `send_file` or `stream`. | ||||||
See [#2500](https://github.com/ruby-grape/grape/pull/2500) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
- `except` and `proc` options have been removed from the `values` validator. Use `except validator` or assign `proc` directly to `values`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
See [#2501](https://github.com/ruby-grape/grape/pull/2501) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
### Upgrading to >= 2.2.0 | ||||||
|
||||||
### `Length` validator | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,22 +5,7 @@ module Validations | |
module Validators | ||
class ValuesValidator < Base | ||
def initialize(attrs, options, required, scope, **opts) | ||
if options.is_a?(Hash) | ||
@excepts = options[:except] | ||
@values = options[:value] | ||
@proc = options[:proc] | ||
|
||
Grape.deprecator.warn('The values validator except option is deprecated. Use the except validator instead.') if @excepts | ||
|
||
raise ArgumentError, 'proc must be a Proc' if @proc && [email protected]_a?(Proc) | ||
|
||
Grape.deprecator.warn('The values validator proc option is deprecated. The lambda expression can now be assigned directly to values.') if @proc | ||
else | ||
@excepts = nil | ||
@values = nil | ||
@proc = nil | ||
@values = options | ||
end | ||
@values = options.is_a?(Hash) ? options[:value] : options | ||
super | ||
end | ||
|
||
|
@@ -34,54 +19,29 @@ def validate_param!(attr_name, params) | |
# don't forget that +false.blank?+ is true | ||
return if val != false && val.blank? && @allow_blank | ||
|
||
param_array = val.nil? ? [nil] : Array.wrap(val) | ||
|
||
raise validation_exception(attr_name, except_message) \ | ||
unless check_excepts(param_array) | ||
|
||
raise validation_exception(attr_name, message(:values)) \ | ||
unless check_values(param_array, attr_name) | ||
return if check_values?(val, attr_name) | ||
|
||
raise validation_exception(attr_name, message(:values)) \ | ||
if @proc && !validate_proc(@proc, param_array) | ||
raise Grape::Exceptions::Validation.new( | ||
params: [@scope.full_name(attr_name)], | ||
message: message(:values) | ||
) | ||
end | ||
|
||
private | ||
|
||
def check_values(param_array, attr_name) | ||
def check_values?(val, attr_name) | ||
values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values | ||
return true if values.nil? | ||
|
||
param_array = val.nil? ? [nil] : Array.wrap(val) | ||
return param_array.all? { |param| values.include?(param) } unless values.is_a?(Proc) | ||
|
||
begin | ||
return param_array.all? { |param| values.call(param) } if values.is_a? Proc | ||
param_array.all? { |param| values.call(param) } | ||
rescue StandardError => e | ||
warn "Error '#{e}' raised while validating attribute '#{attr_name}'" | ||
return false | ||
false | ||
end | ||
param_array.all? { |param| values.include?(param) } | ||
end | ||
|
||
def check_excepts(param_array) | ||
excepts = @excepts.is_a?(Proc) ? @excepts.call : @excepts | ||
return true if excepts.nil? | ||
|
||
param_array.none? { |param| excepts.include?(param) } | ||
end | ||
|
||
def validate_proc(proc, param_array) | ||
case proc.arity | ||
when 0 | ||
param_array.all? { |_param| proc.call } | ||
when 1 | ||
param_array.all? { |param| proc.call(param) } | ||
else | ||
raise ArgumentError, 'proc arity must be 0 or 1' | ||
end | ||
end | ||
|
||
def except_message | ||
options = instance_variable_get(:@option) | ||
options_key?(:except_message) ? options[:except_message] : message(:except_values) | ||
end | ||
|
||
def required_for_root_scope? | ||
|
@@ -92,10 +52,6 @@ def required_for_root_scope? | |
|
||
scope.root? | ||
end | ||
|
||
def validation_exception(attr_name, message) | ||
Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message) | ||
end | ||
end | ||
end | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.