{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:ujn:model:parts-1-4",
  "title": "UJN model",
  "description": "The model the UJN PlantUML module writes from a journey's text: language spec proposal/ujn-language-v18.md, parts 1 to 5 (nodes: view, actor, system, action, handoff, data, start, end, ref; arrows). Version 5: every arrow says whether it is a failure (negative), so that a failed return (.x.>) is a return that failed.",
  "type": "object",
  "required": [
    "language",
    "journey",
    "nodes",
    "arrows",
    "warnings"
  ],
  "additionalProperties": false,
  "properties": {
    "language": {
      "const": "ujn-parts-1-4",
      "description": "Which parts of the language spec the model follows."
    },
    "journey": {
      "$ref": "#/$defs/journey"
    },
    "nodes": {
      "type": "array",
      "description": "Every node, in the order the statements are written.",
      "items": {
        "oneOf": [
          {
            "$ref": "#/$defs/view"
          },
          {
            "$ref": "#/$defs/actor"
          },
          {
            "$ref": "#/$defs/system"
          },
          {
            "$ref": "#/$defs/action"
          },
          {
            "$ref": "#/$defs/handoff"
          },
          {
            "$ref": "#/$defs/data"
          },
          {
            "$ref": "#/$defs/start"
          },
          {
            "$ref": "#/$defs/end"
          },
          {
            "$ref": "#/$defs/ref"
          }
        ]
      }
    },
    "arrows": {
      "type": "array",
      "description": "Every arrow, in the order written. A return the renderer draws from `returns` is not listed separately.",
      "items": {
        "$ref": "#/$defs/arrow"
      }
    },
    "warnings": {
      "type": "array",
      "description": "The warnings listed under the drawing (spec section 14).",
      "items": {
        "$ref": "#/$defs/warning"
      }
    }
  },
  "$defs": {
    "text": {
      "type": [
        "string",
        "null"
      ]
    },
    "handle": {
      "type": "string",
      "pattern": "^[A-Za-z][A-Za-z0-9_]*$"
    },
    "uniqueId": {
      "type": [
        "string",
        "null"
      ],
      "pattern": "^[0-9a-f]{11}$"
    },
    "flag": {
      "enum": [
        "idea",
        "future",
        "not-implemented",
        "out-of-scope",
        "deprecated",
        "experiment",
        "risk",
        "security",
        "needs-decision",
        "needs-updates",
        "legacy"
      ]
    },
    "flags": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/flag"
      },
      "uniqueItems": true
    },
    "traceLinks": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^[a-z][a-z0-9-]*:\\S"
      }
    },
    "notes": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "color": {
      "type": "string",
      "description": "#rgb, #rrggbb, #rrggbbaa, or a PlantUML color name."
    },
    "extra": {
      "type": "object",
      "description": "Keys the language does not define for this statement, kept as written (spec 4.4).",
      "additionalProperties": {
        "type": [
          "string",
          "boolean"
        ]
      }
    },
    "journey": {
      "type": "object",
      "required": [
        "id",
        "title",
        "description",
        "context",
        "trace",
        "a11y",
        "uniqueId",
        "uniqueIdPending",
        "friendlyNamespace"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "description": {
          "$ref": "#/$defs/text"
        },
        "context": {
          "$ref": "#/$defs/text"
        },
        "trace": {
          "$ref": "#/$defs/traceLinks"
        },
        "a11y": {
          "$ref": "#/$defs/notes"
        },
        "uniqueId": {
          "$ref": "#/$defs/uniqueId"
        },
        "uniqueIdPending": {
          "type": "boolean"
        },
        "friendlyNamespace": {
          "type": "string"
        }
      }
    },
    "nodeCommon": {
      "type": "object",
      "required": [
        "key",
        "kind",
        "type",
        "title",
        "id",
        "idAssigned",
        "uniqueId",
        "uniqueIdPending",
        "friendlyId",
        "flags",
        "inherited",
        "description",
        "trace",
        "a11y",
        "context",
        "style",
        "tooltip"
      ],
      "properties": {
        "key": {
          "$ref": "#/$defs/handle"
        },
        "title": {
          "type": "string",
          "description": "As written, markup included; the handle when no title is written."
        },
        "id": {
          "type": [
            "string",
            "null"
          ],
          "description": "The display id, written or assigned; for viewing only, it may change."
        },
        "idAssigned": {
          "type": "boolean"
        },
        "uniqueId": {
          "$ref": "#/$defs/uniqueId"
        },
        "uniqueIdPending": {
          "type": "boolean",
          "description": "True while the text still says {{auto}}."
        },
        "friendlyId": {
          "type": "string",
          "description": "namespace.name"
        },
        "flags": {
          "$ref": "#/$defs/flags",
          "description": "Set on this node."
        },
        "inherited": {
          "$ref": "#/$defs/flags",
          "description": "Delivery flags reaching this node only through flagged nodes or arrows."
        },
        "description": {
          "$ref": "#/$defs/text"
        },
        "trace": {
          "$ref": "#/$defs/traceLinks"
        },
        "a11y": {
          "$ref": "#/$defs/notes"
        },
        "context": {
          "$ref": "#/$defs/text"
        },
        "style": {
          "type": "object",
          "description": "Colors the text sets after the cascade (node, type block, kind block); colors left to the theme are absent.",
          "additionalProperties": false,
          "properties": {
            "frame": {
              "$ref": "#/$defs/color"
            },
            "icon": {
              "$ref": "#/$defs/color"
            },
            "title": {
              "$ref": "#/$defs/color"
            },
            "description": {
              "$ref": "#/$defs/color"
            },
            "background": {
              "$ref": "#/$defs/color"
            }
          }
        },
        "extra": {
          "$ref": "#/$defs/extra"
        },
        "tooltip": {
          "type": [
            "string",
            "null"
          ],
          "description": "Hover text the designer adds with the tooltip key (spec 9.3); null without one."
        }
      }
    },
    "view": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "route",
        "collapsed",
        "parent",
        "behavior",
        "overrides"
      ],
      "properties": {
        "kind": {
          "const": "view"
        },
        "type": {
          "enum": [
            "plain",
            "modal",
            "drawer",
            "popover",
            "toast",
            "tabs",
            "wizard",
            "embedded",
            "new-window",
            "waiting"
          ]
        },
        "route": {
          "$ref": "#/$defs/text"
        },
        "collapsed": {
          "type": "boolean"
        },
        "parent": {
          "type": [
            "string",
            "null"
          ]
        },
        "behavior": {
          "type": "object",
          "description": "The type's defaults merged with the view's overrides (spec 11.4). Keys that do not apply to the type are absent.",
          "additionalProperties": false,
          "properties": {
            "blocks": {
              "type": "boolean"
            },
            "dismiss": {
              "type": "array",
              "items": {
                "enum": [
                  "escape",
                  "outside",
                  "close",
                  "timeout",
                  "none"
                ]
              }
            },
            "focus": {
              "enum": [
                "trap",
                "move",
                "stay"
              ]
            },
            "duration": {
              "type": "string",
              "pattern": "^\\d+(\\.\\d+)?\\s?(ms|s|min|h|d)$"
            },
            "target": {
              "enum": [
                "tab",
                "window"
              ]
            }
          }
        },
        "overrides": {
          "type": "array",
          "items": {
            "enum": [
              "blocks",
              "dismiss",
              "focus",
              "duration",
              "target"
            ]
          }
        }
      }
    },
    "actor": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "role"
      ],
      "properties": {
        "kind": {
          "const": "actor"
        },
        "type": {
          "const": "actor"
        },
        "role": {
          "$ref": "#/$defs/text"
        }
      }
    },
    "system": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "host"
      ],
      "properties": {
        "kind": {
          "const": "system"
        },
        "type": {
          "enum": [
            "internal",
            "external"
          ]
        },
        "host": {
          "$ref": "#/$defs/text"
        }
      }
    },
    "arrow": {
      "type": "object",
      "required": [
        "from",
        "to",
        "kind",
        "negative",
        "role",
        "trigger",
        "guard",
        "payload",
        "returns",
        "returnLabel",
        "direction",
        "flags",
        "inherited",
        "trace",
        "context",
        "a11y",
        "style",
        "tooltip"
      ],
      "additionalProperties": false,
      "properties": {
        "from": {
          "$ref": "#/$defs/handle"
        },
        "to": {
          "$ref": "#/$defs/handle"
        },
        "kind": {
          "enum": [
            "navigation",
            "negative",
            "return"
          ]
        },
        "negative": {
          "type": "boolean",
          "description": "A failure: true for -x-> (kind negative) and for .x.> (kind return, a failed return); false otherwise."
        },
        "role": {
          "enum": [
            "exit",
            "call",
            "reply",
            "resume",
            "write",
            "read"
          ],
          "description": "exit by default; call: an action into a system; reply: a system back to the action that calls it; resume: out of a handoff or a journey reference; write: an action into a data store; read: a data store into an action."
        },
        "trigger": {
          "$ref": "#/$defs/text"
        },
        "guard": {
          "$ref": "#/$defs/text"
        },
        "payload": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "returns": {
          "type": "boolean"
        },
        "returnLabel": {
          "$ref": "#/$defs/text"
        },
        "direction": {
          "enum": [
            "right",
            "left",
            "up",
            "down",
            null
          ]
        },
        "flags": {
          "$ref": "#/$defs/flags"
        },
        "inherited": {
          "$ref": "#/$defs/flags"
        },
        "trace": {
          "$ref": "#/$defs/traceLinks"
        },
        "context": {
          "$ref": "#/$defs/text"
        },
        "a11y": {
          "$ref": "#/$defs/notes"
        },
        "style": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "line": {
              "$ref": "#/$defs/color"
            },
            "text": {
              "$ref": "#/$defs/color"
            },
            "background": {
              "$ref": "#/$defs/color"
            }
          }
        },
        "extra": {
          "$ref": "#/$defs/extra"
        },
        "tooltip": {
          "type": [
            "string",
            "null"
          ],
          "description": "Hover text the designer adds with the tooltip key (spec 9.3); null without one."
        }
      }
    },
    "warning": {
      "type": "object",
      "required": [
        "message",
        "line"
      ],
      "additionalProperties": false,
      "properties": {
        "message": {
          "type": "string"
        },
        "handle": {
          "type": [
            "string",
            "null"
          ]
        },
        "line": {
          "type": [
            "integer",
            "null"
          ],
          "description": "1-based line in the diagram source, when known."
        }
      }
    },
    "duration": {
      "type": [
        "string",
        "null"
      ],
      "pattern": "^\\d+(\\.\\d+)?\\s?(ms|s|min|h|d)$"
    },
    "action": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "timeout",
        "channel"
      ],
      "properties": {
        "kind": {
          "const": "action"
        },
        "type": {
          "enum": [
            "action",
            "message",
            "wait",
            "event",
            "auth",
            "permission",
            "fork",
            "join"
          ]
        },
        "timeout": {
          "$ref": "#/$defs/duration",
          "description": "On action, wait, event and auth; the exit guarded \"timeout\" is taken when it runs out."
        },
        "channel": {
          "$ref": "#/$defs/text",
          "description": "On a message: SMS, email, push."
        }
      }
    },
    "handoff": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "channel",
        "timeout",
        "resume"
      ],
      "properties": {
        "kind": {
          "const": "handoff"
        },
        "type": {
          "enum": [
            "returns",
            "no-return"
          ]
        },
        "channel": {
          "$ref": "#/$defs/text",
          "description": "Where the user goes: SMS, email, identity provider, payment page, bank app, phone."
        },
        "timeout": {
          "$ref": "#/$defs/duration",
          "description": "How long the link or session out there stays valid."
        },
        "resume": {
          "$ref": "#/$defs/text",
          "description": "The deep link the user comes back through."
        }
      }
    },
    "data": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "location"
      ],
      "properties": {
        "kind": {
          "const": "data"
        },
        "type": {
          "enum": [
            "file",
            "folder",
            "browser-cache",
            "browser-storage",
            "server-database",
            "api"
          ]
        },
        "location": {
          "$ref": "#/$defs/text",
          "description": "A path, a key, a table or an endpoint."
        }
      }
    },
    "start": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "route"
      ],
      "properties": {
        "kind": {
          "const": "start"
        },
        "type": {
          "enum": [
            "plain",
            "deep-link",
            "notification",
            "handoff-return"
          ]
        },
        "route": {
          "$ref": "#/$defs/text",
          "description": "The URL or deep link that opens here."
        }
      }
    },
    "end": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [],
      "properties": {
        "kind": {
          "const": "end"
        },
        "type": {
          "enum": [
            "success",
            "failure",
            "abandoned"
          ]
        }
      }
    },
    "ref": {
      "allOf": [
        {
          "$ref": "#/$defs/nodeCommon"
        }
      ],
      "required": [
        "journey"
      ],
      "properties": {
        "kind": {
          "const": "ref"
        },
        "type": {
          "const": "ref"
        },
        "journey": {
          "type": [
            "string",
            "null"
          ],
          "description": "The id of the journey this node stands for."
        }
      }
    }
  }
}
