Class: NxtSchema::Node::Schema

Inherits:
Base
  • Object
show all
Includes:
HasSubNodes
Defined in:
lib/nxt_schema/node/schema.rb

Direct Known Subclasses

Constructor

Instance Attribute Summary

Attributes included from HasSubNodes

#template_store, #value_store

Attributes inherited from Base

#additional_keys_strategy, #all_nodes, #applied, #context, #errors, #input, #level, #locale, #name, #namespace, #options, #parent_node, #root, #schema_errors, #schema_errors_key, #type, #type_system, #validation_errors, #validations, #value

Instance Method Summary collapse

Methods included from HasSubNodes

#dup, #node, #nodes, #required, #schema, #struct

Methods inherited from Base

#add_error, #add_validators, #apply_validations, #build_validations, #default, #leaf?, #maybe, #meta, #parent, #presence, #presence?, #root?, #schema_errors?, #valid?, #validate, #validate_all_nodes, #validate_with, #validation_errors?, #validator, #value_or_default_value

Constructor Details

#initialize(name:, type: NxtSchema::Types::Strict::Hash, parent_node:, **options, &block) ⇒ Schema



4
5
6
7
# File 'lib/nxt_schema/node/schema.rb', line 4

def initialize(name:, type: NxtSchema::Types::Strict::Hash, parent_node:, **options, &block)
  @template_store = TemplateStore.new
  super
end

Instance Method Details

#apply(input, parent_node: self.parent_node, context: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nxt_schema/node/schema.rb', line 9

def apply(input, parent_node: self.parent_node, context: nil)
  self.input = input
  register_node(context)

  self.parent_node = parent_node
  self.schema_errors = { schema_errors_key => [] }
  self.validation_errors = { schema_errors_key => [] }
  self.value_store = {}
  self.value = transform_keys(input)

  if maybe_criteria_applies?(value)
    self.value_store = value
  else
    self.value = value_or_default_value(value)

    unless maybe_criteria_applies?(value)
      self.value = coerce_value(value)

      # TODO: We should not allow additional keys to be present per default?!
      # TODO: Handle this here



      sanitized_keys.each do |key|
        node = template_store[key]

        if allowed_additional_key?(key)
          value_store[key] = input[key]
        elsif node.presence? || input.key?(key)
          node.apply(input[key], parent_node: self, context: context).schema_errors?
          value_store[key] = node.value
          schema_errors[key] = node.schema_errors
          validation_errors[key] = node.validation_errors
        else
          evaluate_optional_option(node, input, key)
        end
      end

      self.value_store = coerce_value(value_store)
      self.value = value_store
    end
  end

  self_without_empty_schema_errors
rescue Dry::Types::ConstraintError, Dry::Types::CoercionError => error
  add_schema_error(error.message)
  self_without_empty_schema_errors
ensure
  mark_as_applied
end

#optional(name, type, **options, &block) ⇒ Object



60
61
62
63
64
# File 'lib/nxt_schema/node/schema.rb', line 60

def optional(name, type, **options, &block)
  raise_invalid_options_presence_options if options[:presence]

  node(name, type, options.merge(optional: true), &block)
end

#present(name, type, **options, &block) ⇒ Object



66
67
68
69
70
# File 'lib/nxt_schema/node/schema.rb', line 66

def present(name, type, **options, &block)
  raise_invalid_options_presence_options if options[:optional]

  node(name, type, options.merge(presence: true), &block)
end