Skip to content

Fix non string object keys #170

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
import json
import pkgutil
import re
import sys

from jsonschema.compat import str_types, MutableMapping, urlsplit


if sys.version_info[0] == 3:
STRING_TYPES = str
else:
STRING_TYPES = (str, unicode)


class URIDict(MutableMapping):
"""
Dictionary which uses normalized URIs as keys.
Expand Down Expand Up @@ -97,7 +104,7 @@ def find_additional_properties(instance, schema):
patterns = "|".join(schema.get("patternProperties", {}))
for property in instance:
if property not in properties:
if patterns and re.search(patterns, property):
if isinstance(property, STRING_TYPES) and patterns and re.search(patterns, property):
continue
yield property

Expand Down
3 changes: 2 additions & 1 deletion jsonschema/_validators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

from jsonschema import _utils
from jsonschema._utils import STRING_TYPES
from jsonschema.exceptions import FormatError, ValidationError
from jsonschema.compat import iteritems

Expand All @@ -14,7 +15,7 @@ def patternProperties(validator, patternProperties, instance, schema):

for pattern, subschema in iteritems(patternProperties):
for k, v in iteritems(instance):
if re.search(pattern, k):
if isinstance(k, STRING_TYPES) and re.search(pattern, k):
for error in validator.descend(
v, subschema, path=k, schema_path=pattern,
):
Expand Down
20 changes: 20 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,26 @@ def test_if_you_give_it_junk_you_get_a_resolution_error(self):
self.assertEqual(str(err.exception), "Oh no! What's this?")


class TestSimpleBug(unittest.TestCase):
def test_non_string_object_key(self):
schema = {
"type": "object",
"patternProperties": {
".*": {
"type": "string",
},
},
"additionalProperties": False,
}

obj = {
42: True,
}

with self.assertRaises(ValidationError):
validate(obj, schema)


def sorted_errors(errors):
def key(error):
return (
Expand Down