Json 模式中的嵌套对象不考虑additionalProperties true

问题描述 投票:0回答:1

在 Json 模式中,如何指定 TestParameters/config.yaml 对象中的字段是固定的,并且 不能将其他字段添加到 config.yaml,同时为 TestParameters/config.yaml 的父对象 ExecutionParameters 保留additionalProperties:true 。这样,可以将位置等其他字段添加到 ExecutionParameters 中,但不能添加到 ExecutionParameters/TestParameters/config.yaml 中。这是架构:

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

这是带有无效字段 “junk_test” 的测试 json,可根据此架构成功验证。我也尝试过使用nodejs ajv模式验证,但它仍然成功。 RLocation 是一个随机字段,应在顶级 ExecutionParameters 级别允许。

{
 "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
        }
    }
  }
}
json jsonschema
1个回答
0
投票

您的架构有问题:

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

如果您希望

TestParameters
成为另一个顶级属性,那么您需要关闭
ExecutionParameters
子模式

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

如果您希望

TestParameters
成为
ExecutionParameters
定义的一部分,那么您需要将其包装在
properties
关键字中。

就目前情况而言,

TestParameters
ExecutionParameters
子模式中被视为未知关键字,并且未知关键字将被忽略。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.