-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made sure we report strict validation errors for draft1 and draft2
schemas We've had a bug for a while where strict validation didn't work for draft1 and draft2 schemas. Strict validation should raise errors if there are properties in the data that are not defined in the schema. However, this was only happening with draft3 and draft4 schemas. This error slipped through because the tests for strict validation were silently switching to either draft4 or the default schema version, and draft3 and draft4 did implement strict validation correctly. So in practice we were never really testing draft1 and draft2 with strict validation. I've fixed this by refactoring the code that draft3 and draft4 used for handling unrecognized properties (under strict validation) such that it could also be used by draft1 and draft2, and fixed up the strict validation tests so that they use the correct json-schema draft.
- Loading branch information
1 parent
780a7e1
commit 7dad000
Showing
8 changed files
with
118 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,91 @@ | ||
module StrictValidation | ||
def test_strict_properties | ||
schema = { | ||
"$schema" => "http://json-schema.org/draft-04/schema#", | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
module PropertiesTests | ||
def test_strict_properties | ||
schema = { | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
} | ||
} | ||
} | ||
|
||
data = {"a" => "a"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"b" => "b"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"b" => "b"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b"} | ||
assert(JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b"} | ||
assert_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
end | ||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
refute_valid schema,data,:strict => true | ||
end | ||
|
||
def test_strict_error_message | ||
schema = { :type => 'object', :properties => { :a => { :type => 'string' } } } | ||
data = { :a => 'abc', :b => 'abc' } | ||
errors = JSON::Validator.fully_validate(schema,data,:strict => true) | ||
assert_match("The property '#/' contained undefined properties: 'b' in schema", errors[0]) | ||
def test_strict_error_message | ||
schema = { :type => 'object', :properties => { :a => { :type => 'string' } } } | ||
data = { :a => 'abc', :b => 'abc' } | ||
errors = JSON::Validator.fully_validate(schema,data,:strict => true) | ||
assert_match("The property '#/' contained undefined properties: 'b' in schema", errors[0]) | ||
end | ||
end | ||
|
||
def test_strict_properties_additional_props | ||
schema = { | ||
"$schema" => "http://json-schema.org/draft-04/schema#", | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
}, | ||
"additionalProperties" => {"type" => "integer"} | ||
} | ||
module AdditionalPropertiesTests | ||
def test_strict_properties_additional_props | ||
schema = { | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
}, | ||
"additionalProperties" => {"type" => "integer"} | ||
} | ||
|
||
data = {"a" => "a"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"b" => "b"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"b" => "b"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b"} | ||
assert(JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b"} | ||
assert_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "c" => 3} | ||
assert(JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "c" => 3} | ||
assert_valid schema,data,:strict => true | ||
end | ||
end | ||
|
||
def test_strict_properties_pattern_props | ||
schema = { | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
}, | ||
"patternProperties" => {"\\d+ taco" => {"type" => "integer"}} | ||
} | ||
module PatternPropertiesTests | ||
def test_strict_properties_pattern_props | ||
schema = { | ||
"properties" => { | ||
"a" => {"type" => "string"}, | ||
"b" => {"type" => "string"} | ||
}, | ||
"patternProperties" => {"\\d+ taco" => {"type" => "integer"}} | ||
} | ||
|
||
data = {"a" => "a"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"b" => "b"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"b" => "b"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b"} | ||
assert(JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b"} | ||
assert_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "c" => "c"} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "c" => 3} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "c" => 3} | ||
refute_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "23 taco" => 3} | ||
assert(JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "23 taco" => 3} | ||
assert_valid schema,data,:strict => true | ||
|
||
data = {"a" => "a", "b" => "b", "23 taco" => "cheese"} | ||
assert(!JSON::Validator.validate(schema,data,:strict => true)) | ||
data = {"a" => "a", "b" => "b", "23 taco" => "cheese"} | ||
refute_valid schema,data,:strict => true | ||
end | ||
end | ||
|
||
end |