{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://apigenie.pl/schemas/apigenie-mock-grpc-stubs.json",
  "title": "apigenie-mock gRPC stubs",
  "description": "Stubs answering the gRPC calls of a Protobuf file: a list, {stubs: [...]}, or one stub.",
  "oneOf": [
    {
      "type": "array",
      "items": {
        "$ref": "#/$defs/stub"
      }
    },
    {
      "type": "object",
      "properties": {
        "$schema": {
          "type": "string"
        },
        "stubs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/stub"
          }
        }
      },
      "required": [
        "stubs"
      ],
      "additionalProperties": false
    },
    {
      "$ref": "#/$defs/stub"
    }
  ],
  "$defs": {
    "stub": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "description": "Names the stub in the admin API and in x-apigenie-mock-stub"
        },
        "method": {
          "type": "string",
          "description": "The method answered: pkg.Service/Method, or Method with service"
        },
        "service": {
          "type": "string"
        },
        "priority": {
          "type": "integer",
          "description": "Higher priorities are tried first"
        },
        "times": {
          "type": "integer",
          "minimum": 0,
          "description": "How many calls the stub answers"
        },
        "request": {
          "$ref": "#/$defs/requestMatcher"
        },
        "requests": {
          "oneOf": [
            {
              "type": "array",
              "items": {
                "$ref": "#/$defs/matcher"
              },
              "description": "The messages of a client or bidi stream, in order"
            },
            {
              "type": "object",
              "properties": {
                "every": {
                  "$ref": "#/$defs/matcher"
                }
              },
              "required": [
                "every"
              ],
              "additionalProperties": false
            }
          ]
        },
        "metadata": {
          "$ref": "#/$defs/metadataMatcher"
        },
        "response": {
          "$ref": "#/$defs/response"
        }
      },
      "required": [
        "method"
      ],
      "additionalProperties": false
    },
    "matcher": {
      "type": "object",
      "properties": {
        "equals": {
          "type": "object",
          "description": "The whole message, compared in its canonical form"
        },
        "contains": {
          "type": "object",
          "description": "Fields with these values"
        },
        "matches": {
          "type": "object",
          "description": "Fields whose text contains these regular expressions"
        },
        "ignore_array_order": {
          "type": "boolean"
        },
        "ignoreArrayOrder": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    },
    "requestMatcher": {
      "allOf": [
        {
          "$ref": "#/$defs/matcher"
        }
      ],
      "properties": {
        "equals": true,
        "contains": true,
        "matches": true,
        "ignore_array_order": true,
        "ignoreArrayOrder": true,
        "metadata": {
          "$ref": "#/$defs/metadataMatcher"
        }
      },
      "unevaluatedProperties": false
    },
    "stringMap": {
      "type": "object",
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "boolean"
        ]
      }
    },
    "metadataMatcher": {
      "type": "object",
      "properties": {
        "equals": {
          "$ref": "#/$defs/stringMap"
        },
        "contains": {
          "$ref": "#/$defs/stringMap"
        },
        "matches": {
          "$ref": "#/$defs/stringMap"
        }
      },
      "additionalProperties": false
    },
    "message": {
      "oneOf": [
        {
          "type": "object",
          "properties": {
            "message": {
              "type": "object"
            },
            "delay": {
              "type": "integer",
              "minimum": 0
            }
          },
          "required": [
            "message"
          ],
          "additionalProperties": false
        },
        {
          "type": "object"
        }
      ]
    },
    "status": {
      "oneOf": [
        {
          "type": [
            "string",
            "integer"
          ]
        },
        {
          "type": "object",
          "properties": {
            "code": {
              "type": [
                "string",
                "integer"
              ],
              "description": "OK, NOT_FOUND, ... or 0 to 16"
            },
            "message": {
              "type": "string"
            },
            "details": {
              "type": "array",
              "items": {
                "type": "object",
                "required": [
                  "@type"
                ]
              }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "response": {
      "type": "object",
      "properties": {
        "headers": {
          "$ref": "#/$defs/stringMap"
        },
        "message": {
          "type": "object"
        },
        "messages": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/message"
          }
        },
        "each": {
          "$ref": "#/$defs/message"
        },
        "trailers": {
          "$ref": "#/$defs/stringMap"
        },
        "status": {
          "$ref": "#/$defs/status"
        },
        "delay": {
          "type": "integer",
          "minimum": 0
        },
        "drop": {
          "enum": [
            "reset",
            "close",
            "timeout",
            "truncate"
          ]
        },
        "generate": {
          "type": "boolean"
        }
      },
      "additionalProperties": false
    }
  }
}
