Class: Schemacop::V3::OneOfNode

Inherits:
CombinationNode show all
Defined in:
lib/schemacop/v3/one_of_node.rb

Instance Attribute Summary

Attributes inherited from Node

#as, #default, #description, #name, #options, #parent, #require_key, #title

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CombinationNode

#add_child, #as_json, dsl_methods, #init

Methods inherited from Node

#allowed_types, #as_json, #children, create, #create, dsl_methods, #dsl_node, #dsl_scm, #init, #initialize, #require_key?, #required?, resolve_class, #schemas, supports_children, supports_children_options, #used_external_schemas, #validate

Constructor Details

This class inherits a constructor from Schemacop::V3::Node

Class Method Details

.allowed_optionsObject



8
9
10
# File 'lib/schemacop/v3/one_of_node.rb', line 8

def self.allowed_options
  super + %i[treat_blank_as_nil]
end

Instance Method Details

#_validate(data, result:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/schemacop/v3/one_of_node.rb', line 26

def _validate(data, result:)
  if options[:treat_blank_as_nil] && data.blank? && !data.is_a?(FalseClass)
    data = nil
  end

  super_data = super(data, result: result)
  return if super_data.nil?

  matches = matches(super_data)

  if matches.size == 1
    matches.first._validate(super_data, result: result)
  else
    result.error <<~PLAIN.strip
      Matches #{matches.size} schemas but should match exactly 1:
      #{schema_messages(data).join("\n")}
    PLAIN
  end
end

#cast(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/schemacop/v3/one_of_node.rb', line 12

def cast(value)
  item = match(value)

  unless item
    if options[:treat_blank_as_nil] && value.blank? && !value.is_a?(FalseClass)
      return nil
    else
      return value
    end
  end

  return item.cast(value)
end

#typeObject



4
5
6
# File 'lib/schemacop/v3/one_of_node.rb', line 4

def type
  :oneOf
end

#validate_selfObject



46
47
48
49
50
# File 'lib/schemacop/v3/one_of_node.rb', line 46

def validate_self
  if @items.size < 2
    fail 'Node "one_of" makes only sense with at least 2 items.'
  end
end