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

标签 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"
          ]
        }
      }
    }
  }
}

要验证的 JSON:
{
  "form_submission": {
    "sections": {
      "3": {
        "questions": {
          "9": {
            "answer": "Home improvements (General)",
          },
          "257": {
            "answer": "",
          }
        }
      }
    }
  }
}

更新如果-那么
"if": {
  "properties": {
    "9": {
      "properties": {
        "answer": {
          "enum": [
            "Home improvements (General)"
          ]
        }
      },
      "required": ["answer"]
    }
  },
  "required": ["9"]
},
"then": {
  "257": {
    "properties":{
      "answer":{
        "minLength": 1
      }
    }
  }
}

最佳答案

您的问题是您正在等待 required检查键的值,它没有。
当前草案 7 规范要求:

An object instance is valid against this keyword if every item in the array is the name of a property in the instance.


这意味着 required只检查对象的键是否存在。
它与值(value)无关。
对于字符串验证,请参见适用于字符串的验证关键字。我怀疑你想要 minLengthpattern (这是正则表达式)。
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.3

关于JSON Schema `required` 允许值为空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54399509/

相关文章:

java - 如何将包含文本的 HTML 页面转换为 JSON

json - 如何从 JAXB 注释类生成 JSON 模式?

c# - JsonSchemaGenerator 字符串的自定义 ContractResolver

JSON 架构 - 除了架构中声明的字段之外,实例没有其他字段

python - 将 JSON 文档展平为单行

PHP,你为什么要转义我的引号?

json - 没有固定属性列表的对象的 Swagger Yaml 模式定义

json - JSON模式-递归模式定义

java - Json 架构验证失败

java - REST Assured with JSON 模式验证不起作用