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

error path is missing when using iter_errors #411

Closed
wichert opened this issue May 23, 2018 · 7 comments
Closed

error path is missing when using iter_errors #411

wichert opened this issue May 23, 2018 · 7 comments

Comments

@wichert
Copy link

wichert commented May 23, 2018

I have a very basic schema:

    schema = {
           '$schema': 'http://json-schema.org/draft-04/schema',
           'type': 'object',
           'properties': {
               'email': {
                   'type': 'string',
                   'format': 'email',
               },
               'password': {
                    'type': 'string',
                    'minLength': 1,
               },
           },
           'additionalProperties': False,
           'required': ['email', 'password'],
    }

When I validate an empty map against that I can see two errors:

>>> validator = validator_for(schema)
>>> errors = list(validator(schema).iter_errors(data))
>>> errors
[<ValidationError: "'email' is a required property">,
 <ValidationError: "'password' is a required property">]

Creating an error tree fails shows some odd behaviour:

>>> et = jsonschema.ErrorTree(validator(schema).iter_errors(data))
>>> et
<ErrorTree (1 total errors)>
>>>  'email' in et
False

Trying to figure out why this happened revealed that the error path is empty:

>>> [e.path for e in errors]
[deque([]), deque([])]
wichert added a commit to wichert/rest_toolkit that referenced this issue May 23, 2018
@Julian
Copy link
Member

Julian commented May 23, 2018

This is expected, though maybe underdocumented.

path means "where in the instance the error occured", and should always be a sequence of indexes that can be performed against the instance.

required is validated at the level of the object that should contain the properties (here your root object), so the path is empty.

Try your example with any other validator, or any nested object.

@wichert
Copy link
Author

wichert commented May 24, 2018

Hm, that is problematic for me. My use case here is doing validation on data past to a REST service, and I would like to report all errors back to the client, which I can't do now due to the missing path. As far as I can see there is no other way to see which property triggered the error. This is the __dict__ if the error:

{'cause': None,
 'context': [],
 'instance': {},
 'message': "'email' is a required property",
 'parent': None,
 'path': deque([]),
 'relative_path': deque([]),
 'relative_schema_path': deque(['required']),
 'schema': {'additionalProperties': False,
            'properties': {'email': {'format': 'email', 'type': 'string'},
                           'password': {'minLength': 1, 'type': 'string'}},
            'required': ['email', 'password'],
            'type': 'object'},
 'schema_path': deque(['required']),
 'validator': 'required',
 'validator_value': ['email', 'password']}

Can you consider adding some way to see which property triggered the error?

@Julian
Copy link
Member

Julian commented May 24, 2018

You're already seeing every error.

If you mean you want to know specifically for required which properties are missing, that's #119, patches certainly welcome.

@wichert
Copy link
Author

wichert commented May 24, 2018

I'll see if I can take a stab at that. One thing that has me a little confused is that the behaviour is not consistent: when using jsonschema.validate() instead of lazy validation the error exception does have the missing property in path. Reading the discussion in #119 I am wondering if that is a bug, since you mention the path should always be reachable, or if there is a specific reading for the different in behaviour?

@handrews
Copy link

handrews commented May 24, 2018

@wichert you might be interested in the discussion in json-schema-org/json-schema-spec#396, about standardizing or at least recommending output/error formats. Note that that's on the spec repo, not this implementation. It's also just getting off the ground despite having been filed a while back.

@Julian
Copy link
Member

Julian commented May 24, 2018

when using jsonschema.validate() instead of lazy validation the error exception does have the missing property in path

What code are you running and what path are you looking at?

The behavior here should match between iter_errors and validate (and the latter actually just uses the former).

@Julian
Copy link
Member

Julian commented May 28, 2018

Going to close this given the above (but if you do have a case where this has different behavior definitely lemme know!) and given @handrews's pointer, which I'll have to have a look at myself.

@Julian Julian closed this as completed May 28, 2018
Julian added a commit that referenced this issue Jul 25, 2020
ea41553 Remove test that doesn't match RFC 3339 duration grammar
dee8ef7 Merge pull request #411 from ChALkeR/chalker/more-unevaluted
cd88fb5 Add more nested and cousin unevaluatedProperties tests
5f3dc7e Merge pull request #415 from json-schema-org/ether/more-ipv6-tests
04ddab1 some more ipv6 tests
acb45cd Merge pull request #417 from ChALkeR/chalker/patternProperties
0f4b73a One more patternProperties test with boolean schemas
8ccbfdc Merge pull request #412 from json-schema-org/ether/more-format-duration-2
1329dab some more tests for the "duration" format
1d5c3c0 Merge pull request #405 from ChALkeR/chalker/email-format
7ad7443 Extend email format tests

git-subtree-dir: json
git-subtree-split: ea415537dda2613f3cd5df1f219f84086f75a7dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants