jsonschema: required, additionalProperties, propertyNames, dependentRequired and any other validators that reference property *names* need a way to indicate the erroring names in ValidationErrors

Trying to figure out if there is a way to retrieve the name of a missing required property from an error (without hacking it out of error.message).

For example:

from jsonschema import Draft4Validator as Validator

schema = {
    'type': 'object',
    'properties': {
        'a': {
            'type': 'string'
        },
        'b': {
            'type': 'string',
            'pattern': '^hello$'
        }
    },
    'required': ['a', 'b']
}

errors = Validator(schema).iter_errors({'b': 'world'})
for error in errors:
    print error.path, error.schema_path, error.message

# deque([]) deque(['required']) 'a' is a required property
# deque(['b']) deque(['properties', 'b', 'pattern']) 'world' does not match '^hello$'

In error.path and error.schema_path there is no mention of ‘a’. Is there a way to determine that the property missing was ‘a’ without using error.message?

About this issue

  • Original URL
  • State: open
  • Created 11 years ago
  • Reactions: 10
  • Comments: 29 (10 by maintainers)

Commits related to this issue

Most upvoted comments

For reference, I currently use the following workaround:

# Custom validators
def _required(validator, required, instance, schema):
    '''Validate 'required' properties.'''
    if not validator.is_type(instance, 'object'):
        return

    for index, requirement in enumerate(required):
        if requirement not in instance:
            error = jsonschema.ValidationError(
                '{0!r} is a required property'.format(requirement)
            )
            error.schema_path.append(index)
            yield error


# Construct validator as extension of Json Schema Draft 4.
Validator = jsonschema.validators.extend(
    validator=jsonschema.validators.Draft4Validator,
    validators={
        'required': _required
    }
)

We also bumped in a similar limitation, accessing the list of fields for errors like Additional properties are not allowed ('field1', 'field2' were unexpected).

As said earlier, with a minimum of indication on the expected strategy we can submit a PR 😃

Thanks !

I’m glad I found this issue. This has been my question for the last couple of weeks. I’d been working on a solution to this when I came across this thread.

My 2 cents: ValidationError is far too general. I like the idea that @fre-sch had about specializing the errors. The exceptions thrown should be specific to the case they refer to. For instance, I should be able to catch an AdditionalPropertiesError when I specifically looking for validation errors regarding additional properties (say I’m tallying the validation failures when an additional property is present). And since I’m catching an AdditionalProperitesError, the documentation for that error will show what attributes contain the information I’m after. I’m of the mind that errors can have arbitrary bits attached to them as long as they are documented. Other than the attributes that Exception defines, I don’t feel like these specialized exceptions should have an attribute convention. The attributes should be specific to the exception and documented (like they are with the built-in exceptions). I envision each validation function having its own exception.

@leplatrem the expected strategy I think is still to just introduce extra_info, which is the most reasonable thing I think unfortunately of the current options.

I’ve came to this issue too, it would be nice to know what property caused an error, not only for the required validator.

Maybe a good approach is to return the JSON Path of the property: http://goessner.net/articles/JsonPath/

I agree this would be useful info to have. I’m not so sure about having it in schema_path though, as currently that always points to the json schema keyword, and I like that consistency. I sorta like the extra_info idea, as there are some other validators that could use it, for example, it would be nice to know with the additionalProperties validator which extra properties caused it to fail.