繁体   English   中英

Json 模式未按预期验证

[英]Json schema not validating as expected

我正在使用 Json 模式进行验证,但它似乎不起作用。 除了 if then 条件之外,其余的都可以正常工作,但即使我遵循 jsonschema - Applying Subschema Conditionally 中提供的示例,它也不起作用。 这是架构

'''

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "identifierKey": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(_[a-z0-9]+)*$",
      "minLength": 2
    },
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
      "minLength": 36,
      "maxLength": 36
    },
    "mail_identifierKey": {
      "pattern": "^(?:(?!.*?[.]{2})[a-zA-Z0-9](?:[a-zA-Z0-9.+!%-]{1,64}|)|\"[a-zA-Z0-9.+!% -]{1,64}\")@[a-zA-Z0-9][a-zA-Z0-9.-]+(.[a-z]{2,}|.[0-9]{1,})$"
    },
    "question_type": {
      "enum": [
        "barcode",
        "name",
        "nps",
        "single_choice",
        "text",
        "date",
        "number",
        "id",
        "mail_address",
        "multiple_choice",
        "phone_number",
        "zip_code",
        "rating"
      ]
    },
    "question": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "key": {
          "$ref": "#/definitions/identifierKey"
        },
        "type": {
          "$ref": "#/definitions/question_type"
        }
      },
      "required": ["key", "type"]
    },
    "answers": {
      "type":"array",
      "items": {
        "type": "object",
        "properties": {
          "question": {
            "$ref": "#/definitions/question"
          },
          "status": {
            "type": "string"
          }
        },
        "allOf": [
          {"if": {
            "properties": { "question": { "const": "rating" } } },
          "then": {
            "properties": { "value": { "type": "number", "minimum": 1, "maximum": 5 } } }
          },
          {"if": {
            "properties": { "question": { "const": "zip_code" } } },
           "then": {
            "properties": { "value": { "type": "string", "$ref": "#/definitions/identifierKey","minLength": 4, "maxLength": 10 } } }
          },
          {"if": {
            "properties": { "question": { "const": "phone_number" } } },
          "then": {
            "properties": { "value": { "type": "string", "minLength": 8, "maxLength": 15 } } }
          },
          {"if": {
            "properties": { "question": { "const": "multiple_choice" } } },
           "then": { 
            "properties": { "value": { "type": "array", "minItems": 1, "uniqueItems": true,
                "contains": { "type": "string" } } 
              } 
            } 
          },
          {"if": {
            "properties": { "question": { "const": "single_choice" } } },
           "then": {
            "properties": { "value": {"type": "string","$ref": "#/definitions/identifierKey"} } }
          },
          {"if": {
            "properties": { "question": { "const": "mail_address" } } },
           "then": {
            "properties": { "value": {"type": "string","$ref": "#/definitions/mail_identifierKey"} } }
          },
          {"if": {
            "properties": { "question": { "const": "id" } } },
           "then": {
            "properties": { "value": {"type": "string","$ref": "#/definitions/uuid"} } }
          },
          {"if": {
            "properties": { "question": { "const": "number" } } },
           "then": {
            "properties": { "value": {"type": "number"} } }
          },
          {"if": {
            "properties": { "question": { "const": "text" } } },
           "then": {
            "properties": { "value": {"type": "string"} } }
          },
          {"if": {
            "properties": { "question": { "const": "date" } } },
           "then": {
            "properties": { "value": {"type": "string", "format": "date"} } }
          },
          {"if": {
            "properties": { "question": { "const": "nps" } } },
           "then": {
            "properties": { "value": {"type": "number", "minimum": 1, "maximum":10 } } }
          },
          {"if": {
            "properties": { "question": { "const": "name" } } },
           "then": {
            "properties": { "value": {"type": "string" } } }
          },
          {"if": {
            "properties": { "question": { "const": "barcode" } } },
           "then": {
            "properties": { "value": {"type": "number" } } }
          }
        ]  
      }
    }
  },
  "properties": {
    "answers": {
      "$ref": "#/definitions/answers"
    }
  }
}

'''

这就是我要验证的

''' {
    "$schema": "./blank.json",
    "answers":[ {
        "status": "hello1",
        "question": {
            "key": "hello",
            "type": "rating"
        },
        "value": 1234
    }]
} '''

我正在遵循下面的示例,但无法弄清楚我缺少什么

好的...我看到了问题。

这是您希望为数据触发的if子模式

{
  "properties": {
    "question": { "const": "rating" }
  }
}

但是,在您的数据中, question具有type属性。

"question": {
  "key": "hello",
  "type": "rating"
}

这意味着if子模式没有通过,因为{"key": "hello", "type": "rating" } != "rating"

您需要更新您的if子模式以考虑此属性层次结构。

{
  "properties": {
    "question": {
      "properties": {
        "type": { "const": "rating" }
      }
    }
  }
}

您需要对所有if子模式执行此操作。

仅进行此更改会在我的验证器上产生此错误:

{
  "valid": false,
  "keywordLocation": "#/properties/answers/$ref/items/allOf/0/then/properties/value/maximum",
  "absoluteKeywordLocation": "https://json-everything/base#/definitions/question_type/items/allOf/0/then/properties/value/maximum",
  "instanceLocation": "#/answers/0/value",
  "error": "1234 is greater than or equal to 5"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM