0

JSON Schema validation is not validating Array items when items are declared using $ref in the Schema. It allows additional properties in the object which are not present in the schema although additionalProperties is set to False in the root schema object. Here's a sample schema json schema where you can reproduce this issue.

{
  "StudentDetails": {
    "$ref": "#"
  },
  "Student":
  {
     "type": "object",
     "properties": {
        "name": {
            "description": "A link that can be used to fetch the next batch of results. The link contains an opaque continuation token.",
            "nullable": "true",
            "type": "string"
        },
        "age": {
            "description": "The average rating of the title",
            "type": "number",
            "format": "double"
        }
     }
  },
  "type": "object",
  "additionalProperties": false,
  "unevaluatedItems": false,
  "unevaluatedProperties": false,
  "properties": {
    "Students": {
      "type": "array",
      "items": {
        "$ref": "#/Student"
      }
    }
  }
}

And here's an example object which should ideally fail the validation, but it actually succeeds.

       {
           ""Students"": [
                {
                     ""name"": ""X""
                },
                {
                     ""age"": 20
                },
                {
                     ""height"": 6
                }
           ]
         }

I expect that JSChema validation would fail saying "height" property is unevaluated.

I only see this issue with Arrays though. Any direct object validation works perfectly fine even with nested object structure.

Can someone please help me address this issue ?

I was testing out the JSchema validator for edge cases and ran into this issue where Array objects are not validated correctly.

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
  • What validator library do you use? What JSON Schema version do you use (e.g. 2020-12)? Can you post sample code showing how you do validation? – Helen May 31 '23 at 09:13
  • using Newtonsoft.Json.Linq; using Newtonsoft.Json.Schema; string fileName = @"tudendetailsschema.json"; string text = File.ReadAllText(fileName); JSchema schema = JSchema.Parse(text); string jsonSchemaString = @" { ""Students"": [ { ""age"": 20 }, { ""height"": 6 } ] }"; JObject schemaObject = JObject.Parse(jsonSchemaString); bool isValid = schemaObject.IsValid(schema, out IList errorMessages); – Sravanthi Tururmella Jun 13 '23 at 23:39

1 Answers1

0

There is no version of the JSON Schema spec that both accepts "nullable": true (null was added as a possible type), and "unevaluatedProperties": false. What implementation are you using? Regardless, it is buggy as using a $ref inside an items is perfectly valid.

Ether
  • 53,118
  • 13
  • 86
  • 159