Skip to content

Annoying behaviour of additionalProperties validation when using draft3/extends #167

@bustbr

Description

@bustbr

Hi,

there seems to be a problem with validating against the additionalProperties: false constraint when extending a schema using draft 3.

import json, jsonschema

validator = jsonschema.Draft3Validator(json.loads('''{
    "type": "object",
    "extends": {
        "properties": {
            "id": {
                "type": "string"
            }
        }
    },
    "properties": {},
    "additionalProperties": false
}'''))
instance = {'id': 'should be allowed.'}
print([e.message for e in validator.iter_errors(instance)])

=>
["Additional properties are not allowed ('id' was unexpected)"]

I know about the problematic nature of the wording in the draft 3 specs (the behavior of extends can be seen as validating an instance against all constraints in the extending schema as well as the extended schema(s)) and that draft 4 should be preferred, but is there any chance this will be fixed?

In my real world application extends is basically used to emulate mutli level inheritance using $refs, so it's quite a bit more complex than my example here.
In case this won't/can't be fixed in the library, I came up with 3 ways to fix this on the application side so far:

  1. Rewrite all schemas for draft 4.
  2. Get rid of all the extends and inline everything.
  3. Add empty property definitions (i.e. declarations) for all inherited properties, so for my example above that would mean adding "id": {} to the schema's properties.
    1 & 2 being the most time consuming, 3 being kind of an ugly hack. Can you think of yet another, more practical solution?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions