Class: JSON::Schema::RefAttribute

Inherits:
Attribute
  • Object
show all
Defined in:
lib/json-schema/attributes/ref.rb

Class Method Summary collapse

Methods inherited from Attribute

build_fragment, validation_error

Class Method Details

.validate(current_schema, data, fragments, validator, options = {}) ⇒ Object



4
5
6
7
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
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/json-schema/attributes/ref.rb', line 4

def self.validate(current_schema, data, fragments, validator, options = {})        
  temp_uri = URI.parse(current_schema.schema['$ref'])
  if temp_uri.relative?
    temp_uri = current_schema.uri.clone
    # Check for absolute path
    path = current_schema.schema['$ref'].split("#")[0]
    if path.nil? || path == ''
      temp_uri.path = current_schema.uri.path
    elsif path[0,1] == "/"
      temp_uri.path = Pathname.new(path).cleanpath.to_s
    else
      temp_uri = current_schema.uri.merge(path)
    end
    temp_uri.fragment = current_schema.schema['$ref'].split("#")[1]
  end
  temp_uri.fragment = "" if temp_uri.fragment.nil?

  # Grab the parent schema from the schema list
  schema_key = temp_uri.to_s.split("#")[0] + "#"

  ref_schema = JSON::Validator.schemas[schema_key]

  if ref_schema
    # Perform fragment resolution to retrieve the appropriate level for the schema
    target_schema = ref_schema.schema
    fragments = temp_uri.fragment.split("/")
    fragment_path = ''
    fragments.each do |fragment|
      if fragment && fragment != ''
        if target_schema.is_a?(Array)
          target_schema = target_schema[fragment.to_i]
        else
          target_schema = target_schema[fragment]
        end
        fragment_path = fragment_path + "/#{fragment}"
        if target_schema.nil?
          raise SchemaError.new("The fragment '#{fragment_path}' does not exist on schema #{ref_schema.uri.to_s}")
        end
      end
    end

    # We have the schema finally, build it and validate!
    schema = JSON::Schema.new(target_schema,temp_uri,validator)
    schema.validate(data, fragments, options)
  else
    message = "The referenced schema '#{temp_uri.to_s}' cannot be found"
    validation_error(message, fragments, current_schema, options[:record_errors])
  end
end