Class: JSONSkooma::Keywords::Draft201909::RecursiveRef

Inherits:
Base
  • Object
show all
Defined in:
lib/json_skooma/keywords/draft_2019_09/recursive_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) ⇒ RecursiveRef

Returns a new instance of RecursiveRef.



9
10
11
12
# File 'lib/json_skooma/keywords/draft_2019_09/recursive_ref.rb', line 9

def initialize(parent_schema, value)
  super
  raise "`$recursiveRef` may only take the value `#`" if value != "#"
end

Instance Method Details

#evaluate(instance, result) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/json_skooma/keywords/draft_2019_09/recursive_ref.rb', line 27

def evaluate(instance, result)
  ref_schema = @ref_schema
  recursive_anchor = ref_schema["$recursiveAnchor"]
  if recursive_anchor&.value
    target = result
    while target
      base_anchor = target.schema["$recursiveAnchor"]
      if base_anchor&.value
        ref_schema = target.schema
      end
      target = target.parent
    end
  end
  ref_schema.evaluate(instance, result)
  result.ref_schema = ref_schema
end

#resolveObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/json_skooma/keywords/draft_2019_09/recursive_ref.rb', line 14

def resolve
  base_uri = parent_schema.base_uri
  if base_uri.nil?
    raise "No base URI against which to resolve the `$recursiveRef`"
  end

  @ref_schema = parent_schema.registry.schema(
    base_uri,
    metaschema_uri: parent_schema.metaschema_uri,
    cache_id: parent_schema.cache_id
  )
end