class Grape::Validations::Validators::ValuesValidator

Public Class Methods

new(attrs, options, required, scope, **opts) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 7
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 && !@proc.is_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
  super
end

Public Instance Methods

validate_param!(attr_name, params) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 27
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

  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)

  raise validation_exception(attr_name, message(:values)) \
    if @proc && !validate_proc(@proc, param_array)
end

Private Instance Methods

check_excepts(param_array) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 64
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
check_values(param_array, attr_name) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 51
def check_values(param_array, attr_name)
  values = @values.is_a?(Proc) && @values.arity.zero? ? @values.call : @values
  return true if values.nil?

  begin
    return param_array.all? { |param| values.call(param) } if values.is_a? Proc
  rescue StandardError => e
    warn "Error '#{e}' raised while validating attribute '#{attr_name}'"
    return false
  end
  param_array.all? { |param| values.include?(param) }
end
except_message() click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 82
def except_message
  options = instance_variable_get(:@option)
  options_key?(:except_message) ? options[:except_message] : message(:except_values)
end
required_for_root_scope?() click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 87
def required_for_root_scope?
  return false unless @required

  scope = @scope
  scope = scope.parent while scope.lateral?

  scope.root?
end
validate_proc(proc, param_array) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 71
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
validation_exception(attr_name, message) click to toggle source
# File lib/grape/validations/validators/values_validator.rb, line 96
def validation_exception(attr_name, message)
  Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
end