class ParamsChecker::ParamChecker::NestedHashsChecker

Attributes

class[RW]
context[RW]
key[RW]
params[RW]
schema[RW]

Public Class Methods

new(key = '', schema = {}, params = {}, context = {}) click to toggle source
# File lib/params_checker/param_checker.rb, line 181
def initialize(key = '', schema = {}, params = {}, context = {})
  @key = key
  @schema = schema
  @params = params
  @context = context
end

Public Instance Methods

add_errors() click to toggle source
# File lib/params_checker/param_checker.rb, line 194
def add_errors
  all_errors = params[key].map do |nested_hash|
    get_error(nested_hash)
  end
  return true if all_errors.all?(&:nil?)

  errors.add(key, all_errors)
end
add_field_error(message = '') click to toggle source
# File lib/params_checker/param_checker.rb, line 235
def add_field_error(message = '')
  errors.add(key, message)
end
call() click to toggle source
# File lib/params_checker/param_checker.rb, line 188
def call
  return nil if schema[key][:allow_nil] && params[key].nil?

  check_type && add_errors && formatted_nested_hashs
end
check_type() click to toggle source
# File lib/params_checker/param_checker.rb, line 229
def check_type
  valid = params[key].is_a?(Array)
  add_field_error("This field's type must be array.") unless valid
  valid
end
formatted_nested_hash(nested_hash) click to toggle source
# File lib/params_checker/param_checker.rb, line 220
def formatted_nested_hash(nested_hash)
  cmd = schema[key][:class].call(
    params: nested_hash,
    context: context,
    is_outest_hash: false
  )
  cmd.result
end
formatted_nested_hashs() click to toggle source
# File lib/params_checker/param_checker.rb, line 214
def formatted_nested_hashs
  params[key].map do |nested_hash|
    formatted_nested_hash(nested_hash)
  end
end
get_error(nested_hash) click to toggle source
# File lib/params_checker/param_checker.rb, line 203
def get_error(nested_hash)
  cmd = schema[key][:class].call(
    params: nested_hash,
    context: context,
    is_outest_hash: false
  )
  return nil if cmd.success?

  cmd.errors[:errors][0][:field_errors]
end