Class: NxtSchema::Node::Leaf

Inherits:
Base
  • Object
show all
Defined in:
lib/nxt_schema/node/leaf.rb

Instance Attribute Summary

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 inherited from Base

#add_error, #add_validators, #apply_validations, #build_validations, #default, #maybe, #meta, #optional, #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:, parent_node:, **options, &block) ⇒ Leaf

Returns a new instance of Leaf.



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

def initialize(name:, type:, parent_node:, **options, &block)
  super
  @type = resolve_type(type)
end

Instance Method Details

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nxt_schema/node/leaf.rb', line 13

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 => [] }

  if maybe_criteria_applies?(input)
    self.value = input
  else
    self.value = value_or_default_value(input)
    self.value = coerce_value(value) unless maybe_criteria_applies?(value)
  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

#leaf?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/nxt_schema/node/leaf.rb', line 9

def leaf?
  true
end