【问题标题】:How do I make the property of a JSON object required based upon the value of a second JSON object?如何根据第二个 JSON 对象的值使 JSON 对象的属性成为必需?
【发布时间】:2019-01-24 16:30:04
【问题描述】:

我有一个 JSON 提交,我正在尝试根据模板中定义的一些规则来验证它。此模板定义了向用户提出的许多问题。对于提交,是否需要其中一个问题的答案取决于上一个问题的值。

基本上

Do you have a dog? Yes/No
What kind of dog do you have?

第一个问题的有效答案使用枚举进行保护,因此用户只能提供yesno 字符串作为问题的答案。

如果用户对该问题的回答是肯定的,我希望第二个问题是必需的,这样如果当第一个答案是 yes 时第二个答案留空,则会引发错误。如果第一个答案是no,则用户可以将第二个问题留空。

以下是我目前拥有的 JSON 架构。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "question3-9": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "enum": [
            "Yes", "No"
          ]
        }
      },
      "if": {
        "properties":{
          "answer": {"enum": ["Yes"]}
        }
      },
      "then": {"requried": "#/definitions/question3-257"}
    },
    "question3-257": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "minLength": 1
        }
      }
    }
  },
  "type": "object",
  "properties": {
    "form_submission": {
      "type": "object",
      "properties": {
        "sections": {
          "type": "object",
          "properties": {
            "3": {
              "type": "object",
              "properties": {
                "questions": {
                  "type": "object",
                  "properties": {
                    "9": {
                      "$ref": "#/definitions/question3-9"
                    },
                    "257": {
                      "$ref": "#/definitions/question3-257"
                    }
                  },
                  "required": [
                    "257"
                  ]
                }
              }
            }
          },
          "required": [
            "3"
          ]
        }
      }
    }
  }
}

我认为通过使用 JSON-Schema7 中可用的 if-then-else,我可以将第二个问题设置为 required,但它似乎不像这样工作。

这是使用上述架构验证的提交。

{
  "form_submission": {
    "sections": {
      "3": {
        "questions": {
          "9": {
            "answer": "Yes",
          },
          "257": {
            "answer": "",
          }
        }
      }
    }
  }
}

更新的 JSON 架构:

"3": {
                "type": "object",
                "properties": {
                  "questions": {
                    "type": "object",
                    "properties": {
                      "9": {
                        "$ref": "#/definitions/question3-9"
                      },
                      "257": {
                        "$ref": "#/definitions/question3-257"
                      }
                    },
                    "if": {
                      "properties":{
                        "answer": {"const": "Home improvements (General)"}
                      }
                    },
                    "then": {"required": ["257"]}
                  }
                }
              }

待验证:

  "3": {
    "questions": {
      "9": {
        "answer": "Home improvements (General)",
      },
      "257": {
        "answer": "", //<-- This is an empty string but should be required since the answer to the abvoe question is "Home improvements (general) as defined with "answer": {"const": "Home improvements (General)"}
      }
    }

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    您在使用 if/then 关键字时走在正确的轨道上,但您的 then 中的要求并不完全正确。

    required关键字需要是一个字符串数组,每个字符串表示一个属性名;在这种情况下,属性名称是“257”。

    此外,您需要将if/then 关键字放在questions 属性下,而不是在定义中。通过在定义中包含它,您需要 questions.9 属性具有 257 属性 (questions.9.257),但您希望 questions.257 属性是必需的。

    试试这个:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "definitions": {
        "question3-9": {
          "type": "object",
          "properties": {
            "answer": {
              "type": "string",
              "enum": ["Yes", "No"]
            }
          }
        },
        "question3-257": {
          "type": "object",
          "properties": {
            "answer": {
              "type": "string",
              "minLength": 1
            }
          }
        }
      },
      "type": "object",
      "properties": {
        "form_submission": {
          "type": "object",
          "properties": {
            "sections": {
              "type": "object",
              "properties": {
                "3": {
                  "type": "object",
                  "properties": {
                    "questions": {
                      "type": "object",
                      "properties": {
                        "9": {
                          "$ref": "#/definitions/question3-9"
                        },
                        "257": {
                          "$ref": "#/definitions/question3-257"
                        }
                      },
                      "if": {
                        "properties":{
                          "answer": {"const": "Yes"}
                        }
                      },
                      "then": {"required": ["257"]}
                    }
                  }
                }
              },
              "required": ["3"]
            }
          }
        }
      }
    }
    

    另外,我建议在if 中使用const 关键字。我认为它读起来更明确。

    【讨论】:

    • 您好!因此,我接受了您的建议并尝试将其应用于我的架构和数据。我已经用我的新版本更新了 OP。但是,当我使用在线验证器对此进行验证时,它会毫无错误地处理。我真的想要 257,因为 9 的答案是 Home Improvements (General),我到底做错了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2022-08-22
    • 2019-03-09
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    相关资源
    最近更新 更多