Class: Schemacop::V2::FieldNode

Inherits:
NodeSupportingType show all
Defined in:
lib/schemacop/v2/field_node.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#options

Instance Method Summary collapse

Methods inherited from NodeSupportingType

build, #exec_block, #type

Methods inherited from NodeWithBlock

block_method, #exec_block

Methods inherited from Node

build, class_matches?, clear_klasses, clear_symbols, #exec_block, klass, option, #option, #option?, register, #resolve_type_klass, symbol, symbol_matches?, #type_filter_matches?, #type_label, type_matches?, #type_matches?

Constructor Details

#initialize(name, required, options = {}, &block) ⇒ FieldNode

Returns a new instance of FieldNode.



5
6
7
8
9
10
11
12
13
14
# File 'lib/schemacop/v2/field_node.rb', line 5

def initialize(name, required, options = {}, &block)
  if options.any?
    fail Exceptions::InvalidSchemaError, 'Node does not support options.'
  end

  super({}, &block)

  @name = name
  @required = required
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/schemacop/v2/field_node.rb', line 3

def name
  @name
end

Instance Method Details

#validate(data, collector) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/schemacop/v2/field_node.rb', line 16

def validate(data, collector)
  if !data.key?(name) && @required
    collector.error "Missing key #{name.inspect}."
  end

  collector.path "/#{name}", name, :hash do
    value, default_applied = apply_default!(data[name], collector)

    unless data.key?(name) || default_applied
      next
    end

    super(value, collector)
  end
end