Class: NxtSchema::Template::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nxt_schema/template/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/template/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
  node_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/template/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/template/base.rb', line 35

def configuration
  @configuration
end

#contextObject

Returns the value of attribute context.



35
36
37
# File 'lib/nxt_schema/template/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/template/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/template/base.rb', line 35

def maybe_evaluators
  @maybe_evaluators
end

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#on_evaluatorsObject

Returns the value of attribute on_evaluators.



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

def on_evaluators
  @on_evaluators
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#parent_nodeObject

Returns the value of attribute parent_node.



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

def parent_node
  @parent_node
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#root_nodeObject

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

#typeObject

Returns the value of attribute type.



28
29
30
# File 'lib/nxt_schema/template/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/template/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/template/base.rb', line 35

def validations
  @validations
end

Instance Method Details

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



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

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

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



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

def apply!(input: Undefined.new, context: self.context, parent: nil, error_key: nil)
  result = build_node(input: input, context: context, parent: parent, error_key: error_key).call
  return result if parent

  raise NxtSchema::Errors::Invalid.new(result) if result.errors.any?

  result.output
end

#build_node(input: Undefined.new, context: self.context, parent: nil, error_key: nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/nxt_schema/template/base.rb', line 58

def build_node(input: Undefined.new, context: self.context, parent: nil, error_key: nil)
  node_class.new(
    node: self,
    input: input,
    parent: parent,
    context: context,
    error_key: error_key
  )
end

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



80
81
82
83
84
85
86
# File 'lib/nxt_schema/template/base.rb', line 80

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

  self
end

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



95
96
97
98
99
100
# File 'lib/nxt_schema/template/base.rb', line 95

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

  self
end

#omnipresent?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/nxt_schema/template/base.rb', line 76

def omnipresent?
  @omnipresent
end

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



88
89
90
91
92
93
# File 'lib/nxt_schema/template/base.rb', line 88

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

  self
end

#optional?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/nxt_schema/template/base.rb', line 72

def optional?
  @optional
end

#root_node?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/nxt_schema/template/base.rb', line 68

def root_node?
  @is_root_node
end

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



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

def validate(key = NxtSchema::Undefined.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::Undefined)
      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



124
125
126
127
# File 'lib/nxt_schema/template/base.rb', line 124

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