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

Treat invalid pattern property as unevaluated #1073

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
6 changes: 3 additions & 3 deletions jsonschema/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema):

if "patternProperties" in schema:
for property, value in instance.items():
for pattern, _ in schema["patternProperties"].items():
for pattern, subschema in schema["patternProperties"].items():
if re.search(pattern, property) and validator.evolve(
schema=schema["patternProperties"],
).is_valid({property: value}):
schema=subschema,
).is_valid(value):
evaluated_keys.append(property)

if "dependentSchemas" in schema:
Expand Down
29 changes: 27 additions & 2 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,14 @@ def id_of(schema):


class TestValidationErrorMessages(TestCase):
def message_for(self, instance, schema, *args, **kwargs):
def errors_for(self, instance, schema, *args, **kwargs):
cls = kwargs.pop("cls", validators._LATEST_VERSION)
cls.check_schema(schema)
validator = cls(schema, *args, **kwargs)
errors = list(validator.iter_errors(instance))
return list(validator.iter_errors(instance))

def message_for(self, instance, schema, *args, **kwargs):
errors = self.errors_for(instance, schema, *args, **kwargs)
self.assertTrue(errors, msg=f"No errors were raised for {instance!r}")
self.assertEqual(
len(errors),
Expand Down Expand Up @@ -677,6 +680,28 @@ def test_unevaluated_properties_disallowed(self):
"('bar', 'foo' were unexpected)",
)

def test_unevaluated_properties_failed_validation(self):
schema = {
"type": "object",
"patternProperties": {
"^foo": {"type": "string"},
},
"unevaluatedProperties": False,
}
errors = self.errors_for(
instance={
"foo": 42,
},
schema=schema,
)
error = [e for e in errors
if e.validator == "unevaluatedProperties"][0]
self.assertEqual(
error.message,
"Unevaluated properties are not allowed "
"('foo' was unexpected)",
)

def test_unevaluated_properties_on_invalid_type(self):
schema = {"type": "object", "unevaluatedProperties": False}
message = self.message_for(instance="foo", schema=schema)
Expand Down