2

我正在尝试编写一个 JSONSchema 以与 react-jsonschema-form 一起使用,但是无法让依赖项以正确的顺序工作。

  "properties":{
      "condition": {
          "type":"string",
          "enum":["1","2"],
           "options": {
           "dependencies":[
              {"id":"one","value":"1"},
              {"id":"two","value":"2"}
              ]
           }
      },
      "one":{
          "id":"one",
          "type":"number"
          },
      "two":{
          "id":"two",
          "type":"string"
      },
      "misc": {
        "type": "string"
      }
  },
  "type": "object"
}

在上面,我希望有效的表单数据是......

{
  "condition": "1",
  "one": 123
  "misc": "abc"
}

或者如果条件是“2”...

{
  "condition": "2",
  "two": "something",
  "misc": "abc"
}

但是,依赖项部分似乎并没有像我希望的那样工作。因此,无论条件值如何,“一”和“二”都可以存在。

{
  "condition": "1",
  "one": 123,
  "two": "something",
  "misc": "abc"
}

如何更改 JSON 模式,以便根据“条件”仅允许相应的字段。

注意...顺序在提供的架构中确实很重要。有关演示,请参阅https://rjsf-team.github.io/react-jsonschema-form/

4

1 回答 1

2

您必须使用oneOf运算符。它需要多个依赖项,如果其中一个为真,则 json 是有效的。

例子:

"oneOf":[
    {
      "required": [
        "name",
        "dateOfBirth"
      ]
    },
    {
      "required": [
        "name",
        "age"
      ]
    }

更多示例可以在这里找到。

于 2020-02-16T08:57:45.710 回答