{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "ql": {
      "type": "string",
      "const": "nql1"
    },
    "expression": {
      "anyOf": [
        {
          "$ref": "#/$defs/__schema0"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "required": [
    "ql",
    "expression"
  ],
  "additionalProperties": false,
  "$defs": {
    "__schema0": {
      "$ref": "#/$defs/Nql1Expression"
    },
    "Nql1Expression": {
      "anyOf": [
        {
          "$ref": "#/$defs/__schema1"
        },
        {
          "$ref": "#/$defs/Nql1ExpressionOr"
        },
        {
          "$ref": "#/$defs/Nql1ExpressionNot"
        },
        {
          "anyOf": [
            {
              "$ref": "#/$defs/Nql1Predicate"
            },
            {
              "$ref": "#/$defs/Nql1Relation"
            }
          ]
        }
      ]
    },
    "__schema1": {
      "$ref": "#/$defs/Nql1ExpressionAnd"
    },
    "Nql1ExpressionAnd": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "and"
        },
        "children": {
          "minItems": 2,
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/Nql1ExpressionOr"
              },
              {
                "$ref": "#/$defs/Nql1ExpressionNot"
              },
              {
                "anyOf": [
                  {
                    "$ref": "#/$defs/Nql1Predicate"
                  },
                  {
                    "$ref": "#/$defs/Nql1Relation"
                  }
                ]
              }
            ]
          }
        }
      },
      "required": [
        "kind",
        "children"
      ],
      "additionalProperties": false
    },
    "Nql1ExpressionOr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "or"
        },
        "children": {
          "minItems": 2,
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/__schema1"
              },
              {
                "$ref": "#/$defs/Nql1ExpressionNot"
              },
              {
                "anyOf": [
                  {
                    "$ref": "#/$defs/Nql1Predicate"
                  },
                  {
                    "$ref": "#/$defs/Nql1Relation"
                  }
                ]
              }
            ]
          }
        }
      },
      "required": [
        "kind",
        "children"
      ],
      "additionalProperties": false
    },
    "Nql1ExpressionNot": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "not"
        },
        "expression": {
          "$ref": "#/$defs/__schema0"
        }
      },
      "required": [
        "kind",
        "expression"
      ],
      "additionalProperties": false
    },
    "Nql1Predicate": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "has"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            }
          },
          "required": [
            "kind",
            "field"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "compare"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            },
            "op": {
              "type": "string",
              "enum": [
                "eq",
                "ne",
                "gt",
                "gte",
                "lt",
                "lte"
              ]
            },
            "value": {
              "$ref": "#/$defs/Nql1Scalar"
            }
          },
          "required": [
            "kind",
            "field",
            "op",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "range"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            },
            "lower": {
              "anyOf": [
                {
                  "type": "object",
                  "properties": {
                    "value": {
                      "$ref": "#/$defs/Nql1Scalar"
                    },
                    "inclusive": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "value",
                    "inclusive"
                  ],
                  "additionalProperties": false
                },
                {
                  "type": "null"
                }
              ]
            },
            "upper": {
              "anyOf": [
                {
                  "type": "object",
                  "properties": {
                    "value": {
                      "$ref": "#/$defs/Nql1Scalar"
                    },
                    "inclusive": {
                      "type": "boolean"
                    }
                  },
                  "required": [
                    "value",
                    "inclusive"
                  ],
                  "additionalProperties": false
                },
                {
                  "type": "null"
                }
              ]
            }
          },
          "required": [
            "kind",
            "field",
            "lower",
            "upper"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "prefix"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            },
            "value": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_-]{2,}$"
            }
          },
          "required": [
            "kind",
            "field",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "set"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            },
            "quantifier": {
              "type": "string",
              "enum": [
                "any",
                "all"
              ]
            },
            "values": {
              "minItems": 1,
              "type": "array",
              "items": {
                "$ref": "#/$defs/Nql1Scalar"
              }
            }
          },
          "required": [
            "kind",
            "field",
            "quantifier",
            "values"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "contains"
            },
            "field": {
              "$ref": "#/$defs/Nql1FieldPath"
            },
            "value": {
              "type": "string"
            }
          },
          "required": [
            "kind",
            "field",
            "value"
          ],
          "additionalProperties": false
        }
      ]
    },
    "Nql1FieldPath": {
      "type": "array",
      "prefixItems": [
        {
          "type": "string",
          "pattern": "^(?!(?:AND|OR|NOT|TO|ANY|ALL|has)$)[A-Za-z_][A-Za-z0-9_]*$"
        }
      ],
      "items": {
        "type": "string",
        "minLength": 1
      }
    },
    "Nql1Scalar": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "text"
            },
            "value": {
              "type": "string"
            }
          },
          "required": [
            "kind",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "number"
            },
            "value": {
              "type": "string",
              "pattern": "^(?:0|-?[1-9][0-9]*|-?(?:0|[1-9][0-9]*)\\.[0-9]*[1-9])$"
            }
          },
          "required": [
            "kind",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "boolean"
            },
            "value": {
              "type": "boolean"
            }
          },
          "required": [
            "kind",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "date"
            },
            "value": {
              "type": "string",
              "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}$"
            }
          },
          "required": [
            "kind",
            "value"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "relativeDate"
            },
            "offset": {
              "anyOf": [
                {
                  "type": "object",
                  "properties": {
                    "amount": {
                      "type": "integer",
                      "minimum": -9007199254740991,
                      "maximum": 9007199254740991
                    },
                    "unit": {
                      "type": "string",
                      "enum": [
                        "m",
                        "h",
                        "d",
                        "w"
                      ]
                    }
                  },
                  "required": [
                    "amount",
                    "unit"
                  ],
                  "additionalProperties": false
                },
                {
                  "type": "null"
                }
              ]
            },
            "roundToDay": {
              "type": "boolean"
            }
          },
          "required": [
            "kind",
            "offset",
            "roundToDay"
          ],
          "additionalProperties": false
        },
        {
          "type": "object",
          "properties": {
            "kind": {
              "type": "string",
              "const": "duration"
            },
            "amount": {
              "type": "integer",
              "minimum": 0,
              "maximum": 9007199254740991
            },
            "unit": {
              "type": "string",
              "enum": [
                "m",
                "h",
                "d",
                "w"
              ]
            }
          },
          "required": [
            "kind",
            "amount",
            "unit"
          ],
          "additionalProperties": false
        }
      ]
    },
    "Nql1Relation": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "relation"
        },
        "relation": {
          "type": "string",
          "enum": [
            "label",
            "source",
            "task"
          ]
        },
        "expression": {
          "$ref": "#/$defs/__schema2"
        }
      },
      "required": [
        "kind",
        "relation",
        "expression"
      ],
      "additionalProperties": false
    },
    "__schema2": {
      "$ref": "#/$defs/Nql1RelationExpression"
    },
    "Nql1RelationExpression": {
      "anyOf": [
        {
          "$ref": "#/$defs/__schema3"
        },
        {
          "$ref": "#/$defs/Nql1RelationExpressionOr"
        },
        {
          "$ref": "#/$defs/Nql1RelationExpressionNot"
        },
        {
          "$ref": "#/$defs/Nql1Predicate"
        }
      ]
    },
    "__schema3": {
      "$ref": "#/$defs/Nql1RelationExpressionAnd"
    },
    "Nql1RelationExpressionAnd": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "and"
        },
        "children": {
          "minItems": 2,
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/Nql1RelationExpressionOr"
              },
              {
                "$ref": "#/$defs/Nql1RelationExpressionNot"
              },
              {
                "$ref": "#/$defs/Nql1Predicate"
              }
            ]
          }
        }
      },
      "required": [
        "kind",
        "children"
      ],
      "additionalProperties": false
    },
    "Nql1RelationExpressionOr": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "or"
        },
        "children": {
          "minItems": 2,
          "type": "array",
          "items": {
            "anyOf": [
              {
                "$ref": "#/$defs/__schema3"
              },
              {
                "$ref": "#/$defs/Nql1RelationExpressionNot"
              },
              {
                "$ref": "#/$defs/Nql1Predicate"
              }
            ]
          }
        }
      },
      "required": [
        "kind",
        "children"
      ],
      "additionalProperties": false
    },
    "Nql1RelationExpressionNot": {
      "type": "object",
      "properties": {
        "kind": {
          "type": "string",
          "const": "not"
        },
        "expression": {
          "$ref": "#/$defs/__schema2"
        }
      },
      "required": [
        "kind",
        "expression"
      ],
      "additionalProperties": false
    }
  }
}
