Class: JSONSkooma::Keywords::Core::DynamicRef

Inherits:
Base
  • Object
show all
Defined in:
lib/json_skooma/keywords/core/dynamic_ref.rb

Instance Attribute Summary

Attributes inherited from Base

#json, #parent_schema

Instance Method Summary collapse

Methods inherited from Base

#each_schema, inherited, #instance_types, #key, set_defaults, #static, value_schema=

Constructor Details

#initialize(parent_schema, value) ⇒ DynamicRef

Returns a new instance of DynamicRef.



9
10
11
12
13
# File 'lib/json_skooma/keywords/core/dynamic_ref.rb', line 9

def initialize(parent_schema, value)
  super
  @fragment = URI.parse(value).fragment
  @dynamic = false
end

Instance Method Details

#evaluate(instance, result) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json_skooma/keywords/core/dynamic_ref.rb', line 34

def evaluate(instance, result)
  ref_schema = @ref_schema
  if @dynamic
    target = result
    checked_uris = Set.new
    while target
      base_uri = target.schema.base_uri
      if base_uri && !checked_uris.include?(base_uri)
        checked_uris << base_uri
        target_uri = base_uri.dup.tap { |u| u.fragment = @fragment }

        begin
          found_schema = parent_schema.registry.schema(
            target_uri,
            cache_id: parent_schema.cache_id
          )
          dynamic_anchor = found_schema["$dynamicAnchor"]
          ref_schema = found_schema if dynamic_anchor == @fragment
        rescue RegistryError, Hana::Pointer::FormatError
          # do nothing
        end
      end

      target = target.parent
    end
  end

  ref_schema.evaluate(instance, result)
  result.ref_schema = ref_schema
end

#resolveObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/json_skooma/keywords/core/dynamic_ref.rb', line 15

def resolve
  uri = URI.parse(json)
  if uri.relative?
    base_uri = parent_schema.base_uri
    if base_uri.nil?
      raise "No base URI against which to resolve the `$dynamicRef` value `#{uri}`"
    end

    uri = base_uri + uri
  end
  @ref_schema = parent_schema.registry.schema(
    uri,
    metaschema_uri: parent_schema.metaschema_uri,
    cache_id: parent_schema.cache_id
  )
  dynamic_anchor = @ref_schema["$dynamicAnchor"] if @ref_schema.type == "object"
  @dynamic = dynamic_anchor == @fragment
end