Class: NxtSchema::Node::Base

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

Direct Known Subclasses

AnyOf, Collection, Leaf, Schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, input: Undefined.new, parent:, context:, error_key:) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nxt_schema/node/base.rb', line 4

def initialize(node:, input: Undefined.new, parent:, context:, error_key:)
  @node = node
  @input = input
  @parent = parent
  @output = nil
  @error_key = error_key
  @context = context || parent&.context
  @coerced = false
  @coerced_nodes = parent&.coerced_nodes || []
  @is_root = parent.nil?
  @root = parent.nil? ? self : parent.root
  @errors = ErrorStore.new(self)
  @locale = node.options.fetch(:locale) { parent&.locale || 'en' }.to_s

  @index = error_key
  resolve_error_key(error_key)
end

Instance Attribute Details

#coercedObject

Returns the value of attribute coerced.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def coerced
  @coerced
end

#coerced_nodesObject (readonly)

Returns the value of attribute coerced_nodes.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def coerced_nodes
  @coerced_nodes
end

#contextObject (readonly)

Returns the value of attribute context.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def context
  @context
end

#error_keyObject (readonly)

Returns the value of attribute error_key.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def error_key
  @error_key
end

#errorsObject (readonly)

Returns the value of attribute errors.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def errors
  @errors
end

#indexObject (readonly)

Returns the value of attribute index.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def index
  @index
end

#inputObject

Returns the value of attribute input.



22
23
24
# File 'lib/nxt_schema/node/base.rb', line 22

def input
  @input
end

#localeObject (readonly)

Returns the value of attribute locale.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def locale
  @locale
end

#nodeObject

Returns the value of attribute node.



22
23
24
# File 'lib/nxt_schema/node/base.rb', line 22

def node
  @node
end

#outputObject

Returns the value of attribute output.



22
23
24
# File 'lib/nxt_schema/node/base.rb', line 22

def output
  @output
end

#parentObject (readonly)

Returns the value of attribute parent.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def parent
  @parent
end

#rootObject

Returns the value of attribute root.



23
24
25
# File 'lib/nxt_schema/node/base.rb', line 23

def root
  @root
end

Instance Method Details

#add_error(error) ⇒ Object



39
40
41
# File 'lib/nxt_schema/node/base.rb', line 39

def add_error(error)
  errors.add_validation_error(message: error)
end

#add_schema_error(error) ⇒ Object



43
44
45
# File 'lib/nxt_schema/node/base.rb', line 43

def add_schema_error(error)
  errors.add_schema_error(message: error)
end

#callObject

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/nxt_schema/node/base.rb', line 25

def call
  raise NotImplementedError, 'Implement this in our sub class'
end

#merge_errors(node) ⇒ Object



47
48
49
# File 'lib/nxt_schema/node/base.rb', line 47

def merge_errors(node)
  errors.merge_errors(node)
end

#root?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/nxt_schema/node/base.rb', line 31

def root?
  @is_root
end

#run_validationsObject



51
52
53
54
55
56
57
58
# File 'lib/nxt_schema/node/base.rb', line 51

def run_validations
  return false unless coerced?

  node.validations.each do |validation|
    args = [self, input]
    validation.call(*args.take(validation.arity))
  end
end

#up(levels = 1) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/nxt_schema/node/base.rb', line 60

def up(levels = 1)
  0.upto(levels - 1).inject(self) do |acc, _|
    parent = acc.send(:parent)
    break acc unless parent

    parent
  end
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/nxt_schema/node/base.rb', line 35

def valid?
  errors.empty?
end