class ParamsChecker::ParamChecker::NestedHashChecker

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 141
def initialize(key = '', schema = {}, params = {}, context = {})
  @key = key
  @schema = schema
  @params = params
  @context = context
end

Public Instance Methods

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

  check_type && formatted_nested_hash
end
check_type() click to toggle source
# File lib/params_checker/param_checker.rb, line 161
def check_type
  valid = params[key].is_a?(ActionController::Parameters) || params[key].is_a?(Hash)
  add_field_error("This field's type must be object or ActionController::Parameters.") unless valid

  valid
end
formatted_nested_hash() click to toggle source
# File lib/params_checker/param_checker.rb, line 154
def formatted_nested_hash
  cmd = schema[key][:class].call(params: params[key], context: context, is_outest_hash: false)
  return cmd.result if cmd.success?

  add_nested_hash_error(cmd.errors)
end