Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
Fix Issue voxpupuli#66
Browse files Browse the repository at this point in the history
$ref schemas inside of allOf, anyOf, etc wasn't being loaded.  This
patch adds support for it.
  • Loading branch information
apsoto committed Aug 2, 2013
1 parent 6a35e70 commit 915c97e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/json-schema/validator.rb
Expand Up @@ -277,6 +277,15 @@ def build_schemas(parent_schema)
end
end

# handle validations that always contain schemas
["allOf", "anyOf", "oneOf", "not"].each do |key|
if parent_schema.schema.has_key?(key)
validations = parent_schema.schema[key]
validations = [validations] unless validations.is_a?(Array)
validations.each {|v| handle_schema(parent_schema, v) }
end
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
7 changes: 7 additions & 0 deletions test/data/any_of_ref_data.json
@@ -0,0 +1,7 @@
{
"names" :
[ "john"
, "jane"
, "jimmy"
]
}
4 changes: 4 additions & 0 deletions test/schemas/any_of_ref_jane_schema.json
@@ -0,0 +1,4 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#"
, "type" : "string"
, "pattern" : "jane"
}
4 changes: 4 additions & 0 deletions test/schemas/any_of_ref_jimmy_schema.json
@@ -0,0 +1,4 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#"
, "type" : "string"
, "pattern" : "jimmy"
}
4 changes: 4 additions & 0 deletions test/schemas/any_of_ref_john_schema.json
@@ -0,0 +1,4 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#"
, "type" : "string"
, "pattern" : "john"
}
15 changes: 15 additions & 0 deletions test/schemas/any_of_ref_schema.json
@@ -0,0 +1,15 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#"
, "type" : "object"
, "properties" :
{ "names" :
{ "type" : "array"
, "items" :
{ "anyOf" :
[ { "$ref" : "any_of_ref_john_schema.json" }
, { "$ref" : "any_of_ref_jane_schema.json" }
, { "$ref" : "any_of_ref_jimmy_schema.json" }
]
}
}
}
}
11 changes: 11 additions & 0 deletions test/test_any_of_ref_schema.rb
@@ -0,0 +1,11 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/json-schema'

class AnyOfRefSchemaTest < Test::Unit::TestCase
def test_all_of_ref_schema
schema = File.join(File.dirname(__FILE__),"schemas/any_of_ref_schema.json")
data = File.join(File.dirname(__FILE__),"data/any_of_ref_data.json")
errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
assert(errors.empty?, errors.map{|e| e[:message] }.join("\n"))
end
end

0 comments on commit 915c97e

Please sign in to comment.