[
  {
    "$id": "/schemas/datastore/aggregator/pipeline",

    "type": "array",
    "items": {
      "oneOf": [
        {
          "$ref": "/schemas/datastore/aggregator/step/fetch"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/json_patch"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/map"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/unset"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/validate"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/each"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/if"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/filter"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/op"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/from"
        },
        {
          "$ref": "/schemas/datastore/aggregator/step/persist"
        }
      ]
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/fetch",

    "type": "object",
    "required": ["type", "model"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["fetch"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/destination"
      },
      "datastore": {
        "$ref": "/schemas/datastore/aggregator/components/model"
      },
      "model": {
        "$ref": "/schemas/datastore/aggregator/components/model"
      },
      "source": {
        "$ref": "/schemas/datastore/aggregator/components/source"
      },
      "timetravel": {
        "$ref": "/schemas/datastore/aggregator/components/timetravel"
      },
      "correlation_field": {
        "$ref": "/schemas/datastore/aggregator/components/correlation_field",

        "description": "Correlation field to use for timetravel"
      },
      "query": {
        "$ref": "/schemas/datastore/aggregator/components/query"
      },
      "headers": {
        "$ref": "/schemas/datastore/aggregator/components/headers"
      },
      "map": {
        "$ref": "/schemas/datastore/aggregator/components/map"
      },
      "page": {
        "type": "number",
        "minimum": 0,
        "description": "Page index of the fetch step"
      },
      "page_size": {
        "type": "number",
        "minimum": 0,
        "description": "Page size of the fetch step"
      },
      "as_entity": {
        "type": "boolean",
        "description": "Does a single entity must be fetched?"
      },
      "must_decrypt": {
        "type": "boolean",
        "description": "Does the entity content be decrypt before being persisted?"
      },
      "default": {
        "description": "Default value"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/json_patch",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["json_patch"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "patch": {
        "type": "array",
        "items": {
          "type": "object",
          "required": ["op", "path"],
          "properties": {
            "op": {
              "type": "string",
              "enum": ["add", "remove", "replace", "copy", "move"],
              "description": "JSON PATCH Operation"
            },
            "path": {
              "type": "string",
              "description": "JSON PATCH Operation path"
            }
          }
        }
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/map",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["map"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "map": {
        "$ref": "/schemas/datastore/aggregator/components/map"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/unset",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["unset"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/validate",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["validate"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to validate"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Validation result path"
      },
      "must_throw": {
        "type": "boolean",
        "description": "Does an invalidation leads to an exception?"
      },
      "schema": {
        "type": "object",
        "description": "JSON Schema to validate"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/each",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["each"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to iterate on"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Validation result path"
      },
      "pipeline": {
        "$ref": "/schemas/datastore/aggregator/pipeline"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/if",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["if"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to validate"
      },
      "schema": {
        "type": "object",
        "description": "JSON Schema to validate"
      },
      "pipeline": {
        "$ref": "/schemas/datastore/aggregator/pipeline",
        "description": "Sub pipeline to execute"
      },
      "repeat_while_true": {
        "type": "boolean",
        "description": "Repeat the step while the validate part is true (while loop)"
      },
      "max_iteration_count": {
        "type": "number",
        "minimum": 0,
        "description": "Maximum number of iterations in case of a while loop iteration (default: 100)"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/filter",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["filter"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to validate"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Validation result path"
      },
      "schema": {
        "type": "object",
        "description": "JSON Schema to validate"
      },
      "as_entity": {
        "type": "boolean",
        "description": "Does a single entity must be found?"
      },
      "map": {
        "$ref": "/schemas/datastore/aggregator/components/map"
      },
      "default": {
        "description": "Default value"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/from",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["from"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to search steps to execute"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/op",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["op"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "path": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Path in the current state to apply the operationn on"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Validation result path"
      },
      "func": {
        "type": "string",
        "description": "Name of the function to invoke"
      },
      "args_as_array": {
        "type": "boolean",
        "description": "Are the args be sent as array (true) to the function or as arguments (otherwise)?"
      },
      "args": {
        "type": "array",
        "description": "List of arguments taken from the value defined by `path`",
        "items": {
          "anyOf": [
            {
              "type": "number"
            },
            {
              "type": "string"
            },
            {
              "type": "boolean"
            },
            {
              "type": "object",
              "properties": {
                "path": {
                  "$ref": "/schemas/datastore/aggregator/components/path"
                },
                "func": {
                  "type": "string",
                  "description": "Lodash valid function"
                }
              }
            }
          ]
        }
      },
      "default": {
        "description": "Default value"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/step/persist",

    "type": "object",
    "required": ["type"],
    "properties": {
      "type": {
        "$ref": "/schemas/datastore/aggregator/components/type",
        "enum": ["persist"]
      },
      "name": {
        "$ref": "/schemas/datastore/aggregator/components/step/name"
      },
      "description": {
        "$ref": "/schemas/datastore/aggregator/components/step/description"
      },
      "destination": {
        "$ref": "/schemas/datastore/aggregator/components/destination"
      },

      "datastore": {
        "$ref": "/schemas/datastore/aggregator/components/model"
      },
      "model": {
        "$ref": "/schemas/datastore/aggregator/components/model"
      },
      "correlation_field": {
        "$ref": "/schemas/datastore/aggregator/components/correlation_field",

        "description": "Correlation field to use for update"
      },
      "imperative_version_next": {
        "$ref": "/schemas/datastore/aggregator/components/path"
      },

      "payload": {
        "$ref": "/schemas/datastore/aggregator/components/query"
      },
      "headers": {
        "$ref": "/schemas/datastore/aggregator/components/headers"
      },
      "map": {
        "$ref": "/schemas/datastore/aggregator/components/map"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/components/type",

    "type": "string",
    "description": "Step type"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/path",

    "oneOf": [
      {
        "type": "string",
        "description": "String path definition"
      },
      {
        "type": "array",
        "description": "Array based path definition",
        "items": {
          "type": "string"
        }
      }
    ]
  },
  {
    "$id": "/schemas/datastore/aggregator/components/datastore",

    "type": "string",
    "description": "Datastore client to use",
    "default": "(The first one available)"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/model",

    "type": "string",
    "description": "Model to fetch data from"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/source",

    "type": "string",
    "enum": ["entities", "events"],
    "description": "Source of data: entities or events",
    "default": "entities"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/timetravel",

    "type": "string",
    "description": "Path of the field to use for timetravel date"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/correlation_field",

    "type": "string",
    "description": "Correlation field"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/destination",

    "$ref": "/schemas/datastore/aggregator/components/path",
    "description": "Location of the persistence in the state as a lodash path format"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/query",

    "type": "object",
    "description": "Base query to use to perform the fetch"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/headers",

    "type": "object",
    "patternProperties": {
      ".*": { "type": "string" }
    },
    "description": "Headers to send along the fetch request"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/map",

    "type": "array",
    "items": {
      "$ref": "/schemas/datastore/aggregator/components/map/item"
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/components/map/item",
    "description": "Map step definition",

    "type": "object",
    "required": ["to"],
    "properties": {
      "from": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Source location of the data to map"
      },
      "to": {
        "$ref": "/schemas/datastore/aggregator/components/path",
        "description": "Destination location of the mapped data"
      },
      "default": {
        "description": "Default value"
      },
      "must_hash": {
        "type": "boolean",
        "description": "Does the mapped value be hashed before being persisted?"
      },
      "json_stringify": {
        "type": "boolean",
        "description": "Does the mapped value be JSON stringified before being hashed or persisted"
      },
      "relative_date_in_seconds": {
        "type": "number",
        "description": "Change the date defined in value or current date of a specific quantity defined in seconds"
      }
    }
  },
  {
    "$id": "/schemas/datastore/aggregator/components/step/name",

    "type": "string",
    "description": "Step name"
  },
  {
    "$id": "/schemas/datastore/aggregator/components/step/description",

    "type": "string",
    "description": "Step description"
  },
  {
    "$id": "/schemas/datastore/projection",
    "title": "Projection configuration",
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "Name of the projection"
      },
      "destination": {
        "type": "string",
        "description": "Datastore name in the configuration receiving the projection"
      },
      "model": {
        "type": "object",
        "description": "Model configuration of the projection"
      },
      "triggers": {
        "type": "array",
        "items": {
          "$ref": "/schemas/datastore/runner/trigger"
        }
      },
      "pipeline": {
        "$ref": "/schemas/datastore/aggregator/pipeline"
      }
    }
  },
  {
    "$id": "/schemas/datastore/runner/trigger",
    "title": "Runner trigger schema",
    "type": "object",
    "required": ["datastore", "model", "source"],
    "properties": {
      "datastore": {
        "type": "string",
        "descrption": "Name of the Datastore in the configuration to use"
      },
      "model": {
        "type": "string",
        "descrption": "Model name used for the trigger"
      },
      "source": {
        "type": "string",
        "descrption": "Source of the trigger",
        "enum": ["entities", "events"]
      },
      "raw": {
        "type": "boolean",
        "descrption": "Does the event be parsed before sending to the handler?"
      },
      "query": {
        "type": "object",
        "descrption": "Query of the trigger"
      }
    }
  }
]
