Class: Grape::Validations::Validators::ExceptValuesValidator
- Defined in:
- lib/grape/validations/validators/except_values_validator.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(attrs, options, required, scope, opts) ⇒ ExceptValuesValidator
constructor
A new instance of ExceptValuesValidator.
- #validate_param!(attr_name, params) ⇒ Object
Methods inherited from Base
default_message_key, #fail_fast?, inherited, new, #validate, #validate!
Constructor Details
#initialize(attrs, options, required, scope, opts) ⇒ ExceptValuesValidator
Returns a new instance of ExceptValuesValidator.
9 10 11 12 13 14 15 16 17 |
# File 'lib/grape/validations/validators/except_values_validator.rb', line 9 def initialize(attrs, , required, scope, opts) super except = option_value raise ArgumentError, 'except_values Proc must have arity of zero (use values: with a one-arity predicate for per-element checks)' if except.is_a?(Proc) && !except.arity.zero? # Zero-arity procs (e.g. -> { User.pluck(:role) }) must be called per-request, # not at definition time, so they are wrapped in a lambda to defer execution. @excepts_call = except.is_a?(Proc) ? except : -> { except } end |
Instance Method Details
#validate_param!(attr_name, params) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/grape/validations/validators/except_values_validator.rb', line 19 def validate_param!(attr_name, params) return unless hash_like?(params) && params.key?(attr_name) excepts = @excepts_call.call return if excepts.nil? param_array = params[attr_name].nil? ? [nil] : Array.wrap(params[attr_name]) return if param_array.none? { |param| excepts.include?(param) } validation_error!(attr_name) end |