Skip to content

Commit

Permalink
Only run additionalProperties when dealing with an object.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxworth committed Jul 1, 2013
2 parents b0ec4ca + 9ae5ca8 commit 91f68f7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 8 additions & 3 deletions lib/json-schema/attributes/additionalproperties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ module JSON
class Schema
class AdditionalPropertiesAttribute < Attribute
def self.validate(current_schema, data, fragments, processor, validator, options = {})
if data.is_a?(Hash)
if data.is_a?(Hash) && (
current_schema.schema['type'].nil? || (
current_schema.schema['type'].is_a?(String) &&
current_schema.schema['type'].downcase == 'object'
)
)
extra_properties = data.keys

extra_properties = remove_valid_properties(extra_properties, current_schema, validator)

addprop= current_schema.schema['additionalProperties']
addprop = current_schema.schema['additionalProperties']
if addprop.is_a?(Hash)
matching_properties= extra_properties # & addprop.keys
matching_properties.each do |key|
Expand Down Expand Up @@ -57,6 +61,7 @@ def self.remove_valid_properties(extra_properties, current_schema, validator)

extra_properties
end

end
end
end
20 changes: 19 additions & 1 deletion test/test_jsonschema_draft3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,27 @@ def test_types

data["a"] = {"b" => "taco"}
assert(!JSON::Validator.validate(schema,data))
end

# Test an array of unioned-type objects that prevent additionalProperties
schema["properties"]["a"] = {
'type' => 'array',
'items' => {
'type' => [
{ 'type' => 'object', 'properties' => { "b" => { "type" => "integer" } } },
{ 'type' => 'object', 'properties' => { "c" => { "type" => "string" } } }
],
'additionalProperties' => false
}
}

data["a"] = [{"b" => 5}, {"c" => "foo"}]
errors = JSON::Validator.fully_validate(schema, data)
assert(errors.empty?, errors.join("\n"))

# This should actually pass, because this matches the first schema in the union
data["a"] << {"c" => false}
assert(JSON::Validator.validate(schema,data))
end

def test_required
# Set up the default datatype
Expand Down

0 comments on commit 91f68f7

Please sign in to comment.