Class: SchemaTools::RefResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_tools/ref_resolver.rb

Class Method Summary collapse

Class Method Details

._retrieve_pointer_from_object(pointer, object) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/schema_tools/ref_resolver.rb', line 73

def self._retrieve_pointer_from_object(pointer, object)
  # assume path to be the JSONPointer expression:
  #  json/pointer/expression
  # and obj to be the ruby hash representation of the json
  path = pointer.is_a?(Array) ? pointer : pointer.split("/")

  while object != nil && component = path.shift
    component = component.to_i if object.is_a?(Array) && component =~ /^\d+$/
    object = object[component]
  end

  return object
end

.find_local_file_path(base_dir, file_path, relative_to = nil) ⇒ Object

contact.json, ./contacts/client.json schema, an attempt is made to load it from the relatives absolute dir

Parameters:

  • base_dir (String)
  • path (String)

    relative file name with optional sub-path prefix:

  • relative_to (Schema) (defaults to: nil)

    If the pointer contains a uri to a referenced



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/schema_tools/ref_resolver.rb', line 54

def self.find_local_file_path(base_dir, file_path, relative_to=nil)
  path = File.join(base_dir, file_path)
  return path if File.exist?(path)

  # try to find in main-dir and subdirs of global schema path and if present
  # a schema's absolute dir
  filename = file_path.split('/').last
  search_dirs = [File.join(SchemaTools.schema_path, filename),
                 File.join(SchemaTools.schema_path, '**/*', filename)]
  if relative_to
    search_dirs += [ File.join(relative_to.absolute_dir, filename),
                     File.join(relative_to.absolute_dir, '**/*', filename) ]
  end
  recursive_search = Dir.glob(search_dirs)[0]
  # if still not found return orig path to throw error on open
  recursive_search || path
end

.load_json_pointer(json_pointer, relative_to = nil) ⇒ Object

super basic resolving of JSON Pointer stuff in order to be able to load $ref parameter.

$refs in JSON Schema are defined in JSON Pointer syntax: tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07 JSON Pointer is a big, abandoned WIP and we’re going to start by only implementing the part’s we need …

the hash to evaluate it against. If the pointer contains a uri to a referenced schema, an attempt is made to load

Parameters:

  • json_pointer (String)

    the JSON Pointer expression to evaluate

  • relative_to (Schema) (defaults to: nil)

    if the pointer refers to a local schema, this is this



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/schema_tools/ref_resolver.rb', line 22

def self.load_json_pointer(json_pointer, relative_to = nil)

  if json_pointer[/#/]
    # hash-symbol syntax pointing to a property of a schema. client.json#properties
    raise "invalid json pointer: #{json_pointer}" unless json_pointer =~ /^(.*)#(.*)/
    uri, pointer = json_pointer.match(/^(.*)#(.*)/).captures
  else
    uri = json_pointer
  end

  raise "invalid uri pointer: #{json_pointer}" if uri.empty?

  schema  = {}
  uri = URI.parse(uri)
  raise "must currently be a relative uri: #{json_pointer}" if uri.absolute?
  # TODO use local tools instance or base path from global SchemaTools.schema_path
  base_dir = relative_to ? relative_to.absolute_dir : SchemaTools.schema_path
  path = find_local_file_path(base_dir, uri.path, relative_to)
  open (path) {|f| schema = JSON.parse(f.read) }

  if pointer
    self._retrieve_pointer_from_object(pointer, schema)
  else
    schema
  end
end