Class: Schemacop::V3::ReferenceNode

Inherits:
Node
  • Object
show all
Defined in:
lib/schemacop/v3/reference_node.rb

Instance Attribute Summary

Attributes inherited from Node

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

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

Constructor Details

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

Class Method Details

.allowed_optionsObject



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

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

.create(path, **options, &block) ⇒ Object



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

def self.create(path, **options, &block)
  options[:path] = path
  super(**options, &block)
end

Instance Method Details

#_validate(data, result:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/schemacop/v3/reference_node.rb', line 21

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

  # Lookup schema #
  node = target
  fail "Schema #{@path.to_s.inspect} not found." unless node

  # Validate schema #
  node._validate(super_data, result: result)
end

#as_jsonObject



13
14
15
16
17
18
19
# File 'lib/schemacop/v3/reference_node.rb', line 13

def as_json
  if context.swagger_json?
    process_json([], '$ref': "#/components/schemas/#{@path}")
  else
    process_json([], '$ref': "#/definitions/#{@path}")
  end
end

#cast(data) ⇒ Object



37
38
39
40
# File 'lib/schemacop/v3/reference_node.rb', line 37

def cast(data)
  data = default if data.nil?
  return target.cast(data)
end

#targetObject



33
34
35
# File 'lib/schemacop/v3/reference_node.rb', line 33

def target
  schemas[@path] || Schemacop.context.schemas[@path] || GlobalContext.schema_for(@path)
end

#used_external_schemasObject



42
43
44
45
46
47
48
49
50
# File 'lib/schemacop/v3/reference_node.rb', line 42

def used_external_schemas
  target_children_schema = target.used_external_schemas

  if schemas.include?(@path)
    return target_children_schema
  else
    return [@path] + target_children_schema
  end
end