python-jsonschema / python-jsonschema/jsonschema

Inaccurate error message while using "unevaluatedProperties": False

Open
#1,169 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement Error Reporting
Dominant language
Python
Stars
5k
Forks
671
Avg merge
1d 1h
Merged PRs (30d)
10

Description

I am using jsonschema to validate JSON data against a schema using "unevaluatedProperties": False. When my data contains an unexpected property, the error message lists all the evaluated properties as unexpected, instead of the actual unevaluated property.

Here is a schema as an example:

{
  "allOf": [
    {
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { 
          "type": "object",
          "properties": {
            "name": { "type": "string" },
          },
          "additionalProperties": False
        }
      }
    },
    {
      "properties": {
        "type":  { "type": "string" },
      }
    }
  ],
  "type": "object",
  "unevaluatedProperties": False
}

And here is a json data:

{
  "street_address": "21 2nd Street",
  "city": "New York",
  "state": {"name": "NY", "cc": "Unexpected field"},
  "type": "home"
}

In this case, 'cc' under 'state' is unexpected. If I remove 'cc', the validation is successful. However, the error message reported by jsonschema is: jsonschema.exceptions.ValidationError: Unevaluated properties are not allowed ('city', 'state', 'street_address' were unexpected).

My questions are:

Why is the error message not mentioning 'cc', the actual cause of the error?
Are there any better ways to provide compatibility with the unevaluatedProperties keyword that could help us identify the specific unevaluated properties?
Please let me know if you need additional details.

Information about my current environment:

Python version: 3.7.16
jsonschema version: 4.16.0
Thanks in advance for your help.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported validation with the schema and data in the issue under the current jsonschema test setup. Trace the unevaluatedProperties handling and add a regression test for the nested state.cc case; done means the reported unevaluated property matches the actual failing property without breaking the successful case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.