Skip to content

Commit

Permalink
Updating external referencing to resolve a referenced file against th…
Browse files Browse the repository at this point in the history
…e current URI, not the parent of the current URI. THIS MIGHT BREAK THINGS.

I've been holding off on this for a while, but it seems as if this is how the JSON schema groups wants it to be done.
  • Loading branch information
hoxworth committed Apr 21, 2011
1 parent eb070a9 commit f5a9afc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lib/json-schema/attributes/ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ def self.validate(current_schema, data, fragments, validator, options = {})
elsif path[0,1] == "/"
temp_uri.path = Pathname.new(path).cleanpath.to_s
else
temp_uri.path = (Pathname.new(current_schema.uri.path).parent + path).cleanpath.to_s
temp_uri.path = (Pathname.new(current_schema.uri.path) + path).cleanpath.to_s
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]
schema_key = temp_uri.to_s.split("#")[0] + "#"

ref_schema = JSON::Validator.schemas[schema_key]

if ref_schema
Expand Down
4 changes: 2 additions & 2 deletions lib/json-schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def initialize(schema,uri,parent_validator=nil)
if @schema['id']
temp_uri = URI.parse(@schema['id'])
if temp_uri.relative?
uri.path = (Pathname.new(uri.path).parent + @schema['id']).cleanpath.to_s
uri.path = (Pathname.new(uri.path) + @schema['id']).cleanpath.to_s
temp_uri = uri
end
@uri = temp_uri
end
@uri.fragment = nil
@uri.fragment = ''

# If there is a $schema on this schema, use it to determine which validator to use
if @schema['$schema']
Expand Down
28 changes: 15 additions & 13 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def load_ref_schema(parent_schema,ref)
if path && path[0,1] == '/'
uri.path = Pathname.new(path).cleanpath.to_s
else
uri.path = (Pathname.new(parent_schema.uri.path).parent + path).cleanpath.to_s
uri.path = (Pathname.new(parent_schema.uri.path) + path).cleanpath.to_s
end
uri.fragment = nil
uri.fragment = ''
end

if Validator.schemas[uri.to_s].nil?
Expand All @@ -156,7 +156,12 @@ def load_ref_schema(parent_schema,ref)


# Build all schemas with IDs, mapping out the namespace
def build_schemas(parent_schema)
def build_schemas(parent_schema)
# Build ref schemas if they exist
if parent_schema.schema["$ref"]
load_ref_schema(parent_schema, parent_schema.schema["$ref"])
end

# Check for schemas in union types
["type", "disallow"].each do |key|
if parent_schema.schema[key] && parent_schema.schema[key].is_a?(Array)
Expand Down Expand Up @@ -199,16 +204,12 @@ def build_schemas(parent_schema)

# Either load a reference schema or create a new schema
def handle_schema(parent_schema, obj)
if obj['$ref']
load_ref_schema(parent_schema, obj['$ref'])
else
schema_uri = parent_schema.uri.clone
schema = JSON::Schema.new(obj,schema_uri,@options[:version])
if obj['id']
Validator.add_schema(schema)
end
build_schemas(schema)
end
schema_uri = parent_schema.uri.clone
schema = JSON::Schema.new(obj,schema_uri,@options[:version])
if obj['id']
Validator.add_schema(schema)
end
build_schemas(schema)
end


Expand All @@ -226,6 +227,7 @@ def validate(schema, data,opts={})
def validate!(schema, data,opts={})
validator = JSON::Validator.new(schema, data, opts)
validator.validate
return true
end
alias_method 'validate2', 'validate!'

Expand Down

0 comments on commit f5a9afc

Please sign in to comment.