Search code examples
jsonjsonschema

additionalProperties true is not respected for nested object in Json schema


In Json schema, how can i specify that the fields within TestParameters/config.yaml object are fixed and no additional fields can be added to config.yaml while keeping additionalProperties:true for the parent Object ExecutionParameters of TestParameters/config.yaml. This way, additional fields like Location can be added to the ExecutionParameters but not within ExecutionParameters/TestParameters/config.yaml. Here is the schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ExecutionParameters": {
      "type":"object",
      "additionalProperties": true,
      "TestParameters": {
        "type":"object",
        "additionalProperties": false,
        "description": "parameters configuration.",
        "properties": {
            "config.yaml": {
              "type": "object",
              "additionalProperties": false,
              "description": "Configuration fields",
              "properties": {
                "warmup_length": {
                  "type": "number"
                },
                "simulation_skip": {
                  "type": "number"
                },
                "clear_stats_after": {
                  "type": "number"
                },
                "trace_length": {
                  "type": "number"
                }
              }
            }
        }
      },
      "description": "Execution parameters for PBT, IWPS, COHO type of Workloads"
    }
  
  }
}

Here is the test json with an invalid field "junk_test" that validates successfully against this schema. I also tried with nodejs ajv schema validation but it still succeeds. RLocation is a random field that should be allowed at the top ExecutionParameters level.

{
 "ExecutionParameters":{
    "RLocation": "RElative_path_to_s3",
    "TestParameters":{
        "config.yaml": {
            "warmup_length":1000,
            "simulation_skip":2000,
            "clear_stats_after":5000,
            "trace_length": 1000000,
            "junk_test":1234
        }
    }
  }
}

Solution

  • You have a problem in your schema:

      "properties": {
        "ExecutionParameters": {
          "type":"object",
          "additionalProperties": true,  // something wierd here.
          "TestParameters": {
            "type":"object",
    

    If you intended TestParameters to be another top-level property, then you need to close the ExecutionParameters subschema

      "properties": {
        "ExecutionParameters": {
          "type":"object",
          "additionalProperties": true
        },
        "TestParameters": {
          "type":"object",
    

    If you intended TestParameters to be part of the ExecutionParameters definition, then you need to wrap it in a properties keyword.

    As it stands, TestParameters is considered an unknown keyword in the ExecutionParameters subschema, and unknown keywords are ignored.