forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature ruby-grape#1616 - split out except_values validator
Deprecate except and proc options of values validator. Values validator now distinguishes between arity zero and arity one Procs. Check default(s) against the except_values list. Handle case where default is an array and values / except_values are ranges.
- Loading branch information
Joe Faber
committed
Apr 26, 2017
1 parent
fc5300d
commit 079fce0
Showing
9 changed files
with
343 additions
and
52 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Grape | ||
module Validations | ||
class ExceptValuesValidator < Base | ||
def initialize(attrs, options, required, scope, opts = {}) | ||
@except = options.is_a?(Hash) ? options[:value] : options | ||
super | ||
end | ||
|
||
def validate_param!(attr_name, params) | ||
return unless params.respond_to?(:key?) && params.key?(attr_name) | ||
|
||
excepts = @except.is_a?(Proc) ? @except.call : @except | ||
return if excepts.nil? | ||
|
||
param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name]) | ||
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: message(:except_values) if param_array.any? { |param| excepts.include?(param) } | ||
end | ||
end | ||
end | ||
end |
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
Oops, something went wrong.