From 915c97eafd36b9baa6cb133111255257703a43c6 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Fri, 2 Aug 2013 10:23:35 -0500 Subject: [PATCH] Fix Issue #66 $ref schemas inside of allOf, anyOf, etc wasn't being loaded. This patch adds support for it. --- lib/json-schema/validator.rb | 9 +++++++++ test/data/any_of_ref_data.json | 7 +++++++ test/schemas/any_of_ref_jane_schema.json | 4 ++++ test/schemas/any_of_ref_jimmy_schema.json | 4 ++++ test/schemas/any_of_ref_john_schema.json | 4 ++++ test/schemas/any_of_ref_schema.json | 15 +++++++++++++++ test/test_any_of_ref_schema.rb | 11 +++++++++++ 7 files changed, 54 insertions(+) create mode 100644 test/data/any_of_ref_data.json create mode 100644 test/schemas/any_of_ref_jane_schema.json create mode 100644 test/schemas/any_of_ref_jimmy_schema.json create mode 100644 test/schemas/any_of_ref_john_schema.json create mode 100644 test/schemas/any_of_ref_schema.json create mode 100644 test/test_any_of_ref_schema.rb diff --git a/lib/json-schema/validator.rb b/lib/json-schema/validator.rb index c65fdbec..73abfe50 100644 --- a/lib/json-schema/validator.rb +++ b/lib/json-schema/validator.rb @@ -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) diff --git a/test/data/any_of_ref_data.json b/test/data/any_of_ref_data.json new file mode 100644 index 00000000..0e8dc43c --- /dev/null +++ b/test/data/any_of_ref_data.json @@ -0,0 +1,7 @@ +{ + "names" : + [ "john" + , "jane" + , "jimmy" + ] +} diff --git a/test/schemas/any_of_ref_jane_schema.json b/test/schemas/any_of_ref_jane_schema.json new file mode 100644 index 00000000..fc42b2d6 --- /dev/null +++ b/test/schemas/any_of_ref_jane_schema.json @@ -0,0 +1,4 @@ +{ "$schema" : "http://json-schema.org/draft-04/schema#" +, "type" : "string" +, "pattern" : "jane" +} diff --git a/test/schemas/any_of_ref_jimmy_schema.json b/test/schemas/any_of_ref_jimmy_schema.json new file mode 100644 index 00000000..f77d0239 --- /dev/null +++ b/test/schemas/any_of_ref_jimmy_schema.json @@ -0,0 +1,4 @@ +{ "$schema" : "http://json-schema.org/draft-04/schema#" +, "type" : "string" +, "pattern" : "jimmy" +} diff --git a/test/schemas/any_of_ref_john_schema.json b/test/schemas/any_of_ref_john_schema.json new file mode 100644 index 00000000..6d5cc9eb --- /dev/null +++ b/test/schemas/any_of_ref_john_schema.json @@ -0,0 +1,4 @@ +{ "$schema" : "http://json-schema.org/draft-04/schema#" +, "type" : "string" +, "pattern" : "john" +} diff --git a/test/schemas/any_of_ref_schema.json b/test/schemas/any_of_ref_schema.json new file mode 100644 index 00000000..59863aeb --- /dev/null +++ b/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" } + ] + } + } + } +} diff --git a/test/test_any_of_ref_schema.rb b/test/test_any_of_ref_schema.rb new file mode 100644 index 00000000..3bfbe15d --- /dev/null +++ b/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