【问题标题】:JSON Schema - using oneOf inside an items arrayJSON Schema - 在 items 数组中使用 oneOf
【发布时间】:2015-10-05 23:38:58
【问题描述】:

我想在我的架构中使用一个数组(项目)。数组中的每个对象都可以是架构中概述的“框架类型”之一。

我正在开发的架构如下:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "required": [
    "Campaign_name",
    "Legals"
  ],
  "properties": {
  "Campaign_name": {
    "type": "string",
    "minLength": 3
  },
  "Legals": {
  "type": "object",
  "properties": {
    "legal-button-label": {
      "type": "string",
      "minLength": 6
    },
    "legal-text": {
      "type": "string",
      "minLength": 15
    }
  }
},
"Banner_120x600": {
  "type": "object",
  "properties": {
    "serve-backup": {
      "type": "object",
      "properties": {
        "choice": {
          "type": "string",
          "enum": [
            "yes",
            "no"
          ]
        },
        "image": {
          "type": "string",
          "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$"
        }
      }
    },
    "background": {
      "type": "string",
      "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$"
    },
    "logo": {
      "type": "string",
      "pattern": "^([a-zA-Z|-]+)([.])(gif|jpeg|jpg|png)$"
    },
    "loop": {
      "type": "integer",
      "enum": [
        0,
        1,
        2
      ]
    },
    "frames": {
      "type": "array",
      "minItems": 1,
      "maxItems": 6,
      "items": {
        "oneOf": [
          {
            "$ref": "#/frame-type/INTRO-FRAME"
          },
          {
            "$ref": "#/frame-type/OFFER-FRAME-TYPE-1"
          }
        ]
      }
    }
  }
}
},
"frame-type": {
    "INTRO-FRAME": {},
    "OFFER-FRAME-TYPE-1": {}
  }
}

但是,JSON 不会针对架构进行验证。我正在开发的 JSON 如下所示:

{
  "Campaign_name": "OSM DT DATA",
  "Legals": {
    "legal-button-label": "Click for Legals",
    "legal-text": "Requires 3G/Wi-Fi. Content depends..."
  },
  "Banner_120x600": {
    "serve-backup": {
  "choice": "no",
  "image": "backup.jpg"
},
"background": "background.png",
"logo": "sky-logo.png",
"loop": 2,
"frames": [
  {
    "type": "INTRO-FRAME"
  },
  {
    "type": "OFFER-FRAME-TYPE-1"
  }
]
}
}

【问题讨论】:

    标签: json validation schema


    【解决方案1】:
    "oneOf": [
          {
            "$ref": "#/frame-type/INTRO-FRAME"
          },
          {
            "$ref": "#/frame-type/OFFER-FRAME-TYPE-1"
          }
        ]
    

    两个 achema 都是空的,如果一个对象匹配多个模式,则 oneOf 会失败,因为任何文档都匹配一个空模式,所以总是会出现这种情况。您可以将 oneOf 更改为 anyOf。

    【讨论】:

    • 我填充了模式,但仍然无法验证。
    • 你是如何填充它们的以及你是如何验证它的?
    • 嗨,我找到了一个不使用 oneOf 的解决方案。我会更新问题并回答。感谢您的时间和建议。
    【解决方案2】:
        "frames": {
            "type": "array",
            "minItems": 1,
            "items": [
                {
                    "type" : "object",
                    "properties" : {
                        "type" : { "enum" : ["INTRO-FRAME"] }   
                    }   
                },
                {
                    "type" : "object",
                    "properties" : {
                        "type" : { "enum" : ["OFFER-FRAME-TYPE-1"] }    
                    }   
                },
                {
                    "type" : "object",
                    "properties" : {
                        "type" : { "enum" : ["OFFER-FRAME-TYPE-2"] }    
                    }   
                },
                {
                    "type" : "object",
                    "properties" : {
                        "type" : { "enum" : ["CONTENT-FRAME"] } 
                    }   
                }
            ]
        }
    

    我删除了“oneOf”的使用,并提供了一组对象来验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2020-06-09
      • 2017-06-24
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      相关资源
      最近更新 更多