Class: JsonSchema::DocumentStore

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema/document_store.rb

Overview

The document store helps resolve URI-based JSON pointers by storing IDs that we’ve seen in the schema.

Each URI tuple also contains a pointer map that helps speed up expansions that have already happened and handles cyclic dependencies. Store a reference to the top-level schema before doing anything else.

Instance Method Summary collapse

Constructor Details

#initializeDocumentStore

Returns a new instance of DocumentStore.



9
10
11
# File 'lib/json_schema/document_store.rb', line 9

def initialize
  @uri_map = {}
end

Instance Method Details

#add_pointer_reference(uri, path, schema) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/json_schema/document_store.rb', line 13

def add_pointer_reference(uri, path, schema)
  raise "can't add nil URI" if uri.nil?

  if !@uri_map[uri][:pointer_map].key?(path)
    @uri_map[uri][:pointer_map][path] = schema
  end
end

#add_uri_reference(uri, schema) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/json_schema/document_store.rb', line 21

def add_uri_reference(uri, schema)
  raise "can't add nil URI" if uri.nil?

  # Children without an ID keep the same URI as their parents. So since we
  # traverse trees from top to bottom, just keep the first reference.
  if !@uri_map.key?(uri)
    @uri_map[uri] = {
      pointer_map: {
        JsonReference.reference("#").to_s => schema
      },
      schema: schema
    }
  end
end

#lookup_pointer(uri, pointer) ⇒ Object



36
37
38
# File 'lib/json_schema/document_store.rb', line 36

def lookup_pointer(uri, pointer)
  @uri_map[uri][:pointer_map][pointer]
end

#lookup_uri(uri) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/json_schema/document_store.rb', line 40

def lookup_uri(uri)
  if @uri_map[uri]
    @uri_map[uri][:schema]
  else
    nil
  end
end