I trying to create a json schema for an object which can contain either array of expressions or array of arrays of expression etc. with any number of levels.
This is what I got so far:
{
"type": "object",
"properties": {
"expression": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/definitions/singleQuery"
},
{
"$ref": "#/definitions/orGroup"
}
]
},
"minItems": 1
}
},
"definitions": {
"orGroup": {
"type": "object",
"properties": {
"or": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/definitions/singleQuery"
},
{
"$ref": "#/definitions/orGroup"
}
]
},
"minItems": 1,
"additionalProperties": false
}
}
},
"singleQuery": {
"type": "object",
"properties": {
"queryString": {
"type": "string"
},
"disabled": {
"type": "boolean"
}
},
"required": [
"queryString"
]
}
}
}
However I am using https://www.jsonschemavalidator.net/ to verify schema is working correctly and with such implementation data like the following will still pass the validation:
{
"expression": [{}]
}
What I am trying to accomplish is validation to expect expression object always have either orGroup or/and singleQuery and orGroup should always require at least one object with queryString. orGroups can be nested in any number of levels, but can't be empty. Here is an example of query:
{
"expression": [
{
"or": [
{
"or": [
{
"queryString": "SELECT * from users WHERE..."
},
{
"queryString": "SELECT * from users WHERE..."
}
]
},
{
"queryString": "SELECT * from users WHERE..."
}
]
},
{
"queryString": "SELECT * from users WHERE..."
}
]
}
This query should pass but it wouldn't if at least one of the object didn't have a queryString.