jsonschema icon indicating copy to clipboard operation
jsonschema copied to clipboard

error object properties "schema" and "relative_schema_path" seem to be inconsistent

Open jason-s opened this issue 10 years ago • 5 comments

I need to parse a jsonschema.exceptions.ValidationError object to help produce a more useful error message to my customers, but I am not sure that the fields of the ValidationError object are correct.

Test case: https://gist.github.com/jason-s/6d2b3418f4edb7fc8064

Raw object (in yaml format, but jsonschema is representation-agnostic -- thank you, it means I can use it for yaml files):

debug:
  foo:
    bar:
      jiminy: cricket
      mickey: mouse
      44hoho: twinkie

The validator correctly find that 44hoho is not a legal property name; it doesn't match patternProperties and in my schema I have additionalProperties = false.

The problem is that the error object's relative_schema_path and schema seem inconsistent.

My test case prints:

parent         : None
relative_path  : deque(['debug', 'foo', 'bar'])
relative_schema_path: deque(['properties', 'debug', 'properties', 'foo', 'properties', 'bar', 'additionalProperties'])
instance       : {'jiminy': 'cricket', 'mickey': 'mouse', '44hoho': 'twinkie'}
schema_path    : deque(['properties', 'debug', 'properties', 'foo', 'properties', 'bar', 'additionalProperties'])
validator      : additionalProperties
context        : []
path           : deque(['debug', 'foo', 'bar'])
message        : Additional properties are not allowed ('44hoho' was unexpected)
schema         : {'additionalProperties': False, 'type': 'object', 'patternProperties': {'^[^\\d\\W]\\w*$': {'type': 'string'}}}
cause          : None
validator_value: False

Note that the schema attribute is a sub-portion of the schema object, but the relative schema path is from the root schema.

jason-s avatar Jan 06 '15 18:01 jason-s

It also doesn't look like there is any reliable way to get the full path of the element causing the problem; this should be /debug/foo/bar/44hoho, and I can get the /debug/foo/bar part from the relative_path attribute, but 44hoho is buried in the text of the message attribute, and not available elsewhere (e.g. in cause or something).

jason-s avatar Jan 06 '15 18:01 jason-s

Sorry, I thought I replied here, but yeah the first thing certainly doesn't look right. Any chance you might have hunted down where the problem is?

The second thing (getting the full path to your problem) is likely another instance of #119.

Julian avatar Jan 21 '15 13:01 Julian

Any chance you might have hunted down where the problem is?

nope, sorry.

jason-s avatar Jan 21 '15 19:01 jason-s

@jason-s, what is the desired output ?

samarsault avatar Feb 26 '15 05:02 samarsault

Since the schema is

{'additionalProperties': False, 'type': 'object', 'patternProperties': {'^[^\\d\\W]\\w*$': {'type': 'string'}}}

the relative_schema_path (and schema_path) should be

deque(['properties', 'debug', 'properties', 'foo', 'properties', 'bar'])

and not

deque(['properties', 'debug', 'properties', 'foo', 'properties', 'bar', 'additionalProperties'])

The point is that if I take the schema path and find the value located there, it should be consistent with what schema refers to.

As for my desired output, I want something that points me to /debug/foo/bar/44hoho. I get /debug/foo/bar from the path property, I'm missing the 44hoho part.

jason-s avatar Feb 26 '15 13:02 jason-s