You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置JSON Schema:父对象允许额外字段,嵌套子对象禁止额外字段

问题:修正JSON Schema以限制嵌套对象的额外字段

需实现规则:父对象ExecutionParameters允许添加额外字段(如RLocation),但其嵌套对象TestParameters/config.yaml仅允许固定字段,禁止添加额外字段。当前Schema在测试时,包含无效字段"junk_test"的JSON仍能通过验证,需修正该Schema。

原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"
    }
  }
}

测试JSON

{
 "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
        }
    }
  }
}

问题分析

原Schema的核心错误是**TestParameters没有被定义在ExecutionParameters的properties字段内**,而是直接作为同级字段写在了ExecutionParameters的对象定义中。JSON Schema的语法要求,对象的属性验证规则必须放在properties字段下,否则该规则不会被解析生效,自然无法限制TestParameters及其内部config.yaml的额外字段。

修正后的Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ExecutionParameters": {
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "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"
    }
  }
}

修正说明

将TestParameters移动到ExecutionParameters的properties字段内部后:

  1. ExecutionParameters的additionalProperties: true依然生效,允许添加RLocation这类自定义字段;
  2. TestParameters的additionalProperties: false开始生效,禁止其添加未定义的字段;
  3. config.yaml的additionalProperties: false也能正确生效,像测试JSON中的junk_test这类无效字段会被拦截,无法通过验证。

内容的提问来源于stack exchange,提问作者Vijay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 11:06:13