首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJV模式验证错误

AJV模式验证错误
EN

Stack Overflow用户
提问于 2017-06-22 12:47:36
回答 2查看 780关注 0票数 0

我的输入如下所示

代码语言:javascript
复制
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}

条件是,如果类型是'itemId‘,那么'itemId’应该是必需的字段,如果类型是'fieldGroup‘或’分段‘,则’itemId‘是可选的。

这是我尝试过的Json模式,它不像预期的那样工作,

代码语言:javascript
复制
"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"
                ]
            }
        }

    }
}

任何使用示例Json实现上述用例的帮助/解决方案都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-22 13:46:46

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

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

类型:"string",null

使用必需的属性。

我更改了您的模式,而不是有单独的定义,但除此之外(以及必需的使用)是相同的:

代码语言:javascript
复制
{
  "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"
                ]
              }
            }
          }
        ]
      }
    }
  }
}
票数 0
EN

Stack Overflow用户

发布于 2017-06-23 04:24:36

以下是您的答案,并对最佳实践和风格进行了一些清理。诀窍是,您需要使用隐含的"a意味着b <=> (而不是a)或b“。在本例中,您有"type = field意味着itemId是必需的,<=>类型不是字段,或者itemId是必需的“。

代码语言:javascript
复制
{
  "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"] }
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44699858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档