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

Friendlier unevaluatedProperties errors #1074

Closed
ikonst opened this issue Mar 27, 2023 · 3 comments
Closed

Friendlier unevaluatedProperties errors #1074

ikonst opened this issue Mar 27, 2023 · 3 comments
Labels
Enhancement Some new desired functionality Error Reporting Issues related to clearer or more robust validation error reporting

Comments

@ikonst
Copy link
Contributor

ikonst commented Mar 27, 2023

It would be helpful to bifurcate the "Unevaluated properties are not allowed" error messages over whether the property was expected — defined but failing validation, e.g.

  • "Unevaluated properties are not allowed ('foo', 'bar' were not valid under the given schema)"
  • "Unevaluated properties are not allowed ('baz' was unexpected)"

A single unevaluatedProperties could yield 0, 1 or 2 errors.

I personally found that aspect of unevaluatedProperties to be confusing when I first stumbled on it ("foo is not a typo, so why is it reported?"). Later I understood it, but my learning path could be shorter and astonishment smaller if we somehow structured the message to suggest this possibility.

--

One way to implement this would be to have find_evaluated_property_keys_by_schema collect tuples of (property_name, is_valid), and then process them within unevaluatedProperties:

-evaluated_keys = find_evaluated_property_keys_by_schema(...)
+res = find_evaluated_property_keys_by_schema(...)
+evaluated_keys = {key for key, is_valid in res if is_valid}
+expected_keys = {key for key, _ in res if key in instance}

Then unevaluated_keys could be split into:

unevaluated_expected_keys = [k for k in unevaluated_keys if k in expected_keys]
unevaluated_unexpected_keys = [k for k in unevaluated_keys if k not in expected_keys]

and each list used to emit the relevant error message.

@Julian Julian added Enhancement Some new desired functionality Error Reporting Issues related to clearer or more robust validation error reporting labels Mar 27, 2023
@Julian
Copy link
Member

Julian commented Jun 5, 2023

Another example of this is the simple schema:

from jsonschema import Draft202012Validator
schema = {
    "required": ["foo"],
    "unevaluatedProperties": False,
    "oneOf": [{"required": ["bar"]}],
}
Draft202012Validator(schema).validate({"foo": 37})

which produces the confusing:

Traceback (most recent call last):
  File "/Users/julian/Desktop/uP.py", line 7, in <module>
    Draft202012Validator(schema).validate({"foo": 37})
  File "/Users/julian/Development/jsonschema/jsonschema/validators.py", line 424, in validate
    raise error
jsonschema.exceptions.ValidationError: Unevaluated properties are not allowed ('foo' was unexpected)

Failed validating 'unevaluatedProperties' in schema:
    {'oneOf': [{'required': ['bar']}],
     'required': ['foo'],
     'unevaluatedProperties': False}

On instance:
    {'foo': 37}

which can be especially bad if the oneOf (or applicator in general) produces some very deep error. Here it's probably best_match that needs enhancing to traverse into the applicators for this example.

@ikonst
Copy link
Contributor Author

ikonst commented Jun 5, 2023

IIRC, this issue has disappeared for me once #1075 got merged in.

@Julian
Copy link
Member

Julian commented Jun 5, 2023

Ah, good to know -- I'll move my above comment (which was really just using this as a clotheshanger issue) to some other unevaluatedProperty one.

Feel free to file another issue if you have one with unevaluatedProperties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Some new desired functionality Error Reporting Issues related to clearer or more robust validation error reporting
Projects
None yet
Development

No branches or pull requests

2 participants