class WeakParameters::ObjectValidator

Attributes

validators[R]

Public Class Methods

new(controller, key, validators, options = {}) click to toggle source
Calls superclass method WeakParameters::BaseValidator::new
# File lib/weak_parameters/object_validator.rb, line 4
def initialize(controller, key, validators, options = {})
  super controller, key, options
  @validators = validators
end

Public Instance Methods

strong_params(*args) click to toggle source
# File lib/weak_parameters/object_validator.rb, line 19
def strong_params(*args)
  super

  strong_values = validators.map do |validator|
    validator.strong_params(*path)
  end.inject(ActionController::Parameters.new, &:merge)
  return {} if strong_values.blank?
  { key => strong_values }
end
validate(*args) click to toggle source
Calls superclass method WeakParameters::BaseValidator#validate
# File lib/weak_parameters/object_validator.rb, line 9
def validate(*args)
  super

  if valid? && exist?
    validators.each do |validator|
      validator.validate(*path)
    end
  end
end

Private Instance Methods

error_message() click to toggle source
# File lib/weak_parameters/object_validator.rb, line 44
def error_message
  keys = path.map { |k| "[#{k.inspect}]" }.join ''
  "params#{keys} must be a valid Hash"
end
valid?() click to toggle source
# File lib/weak_parameters/object_validator.rb, line 31
def valid?
  case
  when required? && nil?
    false
  when exist? && invalid_type?
    false
  when exist? && exceptional?
    false
  else
    true
  end
end