Skip to content

Commit

Permalink
Merge pull request #127 from mpalmer/fragment-validation-with-ref
Browse files Browse the repository at this point in the history
Make intra-doc refs work when validating with :fragment
  • Loading branch information
pd committed Oct 26, 2014
2 parents 5331d76 + 743140b commit 94fafe6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/json-schema/validator.rb
Expand Up @@ -70,6 +70,7 @@ def initialize(schema_data, data, opts={})
end

def schema_from_fragment(base_schema, fragment)
schema_uri = base_schema.uri
fragments = fragment.split("/")

# ensure the first element was a hash, per the fragment spec
Expand All @@ -82,17 +83,17 @@ def schema_from_fragment(base_schema, fragment)
if !base_schema.schema.has_key?(f)
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = base_schema.schema[f]
base_schema = base_schema.schema[f]
elsif base_schema.is_a?(Hash)
if !base_schema.has_key?(f)
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = initialize_schema(base_schema[f]) #need to return a Schema instance for validation to work
base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version])
elsif base_schema.is_a?(Array)
if base_schema[f.to_i].nil?
raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
end
base_schema = initialize_schema(base_schema[f.to_i])
base_schema = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version])
else
raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
end
Expand Down
40 changes: 40 additions & 0 deletions test/test_fragment_validation_with_ref.rb
@@ -0,0 +1,40 @@
require File.expand_path('../test_helper', __FILE__)
require 'json-schema'

class FragmentValidationWithRef < Test::Unit::TestCase
def whole_schema
{
"$schema" => "http://json-schema.org/draft-04/schema#",
"type" => "object",
"definitions" => {
"post" => {
"type" => "object",
"properties" => {
"content" => {
"type" => "string"
},
"author" => {
"type" => "string"
}
}
},
"posts" => {
"type" => "array",
"items" => {
"$ref" => "#/definitions/post"
}
}
}
}
end

def test_validation_of_fragment
data = [{"content" => "ohai", "author" => "Bob"}]
v = nil
assert_nothing_raised do
v = JSON::Validator.fully_validate(whole_schema,data,:fragment => "#/definitions/posts")
end

assert(v.empty?, v.join("\n"))
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,2 @@
require 'test/unit'
$:.unshift(File.expand_path('../../lib', __FILE__))

0 comments on commit 94fafe6

Please sign in to comment.