{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/cgkineo/jsonmongoquery/master/schema/query.schema.json",
  "title": "Query",
  "description": "An object with query operators",
  "type": "object",
  "properties": {
    "$where": {
      "type": "string"
    },
    "$and": {
      "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
    },
    "$nor": {
      "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
    },
    "$or": {
      "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
    }
  },
  "propertyNames": {
    "pattern": "(^[$](where|and|nor|or)+)|(^[^$].*)"
  },
  "additionalProperties": {
    "$ref": "#/$defs/queryOperatorFieldOrArrayOrAnyPrimitive"
  },
  "$defs": {
    "anyPrimitive": {
      "type": ["string", "number", "boolean", "null"]
    },
    "document": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/anyValue"
      }
    },
    "array": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/anyValue"
      }
    },
    "anyValue": {
      "type": ["object", "array", "string", "number", "boolean", "null"],
      "allOf": [
        {
          "if": {
            "type": "object"
          },
          "then": {
            "$ref": "#/$defs/document"
          }
        },
        {
          "if": {
            "type": "array"
          },
          "then": {
            "$ref": "#/$defs/array"
          }
        },
        {
          "if": {
            "type": ["string", "number", "boolean", "null"]
          },
          "then": {
            "$ref": "#/$defs/anyPrimitive"
          }
        }
      ]
    },
    "types": {
      "type": ["string", "number"],
      "enum": [1, "number", 2, "string", 3, "object", 4, "array", 6, "undefined", 8, "bool", 10, "null"]
    },
    "queryOperatorFieldOrArrayOrAnyPrimitive": {
      "allOf": [
        {
          "if": {
            "type": "object"
          },
          "then": {
            "$ref": "#/$defs/queryOperatorField"
          }
        },
        {
          "if": {
            "type": "array"
          },
          "then": {
            "$ref": "#/$defs/array"
          }
        },
        {
          "if": {
            "type": ["string", "number", "boolean", "null"]
          },
          "then": {
            "$ref": "#/$defs/anyPrimitive"
          }
        }
      ]
    },
    "queryOperatorFieldsOrArrayOrAnyPrimitive": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/queryOperatorFieldOrArrayOrAnyPrimitive"
      }
    },
    "queryOperatorFields": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/queryOperatorField"
      }
    },
    "queryOperatorField": {
      "type": "object",
      "properties": {
        "$eq": {
          "$ref": "#/$defs/anyValue"
        },
        "$gt": {
          "$ref": "#/$defs/anyValue"
        },
        "$gte": {
          "$ref": "#/$defs/anyValue"
        },
        "$in": {
          "$ref": "#/$defs/array"
        },
        "$lt": {
          "$ref": "#/$defs/anyValue"
        },
        "$lte": {
          "$ref": "#/$defs/anyValue"
        },
        "$ne": {
          "$ref": "#/$defs/anyValue"
        },
        "$nin": {
          "$ref": "#/$defs/array"
        },
        "$and": {
          "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
        },
        "$not": {
          "$ref": "#/$defs/queryOperatorField"
        },
        "$nor": {
          "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
        },
        "$or": {
          "$ref": "#/$defs/queryOperatorFieldsOrArrayOrAnyPrimitive"
        },
        "$exists": {
          "type": "boolean"
        },
        "$type": {
          "type": ["array", "string", "number"],
          "allOf": [
            {
              "if": {
                "type": "array"
              },
              "then": {
                "items": {
                  "$ref": "#/$defs/types"
                }
              }
            },
            {
              "if": {
                "type": ["string", "number"]
              },
              "then": {
                "$ref": "#/$defs/types"
              }
            }
          ]
        },
        "$mod": {
          "type": "array",
          "items": {
            "type": "number"
          },
          "minItems": 2,
          "maxItems": 2
        },
        "$regex": {
          "type": "string"
        },
        "$options": {
          "type": "string"
        },
        "$where": {
          "type": "string"
        },
        "$all": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/anyValue"
          }
        },
        "$elemMatch": {
          "$ref": "#/$defs/queryOperatorField"
        },
        "$size": {
          "type": "number"
        }
      },
      "propertyNames": {
        "pattern": "(^[$](eq|gt|gte|in|lt|lte|ne|nin|and|not|nor|or|exists|type|mod|regex|options|where|all|elemMatch|size)+)|(^[^$].*)"
      },
      "patternProperties": {
        "^[^$]": {
          "type": ["object", "array", "string", "number", "boolean", "null"],
          "allOf": [
            {
              "if": {
                "type": "object"
              },
              "then": {
                "$ref": "#/$defs/queryOperatorField"
              }
            },
            {
              "if": {
                "type": "array"
              },
              "then": {
                "$ref": "#/$defs/array"
              }
            },
            {
              "if": {
                "type": ["string", "number", "boolean", "null"]
              },
              "then": {
                "$ref": "#/$defs/anyPrimitive"
              }
            }
          ]
        }
      }
    }
  }
}
