JSON Schema `required` 允许使用空字符串作为值

Jak*_*134 4 json jsonschema json-schema-validator

我正在使用 JSON 模式模板来验证在线表单收到的数据。验证器的要求之一是它允许根据对其他问题给出的答案要求一些问题。

例如,如果问题是Do you want a loan?并且用户回答yes,则What is the loan to be used for?需要将问题的答案设置为 required 以便用户必须提供答案。如果答案是,no则不需要第二个问题。

我使用定义来定义我的问题,然后在下面的主要问题模式中引用它们。我通过使用 Draft-07 中提供的 if-then-else 功能读到了这一点,我可以使用它根据其他问题的答案来设置某些问题。

在这个特定的例子中,我想要发生的是,如果用户输入Home improvements (General)问题 9的答案,那么问题 257 将被设置为必需且必须回答,否则应该抛出错误。

目前,当我将此验证器输入https://www.jsonschemavalidator.net/ 时,它无法按预期工作。实际情况是,即使问题 9 的答案是“家庭装修(一般)”,问题 257 的答案也可以留空

如何更改我的架构以提供我想要的行为?

JSON 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "question3-9": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
          "enum": [
            "Home improvements (General)",
            "Other"
          ]
        }
      }
    },
    "question3-257": {
      "type": "object",
      "properties": {
        "answer": {
          "type": "string",
        }
      }
    }
  },
  "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": {
                      "9": {
                        "properties": {
                          "answer": {
                            "enum": [
                              "Home improvements (General)"
                            ]
                          }
                        }
                      }
                    }
                  },
                  "then": {
                    "required": [
                      "257"
                    ]
                  }
                }
              }
            }
          },
          "required": [
            "3"
          ]
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

要验证的 JSON:

{
  "form_submission": {
    "sections": {
      "3": {
        "questions": {
          "9": {
            "answer": "Home improvements (General)",
          },
          "257": {
            "answer": "",
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

更新如果-那么

"if": {
  "properties": {
    "9": {
      "properties": {
        "answer": {
          "enum": [
            "Home improvements (General)"
          ]
        }
      },
      "required": ["answer"]
    }
  },
  "required": ["9"]
},
"then": {
  "257": {
    "properties":{
      "answer":{
        "minLength": 1
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Rel*_*ual 6

您的问题是您希望required检查密钥的值,而事实并非如此。

当前草案 7 规范要求:

如果数组中的每一项都是实例中属性的名称,则对象实例对该关键字有效。

这意味着required只检查对象的键是否存在。它与价值无关。对于字符串验证,请参见适用于字符串的验证关键字。我怀疑你想要minLengthpattern(这是正则表达式)。

https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3

  • 非常好的答案。“必需”和“字符串约束”之间的区别。就像 ps 一样,如果字段为“null”,则强制执行“required”,但当字段为空时则不强制执行。如果你问我,这个答案应该被选为正确答案。 (2认同)