{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/mappings/rules.schema.json",
  "title": "Mapping: rules. Data, editable in Setup, versioned, each with its source.",
  "type": "object",
  "required": [
    "mapping",
    "version",
    "updated",
    "updated_by",
    "source",
    "items"
  ],
  "additionalProperties": false,
  "properties": {
    "mapping": {
      "const": "rules"
    },
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "updated": {
      "type": "string"
    },
    "updated_by": {
      "type": "string",
      "minLength": 1
    },
    "about": {
      "type": "string"
    },
    "source": {
      "type": "object",
      "required": [
        "cite"
      ],
      "additionalProperties": false,
      "properties": {
        "cite": {
          "type": "string",
          "minLength": 1
        },
        "read": {
          "type": "string"
        }
      }
    },
    "items": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "direction",
          "enabled",
          "priority",
          "when",
          "do",
          "source"
        ],
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "string",
            "pattern": "^[a-z0-9][a-z0-9-]*$"
          },
          "about": {
            "type": "string"
          },
          "direction": {
            "enum": [
              "jira_to_i3",
              "i3_to_jira",
              "jira_to_jira"
            ]
          },
          "enabled": {
            "type": "boolean"
          },
          "disabled_why": {
            "type": "string"
          },
          "priority": {
            "type": "integer"
          },
          "when": {
            "type": "object",
            "required": [
              "from",
              "kind"
            ],
            "additionalProperties": false,
            "properties": {
              "from": {
                "enum": [
                  "jira",
                  "i3",
                  "person"
                ]
              },
              "kind": {
                "type": "string"
              },
              "to_status": {
                "type": "string"
              },
              "issuetype": {
                "type": "array",
                "items": {
                  "type": "string"
                }
              },
              "by_person": {
                "const": true
              },
              "linked": {
                "enum": [
                  "work_order",
                  "issue"
                ]
              }
            }
          },
          "do": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "object",
              "required": [
                "action"
              ],
              "additionalProperties": false,
              "properties": {
                "action": {
                  "enum": [
                    "i3.create_work_order",
                    "i3.update_work_order",
                    "i3.upsert_asset",
                    "jira.create_issue",
                    "jira.transition",
                    "jira.comment",
                    "jira.update_asset",
                    "ai.request"
                  ]
                },
                "fields": {
                  "type": "object",
                  "additionalProperties": {
                    "description": "A value: $.path in the message, {const}, {template} with {name} from the message and earlier results, or {map, from}.",
                    "oneOf": [
                      {
                        "type": "string",
                        "pattern": "^\\$\\."
                      },
                      {
                        "type": "object",
                        "required": [
                          "const"
                        ],
                        "additionalProperties": false,
                        "properties": {
                          "const": {}
                        }
                      },
                      {
                        "type": "object",
                        "required": [
                          "template"
                        ],
                        "additionalProperties": false,
                        "properties": {
                          "template": {
                            "type": "string"
                          }
                        }
                      },
                      {
                        "type": "object",
                        "required": [
                          "map",
                          "from"
                        ],
                        "additionalProperties": false,
                        "properties": {
                          "map": {
                            "type": "string"
                          },
                          "from": {
                            "type": "string",
                            "pattern": "^\\$\\."
                          }
                        }
                      }
                    ]
                  }
                },
                "to_status": {
                  "type": "string"
                },
                "text": {
                  "type": "string"
                },
                "tasks": {
                  "type": "array",
                  "items": {
                    "enum": [
                      "summarise",
                      "suggest_asset",
                      "suggest_work_order"
                    ]
                  }
                }
              }
            }
          },
          "source": {
            "type": "object",
            "required": [
              "cite"
            ],
            "additionalProperties": false,
            "properties": {
              "cite": {
                "type": "string",
                "minLength": 1
              },
              "read": {
                "type": "string"
              }
            }
          }
        }
      }
    },
    "maps": {
      "type": "object",
      "additionalProperties": {
        "type": "object"
      }
    }
  }
}
