{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://artifacts.apis.io/akeda-ru/json-schema/akeda-ru-automation-rule-test-request-schema.json",
  "title": "AutomationRuleTestRequest",
  "x-generated": "2026-11-10",
  "x-method": "derived",
  "x-generator": "derive-json-schema.py",
  "x-source": "openapi/akeda-ru-automation-api-openapi.yml#/components/schemas/AutomationRuleTestRequest",
  "type": "object",
  "required": [
    "rule"
  ],
  "properties": {
    "rule": {
      "type": "object",
      "description": "Документ правила в той же форме, что у записи правила; название и исполнитель не нужны.",
      "required": [
        "event_type"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "event_type": {
          "type": "string"
        },
        "condition": {
          "type": "string"
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "field",
              "op"
            ],
            "properties": {
              "field": {
                "type": "string"
              },
              "op": {
                "type": "string",
                "description": "equals, not_equals, contains, starts_with, ends_with, is_true, is_false; числа — gt, gte, lt, lte, between; даты — before, on_or_before, after, on_or_after, between; у событий CRM любое поле, кроме флага, — ещё is_not_empty, is_empty (без значения)"
              },
              "value": {
                "type": "string"
              },
              "value_to": {
                "type": "string",
                "description": "Верхняя граница «между», включительно"
              },
              "of": {
                "type": "string",
                "description": "Числовое поле-основа: value и value_to — проценты от него"
              },
              "group": {
                "type": "integer",
                "minimum": 0,
                "description": "Группа «или»: сравнения группы — «и», группы между собой — «или»"
              }
            }
          }
        },
        "actions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRuleBlock"
          }
        },
        "branches": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRuleBranch"
          }
        },
        "otherwise": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRuleBlock"
          }
        }
      }
    },
    "sample_key": {
      "type": "string",
      "description": "Ключ факта из выборки последних фактов события; пусто — последний факт"
    },
    "payload": {
      "type": "object",
      "additionalProperties": {
        "type": "string"
      },
      "description": "Тело события для проверки «что если» вместо настоящего факта"
    }
  },
  "$defs": {
    "AutomationRuleBlock": {
      "type": "object",
      "description": "Шаг пути правила (ERP-1932): действие каталога или пауза «Подождать»\n(`kind: wait`). `id` — устойчивый id шага: журнал, ключ повтора и\nссылки `{{step:<id>.<результат>}}` во входах следующих шагов того же\nпути. Пусто — сервер ставит a1, a2 … (пауза — w1 …).\n",
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,63}$"
        },
        "kind": {
          "type": "string",
          "enum": [
            "action",
            "wait"
          ],
          "description": "Пусто — действие"
        },
        "command": {
          "type": "string"
        },
        "inputs": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        },
        "wait": {
          "type": "object",
          "description": "Пауза: after_event — через amount единиц unit от начала паузы; relative_to_field — до даты поля field (± amount); cancel_on — события, факт которых по той же записи прекращает правило",
          "additionalProperties": true,
          "properties": {
            "kind": {
              "type": "string",
              "enum": [
                "after_event",
                "relative_to_field"
              ]
            },
            "amount": {
              "type": "integer",
              "minimum": 0
            },
            "unit": {
              "type": "string",
              "enum": [
                "minutes",
                "hours",
                "days",
                "working_days"
              ]
            },
            "direction": {
              "type": "string",
              "enum": [
                "before",
                "after"
              ]
            },
            "field": {
              "type": "string"
            },
            "at": {
              "type": "string"
            },
            "cancel_on": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "AutomationRuleBranch": {
      "type": "object",
      "description": "«Если ещё …»: своё условие сравнениями и свои шаги",
      "required": [
        "conditions",
        "actions"
      ],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z][a-z0-9_]{0,63}$"
        },
        "condition": {
          "type": "string",
          "description": "Выражение, собранное сервером из conditions"
        },
        "conditions": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "field",
              "op"
            ],
            "properties": {
              "field": {
                "type": "string"
              },
              "op": {
                "type": "string"
              },
              "value": {
                "type": "string"
              },
              "value_to": {
                "type": "string"
              },
              "of": {
                "type": "string"
              },
              "group": {
                "type": "integer",
                "minimum": 0
              }
            }
          }
        },
        "actions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/AutomationRuleBlock"
          }
        }
      }
    }
  }
}