Class: Grape::Validations::Validators::ValuesValidator

Inherits:
Base
  • Object
show all
Defined in:
lib/grape/validations/validators/values_validator.rb

Instance Attribute Summary

Attributes inherited from Base

#attrs

Instance Method Summary collapse

Methods inherited from Base

#fail_fast?, inherited, #message, #options_key?, #validate, #validate!

Constructor Details

#initialize(attrs, options, required, scope, **opts) ⇒ ValuesValidator

Returns a new instance of ValuesValidator.



7
8
9
10
# File 'lib/grape/validations/validators/values_validator.rb', line 7

def initialize(attrs, options, required, scope, **opts)
  @values = options.is_a?(Hash) ? options[:value] : options
  super
end

Instance Method Details

#validate_param!(attr_name, params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/grape/validations/validators/values_validator.rb', line 12

def validate_param!(attr_name, params)
  return unless params.is_a?(Hash)

  val = params[attr_name]

  return if val.nil? && !required_for_root_scope?

  # don't forget that +false.blank?+ is true
  return if val != false && val.blank? && @allow_blank

  return if check_values?(val, attr_name)

  raise Grape::Exceptions::Validation.new(
    params: [@scope.full_name(attr_name)],
    message: message(:values)
  )
end