JSON架构验证无效

时间:2018-04-06 08:29:37

标签: json schema jsonschema json-schema-validator

我有一个json响应,如下所示。在视图数组中可能包含也可能不包含actions数组。 enter image description here 在响应中如果任何一个视图数组包含动作对象,那么我必须使用json模式验证该动作数据(schema1.json) 在模式中,我提到了像(type,label,localizedlabel)这样的动作属性作为必需的属性。 但是当我修改此类型的键或值类型时,标签,响应中的localizedlabel不会输出任何错误。 通过(https://www.jsonschemavalidator.net/)测试。我的架构有什么问题? 如果只在任何视图数组中出现动作对象,我怎么才能验证它?

schema1.json

{
  "$id": "",
  "type": "array",
  "items": {
    "$id": "/items",
    "type": "object",
    "properties": {
      "name": {
        "$id": "/items/properties/name",
        "type": "string",
        "title": "The Name Schema ",
        "default": "",
        "examples": [
          "Preview"
        ]
      },
      "displayOrder": {
        "$id": "/items/properties/displayOrder",
        "type": "integer",
        "title": "The Displayorder Schema ",
        "default": 0,
        "examples": [
          1
        ]
      },
      "actions": {
        "$id": "/items/properties/actions",
        "type": "array",
        "items": {
          "$id": "/items/properties/actions/items",
          "type": "object",
          "properties": {
            "type": {
              "$id": "/items/properties/actions/items/properties/type",
              "type": "string",
              "title": "The Type Schema ",
              "default": "",
              "examples": [
                "watch"
              ]
            },
            "label": {
              "$id": "/items/properties/actions/items/properties/label",
              "type": "string",
              "title": "The Label Schema ",
              "default": "",
              "examples": [
                "Watch"
              ]
            },
            "localizedLabel": {
              "$id": "/items/properties/actions/items/properties/localizedLabel",
              "type": "object",
              "properties": {
                "ENG": {
                  "$id": "/items/properties/actions/items/properties/localizedLabel/properties/ENG",
                  "type": "string",
                  "title": "The Eng Schema ",
                  "default": "",
                  "examples": [
                    "Watch"
                  ]
                },
                "ESP": {
                  "$id": "/items/properties/actions/items/properties/localizedLabel/properties/ESP",
                  "type": "string",
                  "title": "The Esp Schema ",
                  "default": "",
                  "examples": [
                    "Ver"
                  ]
                }
              }
            }
          },
          "required": [
            "type",
            "label",
            "localizedLabel"
          ]
        }
      },
      "localizedName": {
        "$id": "/items/properties/localizedName",
        "type": "object",
        "properties": {
          "ENG": {
            "$id": "/items/properties/localizedName/properties/ENG",
            "type": "string",
            "title": "The Eng Schema ",
            "default": "",
            "examples": [
              "Preview"
            ]
          },
          "ESP": {
            "$id": "/items/properties/localizedName/properties/ESP",
            "type": "string",
            "title": "The Esp Schema ",
            "default": "",
            "examples": [
              "Adelanto"
            ]
          }
        }
      }
    },
    "required": [
      "actions"
    ]
  }
}

回应json

[{

        "season": 2017,
        "teamData": {
            "awayTeam": {
                "id": 6,
                "city": "Dallas",
                "name": "Mavericks",
                "abbr": "DAL",
                "color": "#0B51A1"
            },
            "homeTeam": {
                "id": 8,
                "city": "Detroit",
                "name": "Pistons",
                "abbr": "DET",
                "color": "#990300"
            }
        },
        "views": [{
                "name": "Preview",
                "displayOrder": 1,
                "groups": [{
                        "type": "static",
                        "displayOrder": 1,
                        "tiles": [{
                                "context": "event",
                                "collection": "event",
                                "auditType": "pregame-preview",
                                "displayOrder": 1,
                                "_id": "5ac58ea21ee2112b33291f1c",
                                "eventId": 2018040608,
                                "dimensions": {
                                    "width": 372,
                                    "height": 375
                                },
                                "tileId": "36b154e719d7d8397da487cbc4e5f7d1",
                                "renderTime": "2018-04-05T02:49:05+00:00",
                                "dataTime": "2018-04-05T02:48:58+00:00",
                                "dataStamp": 1522896538,
                                "location": "http://test.com/2018040608/static/pre-event/pregame-preview/1522896538.png",
                                "tile_type": "static"
                            }
                        ]
                    }
                ],
                "actions": [{
                        "type": "watch",
                        "label": "Watch",
                        "localizedLabel": {
                            "ENG": "Watch",
                            "ESP": "Ver"
                        }
                    }, {
                        "type": "record",
                        "label": "Record",
                        "localizedLabel": {
                            "ENG": "Record",
                            "ESP": "Grabar"
                        }
                    }, {
                        "type": "tile_overlay",
                        "label": "Current Standings",
                        "tili": {
                            "context": "event",
                            "collection": "event",
                            "auditType": "full-standings",
                            "_id": "5ac6f9de2ccaf768d092c918",
                            "eventId": 2018040608,
                            "dimensions": {
                                "width": 1140,
                                "height": 660
                            },
                            "tileId": "852f92537e68dc99b54f1228459ec9ef",
                            "renderTime": "2018-04-06T04:38:54+00:00",
                            "dataTime": "2018-04-06T04:38:52+00:00",
                            "dataStamp": 1522989532,
                            "location": "http://test.com/2018040608/static/pre-event/full-standings/1522989532.png"
                        },
                        "localizedLabel": {
                            "ENG": "Current Standings",
                            "ESP": "Posición actual"
                        }
                    }, {
                        "type": "favorite",
                        "label": "Favorite",
                        "localizedLabel": {
                            "ENG": "Favorite",
                            "ESP": "Favorito"
                        }
                    }
                ],
                "localizedName": {
                    "ENG": "Preview",
                    "ESP": "Adelanto"
                }
            }, {
                "name": "Team Stats",
                "displayOrder": 2,
                "groups": [{
                        "type": "static",
                        "displayOrder": 1,
                        "tiles": [{
                                "context": "event",
                                "collection": "event",
                                "auditType": "pregame-team_stats",
                                "displayOrder": 1,
                                "_id": "5ac6755a4f82eb58a5eae6a6",
                                "eventId": 2018040608,
                                "dimensions": {
                                    "width": 372,
                                    "height": 510
                                },
                                "tileId": "1302dc16c9fe68c3e6edadd98afce2bc",
                                "renderTime": "2018-04-05T19:13:30+00:00",
                                "dataTime": "2018-04-05T19:13:28+00:00",
                                "dataStamp": 1522955608,
                                "location": "http://test.com/2018040608/static/pre-event/pregame-team_stats/1522955608.png",
                                "tile_type": "static"
                            }
                        ]
                    }
                ],
                "localizedName": {
                    "ENG": "Team Stats",
                    "ESP": "Estadísticas del equipo"
                }
            }, {
                "name": "Leaders",
                "displayOrder": 3,
                "groups": [{
                        "type": "static",
                        "displayOrder": 1,
                        "tiles": [{
                                "context": "event",
                                "collection": "event",
                                "auditType": "pregame-leaders",
                                "displayOrder": 1,
                                "_id": "5ac26eb31ee2112b3328b00c",
                                "eventId": 2018040608,
                                "dimensions": {
                                    "width": 372,
                                    "height": 510
                                },
                                "tileId": "96abc24c47d61327426ef2b24281acbf",
                                "renderTime": "2018-04-02T17:55:57+00:00",
                                "dataTime": "2018-04-02T17:55:54+00:00",
                                "dataStamp": 1522691754,
                                "location": "http://test.com/2018040608/static/pre-event/pregame-leaders/1522691754.png",
                                "tile_type": "static"
                            }
                        ]
                    }
                ],
                "localizedName": {
                    "ENG": "Leaders",
                    "ESP": "Líderes"
                }
            }

        ]
    }
]

1 个答案:

答案 0 :(得分:1)

您的架构没有任何问题。如果使用得当,它应该做你需要的。因为它只描述了"观点"模式的一部分,您需要迭代您的响应并传递"视图"每个项目的一部分到验证者一次一个。

或者,您可以向架构添加足够的响应结构,以便立即验证所有内容。然后你可以将整个回复传递给验证器。

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "views": { "$ref": "schema1.json" }
    }
  }
}