networknt / json-schema-validator

A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Apache License 2.0
822 stars 323 forks source link

Unevaluated properties and subschemas #967

Closed fvanham-coveo closed 6 months ago

fvanham-coveo commented 6 months ago

I think there's an unclarity with unevaluated properties in combination with subschemas, although I must admit that the json schema spec is not particularly clear on some of the details around unevaluatedProperties. My understanding is the following:

Here's a minimal sample that illustrates my case:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "$defs" : {
    "subschema": {
      "type": "object",
      "required": ["group"],
      "properties": {
        "group": {
          "type": "object",
          "additionalProperties": false,
          "required": ["parentprop"],
          "properties": {
            "parentprop": {
              "type": "string"
            }
          }
        }
      }
    }
  },
  "type": "object",
  "unevaluatedProperties": false,
  "allOf": [
    {"properties": { "group" : {"type":"object"} } },
    {"$ref": "#/$defs/subschema"}
  ],
  "required": ["childprop"],
  "properties": {
    "childprop": {
      "type": "string"
    }
  }
}

This schema has a two subschemas in the allOf, one inline one via a $ref. Running a validation with the following input

{
  "childprop": "something",
  "group": {
     "parentprop":"something",
     "notallowed": false
  }
}

Produces three validation errors with the latest version (1.3.2) :

[$.group: property 'notallowed' is not defined in the schema and the schema does not allow additional properties, $: property 'childprop' must not be unevaluated, $: property 'group' must not be unevaluated]

The first validation error is correct, there is an extra property which should not be there. The next two are incorrect (I think): group should've passed validation in the first allOf schema, even though it hasn't been successfully evaluated in the second allOf schema, due to the extra property. childProp should have passed validation in the top level schema.

Looking back, earlier version (e.g. I verified with 1.1.0) do not produce the childProp validation error.

justin-tay commented 6 months ago

Thanks for the explanation and example.

This is a duplicate of

The fix is already merged to master but hasn't been released yet.

Using your example the following is the only result.

$.group: property 'notallowed' is not defined in the schema and the schema does not allow additional properties