class NxtSchema::Node::Schema

Public Instance Methods

call() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 4
def call
  apply_on_evaluators
  child_nodes # build nodes here so we can access them even when invalid
  return self if maybe_evaluator_applies?

  coerce_input
  return self unless valid?

  flag_missing_keys
  apply_additional_keys_strategy

  child_nodes.each do |key, child|
    current_node = child.call

    if !current_node.valid?
      merge_errors(current_node)
    else
      output[key] = current_node.output
    end
  end

  transform_keys
  register_as_coerced_when_no_errors
  run_validations
  self
end

Private Instance Methods

additional_keys() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 46
def additional_keys
  @additional_keys ||= input.keys - keys
end
additional_keys?() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 54
def additional_keys?
  additional_keys.any?
end
allow_additional_keys?() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 79
def allow_additional_keys?
  node.additional_keys_strategy == :allow
end
apply_additional_keys_strategy() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 62
def apply_additional_keys_strategy
  return if allow_additional_keys?
  return unless additional_keys?

  if restrict_additional_keys?
    add_schema_error("Additional keys are not allowed: #{additional_keys}")
  elsif reject_additional_keys?
    self.output = output.except(*additional_keys)
  end
end
build_child_node(key) click to toggle source
# File lib/nxt_schema/node/schema.rb, line 101
def build_child_node(key)
  sub_node = node.sub_nodes[key]
  return unless sub_node.present?

  value = input_has_key?(input, key) ? input[key] : Undefined.new
  sub_node.build_node(input: value, context: context, parent: self)
end
child_nodes() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 91
def child_nodes
  @child_nodes ||= begin
    keys.inject({}) do |acc, key|
      child_node = build_child_node(key)
      acc[key] = child_node if child_node.present?
      acc
    end
  end
end
flag_missing_keys() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 73
def flag_missing_keys
  return if missing_keys.empty?

  add_schema_error("The following keys are missing: #{missing_keys}")
end
input_has_key?(input, key) click to toggle source
# File lib/nxt_schema/node/schema.rb, line 109
def input_has_key?(input, key)
  input.respond_to?(:key?) && input.key?(key)
end
keys() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 42
def keys
  @keys ||= node.sub_nodes.reject { |key, _| optional_and_not_given_key?(key) }.keys
end
missing_keys() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 58
def missing_keys
  @missing_keys ||= node.sub_nodes.reject { |_, node| node.omnipresent? || node.optional? }.keys - input.keys
end
optional_and_not_given_key?(key) click to toggle source
# File lib/nxt_schema/node/schema.rb, line 50
def optional_and_not_given_key?(key)
  node.sub_nodes[key].optional? && !input.key?(key)
end
reject_additional_keys?() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 83
def reject_additional_keys?
  node.additional_keys_strategy == :reject
end
restrict_additional_keys?() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 87
def restrict_additional_keys?
  node.additional_keys_strategy == :restrict
end
transform_keys() click to toggle source
# File lib/nxt_schema/node/schema.rb, line 35
def transform_keys
  transformer = node.key_transformer
  return unless transformer && output.respond_to?(:transform_keys!)

  output.transform_keys!(&transformer)
end