JSON Schema - 在items数组中使用oneOf

时间:2015-10-02 15:02:25

标签: json validation schema

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

我正在开发的架构如下:

{
  "$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"
  }
]
}
}

2 个答案:

答案 0 :(得分:0)

"oneOf": [
      {
        "$ref": "#/frame-type/INTRO-FRAME"
      },
      {
        "$ref": "#/frame-type/OFFER-FRAME-TYPE-1"
      }
    ]

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

答案 1 :(得分:0)

    "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"并提供了一组要验证的对象。