【问题标题】:AJV schema validation errorAJV 架构验证错误
【发布时间】:2017-06-22 12:47:36
【问题描述】:

我有如下的输入 json,

{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}

条件是, 如果类型是 'field',则 'itemId' 应该是必填字段 如果类型是'fieldGroup'或'subSection',则'itemId'是可选的

这是我尝试过的 Json Schema,但它没有按预期工作,

"type": "object",
"additionalProperties": false,
"properties" : {
    "contents" : {
        "type" : "array",
        "items": {"$ref": "#displayItem" }
    }
},
"definitions": {
    "displayItem" : {
        "id": "#displayItem",
        "type": "object",
        "items": {
            "anyOf": [
                {"$ref": "#fieldType"},
                {"$ref": "#fieldGroupSubSectionType"}
            ]
        }
    },

    "fieldType" : {

        "id": "#fieldType",
        "type": "object",
        "additionalProperties": false,
        "properties": {
            "itemId": {
                "type": "string"
            },
            "type": {
                "type": "string",
                "enum": ["field"]
            }
        }

    },

    "fieldGroupSubSectionType" : {

        "id": "#fieldGroupSubSectionType",
        "type": "object",
        "additionalProperties": false,
        "properties": {
            "itemId": {
                "type": [ "string", "null" ]
            },
            "type": {
                "type": "string",
                "enum": [
                    "fieldGroup",
                    "subSection"
                ]
            }
        }

    }
}

感谢任何使用 Sample Json Schema 实现上述用例的帮助/解决方法。

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    如果我正确理解了您想要的内容的描述,那么您提供的 json 示例无效,因为它有一个类型:“field”但没有“itemId”属性。

    假设这是真的。而不是使用

    类型:[“字符串”,空]

    使用必需的属性。

    我稍微更改了您的架构,而不是单独的定义,我将它们内联,但除此之外(以及所需的使用)是相同的:

    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "contents": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "itemId": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "field"
                    ]
                  }
                },
                "required": [
                  "itemId"
                ]
              },
              {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "itemId": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string",
                    "enum": [
                      "fieldGroup",
                      "subSection"
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

      【解决方案2】:

      这是您对最佳实践和风格的一些清理的答案。诀窍是您需要使用暗示“a 暗示 b (不是 a)或 b”。在这种情况下,您有“type = field 暗示 itemId 是必需的 type 不是 field 或 itemId 是必需的”。

      {
        "type": "object",
        "properties": {
          "contents": {
            "type": "array",
            "items": { "$ref": "#/definitions/displayItem" }
          }
        },
        "definitions": {
          "displayItem": {
            "type": "object",
            "properties": {
              "itemId": { "type": "string" },
              "type": { "enum": ["field", "fieldGroup", "subSection"] }
            },
            "anyOf": [
              { "not": { "$ref": "#/definitions/fieldType" } },
              { "required": ["itemId"] }
            ]
          },
          "fieldType": {
            "properties": {
              "type": { "enum": ["field"] }
            }
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-30
        • 1970-01-01
        • 2019-11-19
        • 2018-09-13
        • 2021-12-05
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 2019-10-14
        相关资源
        最近更新 更多