Class: JsonSchema::ReferenceExpander

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors


5
6
7
# File 'lib/json_schema/reference_expander.rb', line 5

def errors
  @errors
end

#storeObject

Returns the value of attribute store


6
7
8
# File 'lib/json_schema/reference_expander.rb', line 6

def store
  @store
end

Instance Method Details

#expand(schema, options = {}) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json_schema/reference_expander.rb', line 8

def expand(schema, options = {})
  @errors       = []
  @local_store  = DocumentStore.new
  @schema       = schema
  @schema_paths = {}
  @store        = options[:store] || DocumentStore.new

  # If the given JSON schema is _just_ a JSON reference and nothing else,
  # short circuit the whole expansion process and return the result.
  if schema.reference && !schema.expanded?
    return dereference(schema, [])
  end

  @uri = URI.parse(schema.uri)

  @store.each do |uri, store_schema|
    build_schema_paths(uri, store_schema)
  end

  # we run #to_s on lookup for URIs; the #to_s of nil is ""
  build_schema_paths("", schema)

  traverse_schema(schema)

  refs = unresolved_refs(schema).sort
  if refs.count > 0
    message = %{Couldn't resolve references: #{refs.to_a.join(", ")}.}
    @errors << SchemaError.new(schema, message, :unresolved_references)
  end

  @errors.count == 0
end

#expand!(schema, options = {}) ⇒ Object


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

def expand!(schema, options = {})
  if !expand(schema, options)
    raise AggregateError.new(@errors)
  end
  true
end