Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 86 #92

Merged
merged 4 commits into from Dec 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/json-schema/attributes/allof.rb
Expand Up @@ -26,12 +26,12 @@ def self.validate(current_schema, data, fragments, processor, validator, options
end
end

if !valid
if !valid || !errors.empty?
message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match all of the required schemas"
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
validation_errors(processor).last.sub_errors = errors
end
end
end
end
end
end
33 changes: 15 additions & 18 deletions lib/json-schema/attributes/oneof.rb
Expand Up @@ -2,34 +2,31 @@ module JSON
class Schema
class OneOfAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
matched = false
valid = false

validation_errors = 0
current_schema.schema['oneOf'].each do |element|
schema = JSON::Schema.new(element,current_schema.uri,validator)

begin
schema.validate(data,fragments,processor,options)
if matched
valid = false
else
matched = true
valid = true
end
# need to raise exceptions on error because
# schema.validate doesn't reliably return true/false
schema.validate(data,fragments,processor,options.merge(:record_errors => false))
rescue ValidationError
validation_errors += 1
end

end

if !valid
if matched
message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
else
message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
end
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
case validation_errors
when current_schema.schema['oneOf'].length - 1 # correct, matched only one
message = nil
when current_schema.schema['oneOf'].length # didn't match any
message = "The property '#{build_fragment(fragments)}' of type #{data.class} did not match any of the required schemas"
else # too many matches
message = "The property '#{build_fragment(fragments)}' of type #{data.class} matched more than one of the required schemas"
end

validation_error(processor, message, fragments, current_schema, self, options[:record_errors]) if message
end
end
end
end
end
3 changes: 3 additions & 0 deletions test/data/all_of_ref_data.json
@@ -0,0 +1,3 @@
{
"name" : "john"
}
5 changes: 5 additions & 0 deletions test/data/one_of_ref_links_data.json
@@ -0,0 +1,5 @@
{ "links":
[{ "rel" : ["self"] , "href":"http://api.example.com/api/object/3" }
,{ "rel" : ["up"] , "href":"http://api.example.com/api/object" }
]
}
4 changes: 4 additions & 0 deletions test/schemas/all_of_ref_base_schema.json
@@ -0,0 +1,4 @@
{"type": "object",
"properties" : {
"name" : { "type": "integer" }
}
7 changes: 7 additions & 0 deletions test/schemas/all_of_ref_schema.json
@@ -0,0 +1,7 @@
{ "$schema" : "http://json-schema.org/draft-04/schema#",
"type" : "object",
"allOf" :
[ { "$ref" : "all_of_ref_base_schema.json" }
]

}
16 changes: 16 additions & 0 deletions test/schemas/one_of_ref_links_schema.json
@@ -0,0 +1,16 @@
{ "$schema": "http://json-schema.org/draft-04/schema#"
, "type": "object"
, "properties":
{ "links" :
{ "type" : "array"
, "items" :
{ "type" : "object"
, "oneOf" :
[ { "$ref" : "self_link_schema.json"}
, { "$ref" : "up_link_schema.json" }
]
}
}
}
}

17 changes: 17 additions & 0 deletions test/schemas/self_link_schema.json
@@ -0,0 +1,17 @@
{ "$schema": "http://json-schema.org/draft-04/schema#"
, "type": "object"
, "properties" :
{ "rel" :
{ "type" : "array"
, "items" :
[ { "type" : "string"
, "pattern" : "self"
}
]
}
, "href" :
{ "type" : "string"
}
}
}

17 changes: 17 additions & 0 deletions test/schemas/up_link_schema.json
@@ -0,0 +1,17 @@
{ "$schema": "http://json-schema.org/draft-04/schema#"
, "type": "object"
, "properties" :
{ "rel" :
{ "type" : "array"
, "items" :
[ { "type" : "string"
, "pattern" : "up"
}
]
}
, "href" :
{ "type" : "string"
}
}
}

11 changes: 11 additions & 0 deletions test/test_all_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/all_of_ref_schema.json")
data = File.join(File.dirname(__FILE__),"data/all_of_ref_data.json")
errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
assert(!errors.empty?, "should have failed to validate")
end
end
2 changes: 1 addition & 1 deletion test/test_any_of_ref_schema.rb
Expand Up @@ -2,7 +2,7 @@
require File.dirname(__FILE__) + '/../lib/json-schema'

class AnyOfRefSchemaTest < Test::Unit::TestCase
def test_all_of_ref_schema
def test_any_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)
Expand Down
42 changes: 42 additions & 0 deletions test/test_one_of.rb
@@ -0,0 +1,42 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/json-schema'

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


def test_one_of_with_string_patterns
schema = {
"$schema" => "http://json-schema.org/draft-04/schema#",
"oneOf" => [
{
"properties" => {"a" => {"type" => "string", "pattern" => "foo"}},
},
{
"properties" => {"a" => {"type" => "string", "pattern" => "bar"}},
},
{
"properties" => {"a" => {"type" => "string", "pattern" => "baz"}},
}
]
}

data = {"a" => "foo"}
assert(JSON::Validator.validate(schema,data))

data = {"a" => "foobar"}
assert(!JSON::Validator.validate(schema,data))

data = {"a" => "baz"}
assert(JSON::Validator.validate(schema,data))

data = {"a" => 5}
assert(!JSON::Validator.validate(schema,data))
end

end