JSONSchema - 必需的属性依赖于父属性

2024-02-26

我想根据根模式中属性的存在在数组子模式中应用额外的“必需”属性。我的架构设置如下:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required":[
       "isParentDependency",
       "subArray"
    ],
    "properties": {
        "isParentDependency": {
            "$id": "#/properties/isParentDependency",
            "type": "boolean"
        },
        "subArray": {
            "$id": "#/properties/subArray",
            "type": "array",
            "items": {
                "$id": "#/properties/subArrayItem",
                "required": ["alwaysRequiredProp"],
                "dependencies": {
                    "isParentDependency":{
                        "required":["requiredPropIfIsParentDependency"]
                    }
                },
                "properties": {
                    "alwaysRequiredProp": {
                        "$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
                        "type": "boolean"
                    },
                    "requiredPropIfIsParentDependency": {
                        "$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
                        "type": "boolean"
                    }
                }
            }
        }
    }
}

传递案例

{
    "isParentDependency": false,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true,
        "requiredPropIfIsParentDependency":true
    }]
}

失败案例

{
    "isParentDependency": true,
    "subArray": [{
        "alwaysRequiredProp": true
    }]
}

显然这行不通,但我一直无法弄清楚如何创建指向根模式的指针(或使用 $ref 应用 if/else 类型解决方案)

非常感谢任何指导!


使用 JSON Schema,每一层嵌套properties树下看不到树上。因此,您必须在需要查看的最高级别定义您的条件。在本例中,这是根级别。

看看这个架构。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "subArray": {
      "type": "array",
      "items": {
        "required": [
          "alwaysRequiredProp"
        ],
        "properties": {
          "alwaysRequiredProp": {
            "type": "boolean"
          },
          "requiredPropIfIsParentDependency": {
            "type": "boolean"
          }
        }
      }
    }
  },
  "required": [
    "isParentDependency",
    "subArray"
  ],
  "properties": {
    "isParentDependency": {
      "type": "boolean"
    },
    "subArray": {
      "$ref": "#/definitions/subArray"
    }
  },
  "if": {
    "properties": {
      "isParentDependency": {
        "const": true
      }
    }
  },
  "then": {
    "properties": {
      "subArray": {
        "items": {
          "required": [
            "requiredPropIfIsParentDependency"
          ]
        }
      }
    }
  }
}

subArray已移至定义然后引用,因此可以更容易地看到条件应用程序逻辑。

的定义subArray仅确定项目结构的属性(如果提供),并且需要alwaysRequiredProp。它不需要requiredPropIfIsParentDependency.

有条件应用程序使用if and then关键词。

If the if模式为真,那么else应用架构。

的价值if是一个模式。它适用于根级别。这个需要isParentDependency be true(它不需要属性本身,因为这是始终需要的。在 if 语句中需要该属性并不意味着该属性始终是必需的,除非else was false,但我们甚至没有使用else here).

When isParentDependency is true, the then应用模式,它沿着树向下查找subArray的物品,要求requiredPropIfIsParentDependency.

您可以使用以下命令在浏览器中测试此功能jsonschema.dev https://jsonschema.dev/s/N4IgziBcLAOgdgAkbEASMBjAFgUwLYCGqkKI2ALhQA6QD0dAVmAPbwC0WeRAdCwE4BzOgBN+hAGYV2ABgDsdLgUIBiVABoEyVBQCe1XCTIsARo1yYKGrWRG4JAS3gOKDtmCNwkyMmACuJgCC-OK6njY+ZHoGRqiEIYRhIJrekaguBB5QiF6RaSD8uACOfg6FIkYA2hF52iCEADYA7olgAErFpeUACvws1Kg1kQC6KbV11H0G-K64WaS543WNLbrtnWW4Ir394alLUfqG2agmLCwNuITwg-u1AL5jB6iFJZvbUwCSEp9g3fG4eAUAAiuAM8Ds8EwSQWQ3GOiOsRAZwuVxuIDhkXumMQ2LuuJqeOQjxsLw25SqNXSfwBQNB4Mh0Os+1Q-iCCSSNlGpJAk36uBmDjme3yDhphTpYMBjJhOThCJiJ2R50u11uWKedTZwVCIryqDQhQkSJUonsThcbngYEUAR1iXVPiJuKe6WN2UWEymAtm8zl+Op-wlIKlEMBTI9ONQmHcVmyFH4fkM+OdxJsJO8Ojw6NhLN53sFwsjAfAdo5etq6QomQr8IK5K2lPxlfrbx6Xx+4sBIYZ4c5zZ8wxxqadhPTCHuyRADigMB5YqD3fp0r7RgTSddpfZuuy1X2np8cWarQ6ba2OwG8cTyaxXInIHuQA(预加载了此架构和您的失败​​实例)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSONSchema - 必需的属性依赖于父属性 的相关文章

随机推荐