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

Issue with unevaluatedProperties and allOf and arrays #1026

Open
silversurfer34 opened this issue Dec 6, 2022 · 1 comment
Open

Issue with unevaluatedProperties and allOf and arrays #1026

silversurfer34 opened this issue Dec 6, 2022 · 1 comment
Labels
Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.

Comments

@silversurfer34
Copy link

I need to have a schema allowing extra fields only under one specific node (ExtensionProperties).
It works well using "additionalProperties": false with simple schemas:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "rootppty": {
            "type": "string"
        },
        "ExtensionProperties": {
            "type": "object"          
        }
    }
}

This data is ok

{
  "rootppty": "ad",
   "ExtensionProperties": {
      "whatever": "you want"
    }
}

and this one fails validation as expected

{
  "rootppty": "ok",
  "other": "fail",
   "ExtensionProperties": {
      "whatever": "you want"
    }
}

But when I have some allOf, I need to use "unevaluatedProperties": false as explained in the documentation
It works well with simple schemas

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "rootppty": {
            "type": "string"
        },
        "data": {
          "unevaluatedProperties": false,
          "allOf": [{
            "type": "object",
            "title": "IndividualProperties",
            "properties": {
              "Nestedppty": {
                "type": "string"
              }
            }
          }
         ]
        },
        "ExtensionProperties": {
            "type": "object"          
        }
    }
}

This data fails the validation as expected (on rootppty2 and Nestedppty2)

{
  "rootppty": "ok",
  "rootppty2": "fail",
  "data": {
    "Nestedppty": "ok",
    "Nestedppty2": "fail"
  },
   "ExtensionProperties": {
      "whatever": "you want"
    }
}

But when I am adding array in the allOf part I am facing issues
This schema does not fail validation on additional fields in the array

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "rootppty": {
            "type": "string"
        },
        "data": {
            "unevaluatedProperties": false,
            "allOf": [{
                    "type": "object",
                    "title": "IndividualProperties",
                    "properties": {
                        "Nestedppty": {
                            "type": "string"
                        },
                        "Nestedarray": {
                            "type": "array",
                            "items": {
                                "unevaluatedProperties": false,
                                "type": "object",
                                "properties": {
                                    "additionalProperties": false,
                                    "nestedarrayInternalpty": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        },
        "ExtensionProperties": {
            "type": "object"
        }
    }
}

for example this data should fail validation on nestedarrayInternalpty2

{
  "rootppty": "ok",
  "data": {
    "Nestedppty": "ok",
    "Nestedarray": [
      {"nestedarrayInternalpty": "ok"},
      {"nestedarrayInternalpty2": "should fail"}
    ]
  },
   "ExtensionProperties": {
      "whatever": "you want"
    }
}

but it fails also on Nestedppty, which should be ok
Am I doing something wrong or is there an issue when several "unevaluatedProperties": false are nested ?

@Julian Julian added the Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis. label Apr 12, 2023
@Julian
Copy link
Member

Julian commented Jun 5, 2023

(Even though this issue itself needs simplifying to be ready to look at) another example of unevaluatedProperties producing poor error messages is the simple schema (which I originally posted to #1074):

from jsonschema import Draft202012Validator
schema = {
    "required": ["foo"],
    "unevaluatedProperties": False,
    "oneOf": [{"required": ["bar"]}],
}
Draft202012Validator(schema).validate({"foo": 37})

which produces the confusing:

Traceback (most recent call last):
  File "/Users/julian/Desktop/uP.py", line 7, in <module>
    Draft202012Validator(schema).validate({"foo": 37})
  File "/Users/julian/Development/jsonschema/jsonschema/validators.py", line 424, in validate
    raise error
jsonschema.exceptions.ValidationError: Unevaluated properties are not allowed ('foo' was unexpected)

Failed validating 'unevaluatedProperties' in schema:
    {'oneOf': [{'required': ['bar']}],
     'required': ['foo'],
     'unevaluatedProperties': False}

On instance:
    {'foo': 37}

which can be especially bad if the oneOf (or applicator in general) produces some very deep error. Here it's probably best_match that needs enhancing to traverse into the applicators for this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Simplification An issue which is in need of simplifying the example or issue being demonstrated for diagnosis.
Projects
None yet
Development

No branches or pull requests

2 participants