Conditional statement is not working in json schema validation

Viewed 1309

I have a json response like below..

[{

        "views": [{             
                "groups": [{
                        "type": "static",                       
                        "tiles": [{
                                "context": "event",
                                "collection": "selo",
                                "tile_type": "static"
                            }
                        ]
                    }
                ]
            }, {                
                "groups": [{
                        "type": "static",                       
                        "tiles": [{
                                "context": "event",
                                "collection": "nitic",                              
                                "tile_type": "static"
                            }
                        ]
                    }
                ]
            }, {                
                "groups": [{
                        "type": "scrollable",                       
                        "tiles": [{
                                "name": "loca",
                                "location": "cal",                              
                                "tile_type": "person"
                            }, {
                                "name": "tom",
                                "location": "toc",                              
                                "tile_type": "person"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

Here I have to validate the tile objects inside every group array.Based on the type key in the group object the size and key elements in the tile object varies. For isntance if the type key is static the size of the tile object is 1 and if it is value is scrollable it contians more than one tile items.In addition to this the tile elemnts also different.

for static tile i have to verify the existence of following key elemnts

                          "context"
                        "collection"                            
                        "tile_type"

for scrollable tile i have to verify the existence of following key elemnts

                        "name"
                        "location"
                        "tile_type"

Based on these i have defined a schema using switch like this and the schema validation is not working.Instead of switch keyword i tried with anyOf as well.(Iam using the draft7 version)

schema definition

   "switch": [
                    {
                      "if": {
                        "properties": {
                          "tile_type": {
                            "enum": [
                              "static"
                            ]
                          }

                        },
                        "required": [
                          "context",
                          "collection",
                          "tile_type"
                        ]
                      }
                    },
                    {
                      "if": {
                        "properties": {
                          "tile_type": {
                            "enum": [
                              "person"
                            ]
                          }
                        },
                        "required": [
                          "name",
                          "location",
                          "tile_type"
                        ]
                      }
                    }
                  ]

Tried with anyOF

"anyOf": [{
        "properties": {
            "tile_type": {
                "enum": [
                    "static"
                ]
            }

        },
        "required": [
            "context",
            "collection",
            "tile_type"
        ]

    }, {

        "properties": {
            "tile_type": {
                "enum": [
                    "person"
                ]
            }
        },
        "required": [
            "name",
            "location",
            "tile_type"
        ]

    }
]

Errors oberseved while using anyOf

 Found 2 error(s)
Message:
Required properties are missing from object: context, collection.
Schema path:
http://example.com/root.json#/views/groups/tiles/items/required
Message:
Required properties are missing from object: context, collection.
Schema path:
http://example.com/root.json#/views/groups/tiles/items/required

Tried in :https://www.jsonschemavalidator.net/

Any solution to perform this?

Updated part goes below

In the response sometimes the some of the tile data contains the keys errorText and errorCode .

[{

    "views": [{             
            "groups": [{
                    "type": "static",                       
                    "tiles": [{
                            "context": "event",                              
                            "tile_type": "static"
                        }
                    ]
                }
            ]
        }, {                
            "groups": [{
                    "type": "static",                       
                    "tiles": [{
                            "context": "event",
                            "collection": "nitic",                              
                            "tile_type": "static"
                        }
                    ]
                }
            ]
        }, {                
            "groups": [{
                    "type": "scrollable",                       
                    "tiles": [{
                            "name": "loca",
                            "location": "cal",                              
                            "tile_type": "person"
                        }, {

                            "errorText":"Tile failure",
                            "errorCode":1,                              
                            "tile_type": "person"
                        },
                              {

                            "errorText":"Tile not generated",
                            "errorCode":2,                              
                            "tile_type": "person"
                        }
                    ]
                }
            ]
        }
    ]
}

]

In such case I added an extra properties inside existing oneOf array as follows . But it is not working.Whats wrong with my schema definition

 {
                    "properties": {
                      "type": {
                        "const": "scrollable"
                      },
                      "tiles": {
                        "type": "array",
                        "minItems": 2,
                        "items": {
                          "properties": {
                               "errorText": {
                              "const": ["Tile failure", "Tile not generated"]
                           }
                          },
                          "required": [
                            "errorText",
                            "errorCode",
                            "tile_type"
                          ]
                        }
                      }
                    }
                  }

error message while doing schema validations:

Message:
Value "static" does not match const.
Schema path:
#/items/properties/views/items/properties/groups/items/oneOf/2/properties/type/const
Message:
Value "static" does not match const.
Schema path:
#/items/properties/views/items/properties/groups/items/oneOf/1/properties/type/const
1 Answers

Here is a schema which works for your given JSON instance with the following validation rules:

Type can be static or scrollable If type is static, max one item in tiles array, and object properties must be context, collection, and tile_type.

If type is scrollable, at least two items in tiles array, and object properties must be name, location, and tile_type.

Items in a scrollable tile must be unique.

In addition to this the tile elemnts also different

Sorry, this is not possible with JSON Schema.


Also tested using the same online validator you used.

{
  "type": "array",
  "items": {
    "properties": {
      "views": {
        "type": "array",
        "items": {
          "properties": {
            "groups": {
              "type": "array",
              "items": {
                "oneOf": [
                  {
                    "properties": {
                      "type": {
                        "const": "static"
                      },
                      "tiles": {
                        "type": "array",
                        "maxItems": 1,
                        "items": {
                          "propertyNames": {
                            "enum": [
                              "context",
                              "collection",
                              "tile_type"
                            ]
                          },
                          "required": [
                            "context",
                            "collection",
                            "tile_type"
                          ]
                        }
                      }
                    }
                  },
                  {
                    "properties": {
                      "type": {
                        "const": "scrollable"
                      },
                      "tiles": {
                        "type": "array",
                        "minItems": 2,
                        "items": {
                          "propertyNames": {
                            "enum": [
                              "name",
                              "location",
                              "tile_type"
                            ]
                          },
                          "required": [
                            "name",
                            "location",
                            "tile_type"
                          ]
                        }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}
Related