Json Schema Draft-07,根据另一个属性值制作所需属性

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

我知道这里存在这个问题,但我尝试了所有解决方案,但没有成功。 我的架构有两个属性,当第一个属性为 true 时,将显示第二个属性。为此,我使用了“选项/依赖项”。我想当第一个属性为 true 时需要第二个属性。

    "authorizationNumber": {
      "type": "object",
      "format": "grid-strict",
      "required": ["firstProperty"],
      "properties": {
        "firstProperty": {
          "type": "boolean",
          "default": false,
          "options": {
            "grid_columns": 3
          }
        },
        "secondProperty": {
          "type": "array",
          "format": "select2",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "type": "string",
            "$ref": "#/definitions/serviceCode"
          },
          "options": {
            "dependencies": {
              "firstProperty": true
            },
            "select2": {
              "closeOnSelect": true,
              "tags": false
            },
            "grid_columns": 4
          }
        }
      }
    }

我尝试了 if/then/else 如下:

    "authorizationNumber": {
      "type": "object",
      "format": "grid-strict",
      "required": ["firstProperty"],
      "if": {
        "properties": {
          "authorizationNumberAvailable":{
            "const": true
          }
        }
      },
      "then": {
        "required": ["secondProperty"]
      },
      "properties": {
        "firstProperty": {
        "type": "boolean",
        "default": false,
        "options": {
          "grid_columns": 3
        }
       },
      "secondProperty": {
        "type": "array",
        "format": "select2",
        "uniqueItems": true,
        "minItems": 1,
        "items": {
          "type": "string",
          "$ref": "#/definitions/serviceCode"
        },
        "options": {
          "dependencies": {
            "firstProperty": true
          },
          "select2": {
            "closeOnSelect": true,
            "tags": false
          },
          "grid_columns": 4
        }
      }
    }
  }

这不会给出任何错误,但它没有按要求设置“secondProperty”。 我还尝试用“allOf”包装 if/then,因为它是建议的答案之一。总而言之,当“firstProperty”设置为 true 时,“secondProperty”应该可用并根据需要进行标记。

json jsonschema react-jsonschema-forms
1个回答
0
投票

您的

if, then
陈述处于错误的水平。

试试这个

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "authorizationNumber": {
      "type": "object",
      "format": "grid-strict",
      "required": [
        "firstProperty"
      ],
      "properties": {
        "firstProperty": {
          "type": "boolean",
          "default": false,
          "options": {
            "grid_columns": 3
          }
        },
        "secondProperty": {
          "type": "array",
          "uniqueItems": true,
          "minItems": 1,
          "items": {
            "$ref": "#/definitions/serviceCode"
          },
          "options": {
            "dependencies": {
              "firstProperty": true
            },
            "select2": {
              "closeOnSelect": true,
              "tags": false
            },
            "grid_columns": 4
          }
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "firstProperty": {
            "const": true
          }
        }
      },
      "then": {
        "required": [
          "secondProperty"
        ]
      }
    }
  ],
  "definitions": {
    "serviceCode": {// you didn't include this definition }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.