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(name:, type:, parent_node:, **options, &block) ⇒ Base

Returns a new instance of Base.



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

def initialize(name:, type:, parent_node:, **options, &block)
  resolve_name(name)

  @parent_node = parent_node
  @options = options
  @is_root_node = parent_node.nil?
  @root_node = parent_node.nil? ? self : parent_node.root_node
  @path = resolve_path
  @on_evaluators = []
  @maybe_evaluators = []
  @validations = []
  @configuration = block

  resolve_key_transformer
  resolve_context
  resolve_optional_option
  resolve_omnipresent_option
  resolve_type_system
  resolve_type(type)
  resolve_additional_keys_strategy
  application_class # memoize
  configure(&block) if block_given?
end

Instance Attribute Details

#additional_keys_strategyObject

Returns the value of attribute additional_keys_strategy.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def additional_keys_strategy
  @additional_keys_strategy
end

#configurationObject (readonly)

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#key_transformerObject (readonly)

Returns the value of attribute key_transformer.



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

def key_transformer
  @key_transformer
end

#maybe_evaluatorsObject

Returns the value of attribute maybe_evaluators.



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

def maybe_evaluators
  @maybe_evaluators
end

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def name
  @name
end

#on_evaluatorsObject

Returns the value of attribute on_evaluators.



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

def on_evaluators
  @on_evaluators
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def options
  @options
end

#parent_nodeObject

Returns the value of attribute parent_node.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def parent_node
  @parent_node
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#root_nodeObject

Returns the value of attribute root_node.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def root_node
  @root_node
end

#typeObject

Returns the value of attribute type.



28
29
30
# File 'lib/nxt_schema/node/base.rb', line 28

def type
  @type
end

#type_systemObject (readonly)

Returns the value of attribute type_system.



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

def type_system
  @type_system
end

#validationsObject (readonly)

Returns the value of attribute validations.



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

def validations
  @validations
end

Instance Method Details

#apply(input: MissingInput.new, context: self.context, parent: nil, error_key: nil) ⇒ Object



45
46
47
# File 'lib/nxt_schema/node/base.rb', line 45

def apply(input: MissingInput.new, context: self.context, parent: nil, error_key: nil)
  build_application(input: input, context: context, parent: parent, error_key: error_key).call
end

#apply!(input: MissingInput.new, context: self.context, parent: nil, error_key: nil) ⇒ Object



49
50
51
52
53
54
# File 'lib/nxt_schema/node/base.rb', line 49

def apply!(input: MissingInput.new, context: self.context, parent: nil, error_key: nil)
  result = build_application(input: input, context: context, parent: parent, error_key: error_key).call
  return result if parent || result.errors.empty?

  raise NxtSchema::Errors::Invalid.new(result)
end

#build_application(input: MissingInput.new, context: self.context, parent: nil, error_key: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/nxt_schema/node/base.rb', line 56

def build_application(input: MissingInput.new, context: self.context, parent: nil, error_key: nil)
  application_class.new(
    node: self,
    input: input,
    parent: parent,
    context: context,
    error_key: error_key
  )
end

#default(value = NxtSchema::MissingInput.new, &block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/nxt_schema/node/base.rb', line 78

def default(value = NxtSchema::MissingInput.new, &block)
  value = missing_input?(value) ? block : value
  condition = ->(input) { missing_input?(input) || input.nil? }
  on(condition, value)

  self
end

#maybe(value = NxtSchema::MissingInput.new, &block) ⇒ Object



93
94
95
96
97
98
# File 'lib/nxt_schema/node/base.rb', line 93

def maybe(value = NxtSchema::MissingInput.new, &block)
  value = missing_input?(value) ? block : value
  maybe_evaluators << MaybeEvaluator.new(value: value)

  self
end

#omnipresent?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/nxt_schema/node/base.rb', line 74

def omnipresent?
  @omnipresent
end

#on(condition, value = NxtSchema::MissingInput.new, &block) ⇒ Object



86
87
88
89
90
91
# File 'lib/nxt_schema/node/base.rb', line 86

def on(condition, value = NxtSchema::MissingInput.new, &block)
  value = missing_input?(value) ? block : value
  on_evaluators << OnEvaluator.new(condition: condition, value: value)

  self
end

#optional?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/nxt_schema/node/base.rb', line 70

def optional?
  @optional
end

#root_node?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/nxt_schema/node/base.rb', line 66

def root_node?
  @is_root_node
end

#validate(key = NxtSchema::MissingInput.new, *args, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/nxt_schema/node/base.rb', line 100

def validate(key = NxtSchema::MissingInput.new, *args, &block)
  # TODO: This does not really work with all kinds of chaining combinations yet!

  validator = if key.is_a?(Symbol)
                validator(key, *args)
              elsif key.respond_to?(:call)
                key
              elsif block_given?
                if key.is_a?(NxtSchema::MissingInput)
                  block
                else
                  configure(&block)
                end
              else
                raise ArgumentError, "Don't know how to resolve validator from: #{key} with: #{args} #{block}"
              end

  register_validator(validator)

  self
end

#validate_with(&block) ⇒ Object



122
123
124
125
# File 'lib/nxt_schema/node/base.rb', line 122

def validate_with(&block)
  proxy = ->(node) { NxtSchema::Validator::ValidateWithProxy.new(node).validate(&block) }
  register_validator(proxy)
end