Psl
Psl

Reputation: 3920

JSON Schema validation is not working

I have a json response like below. In the views array may or may not contain actions array. enter image description here In the reponse If any of view array contains actions object , then i have to validate that actions data with json schema(schema1.json) And in the schema i mentioned the action propertes like(type,label,localizedlabel) as required ones. But when I modify key or value type of this type,label,localizedlabel in the response does not output any errors. Tested via( https://www.jsonschemavalidator.net/). what wrong with my schema? How can i validate actions object only whenever it present inside any of view array?

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

response 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"
                }
            }

        ]
    }
]

Upvotes: 1

Views: 2474

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24409

There's nothing wrong with your schema. It should do what you need if used properly. Because it only describes the "views" part of the schema you would need to iterate through your response and pass just the "views" part of each item to the validator one at a time.

Or, you could add enough of the response structure to the schema to validate everything at once. Then you could just pass your whole response to the validator.

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

Upvotes: 2

Related Questions