Class: Schemacop::V3::ReferenceNode

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

Constant Summary collapse

RFC6901_ESCAPE =
{ '~' => '~0', '/' => '~1' }.freeze

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 Node

#allowed_types, #cast_str_wrapper?, #children, #create, dsl_methods, #dsl_node, #dsl_scm, #initialize, #require_key?, #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



6
7
8
# File 'lib/schemacop/v3/reference_node.rb', line 6

def self.allowed_options
  super + i[path]
end

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



10
11
12
13
# File 'lib/schemacop/v3/reference_node.rb', line 10

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

Instance Method Details

#_validate(data, result:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/schemacop/v3/reference_node.rb', line 30

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/schemacop/v3/reference_node.rb', line 15

def as_json
  if context.swagger_json?
    # OpenAPI schema names must match ^[a-zA-Z0-9._-]+$, so we replace
    # all non-conforming characters: `/` and `~` become `.`.
    sanitized = @path.to_s.gsub(%r{[~/]}, '.')
    process_json([], '$ref': "#/components/schemas/#{sanitized}")
  else
    # Plain JSON Schema: use RFC 6901 JSON Pointer escaping in $ref.
    # gsub with a hash is a single-pass substitution, so the RFC 6901
    # order-of-escaping concern (~ before /) does not apply here.
    escaped = @path.to_s.gsub(%r{[~/]}, RFC6901_ESCAPE)
    process_json([], '$ref': "#/definitions/#{escaped}")
  end
end

#cast(data) ⇒ Object



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

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

#targetObject



42
43
44
# File 'lib/schemacop/v3/reference_node.rb', line 42

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

#used_external_schemas(encountered_nodes = Set.new) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/schemacop/v3/reference_node.rb', line 51

def used_external_schemas(encountered_nodes = Set.new)
  if encountered_nodes.include?(self)
    return []
  end

  target_children_schema = target.used_external_schemas(encountered_nodes + [self])

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