{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://bunt-app.com/teaching-plan-file/teachingplan-1.schema.json",
  "title": "Bunt teaching plan file",
  "description": "A file of base plans for the Bunt teacher app (extension .buntplan, JSON). A base plan orders the subject matter of a school year into folders and units; Bunt makes a copy of it for every course that works with it. The file holds what a plan IS — titles, texts, the tree, links, homework — and nothing that belongs to one teacher or one course: no ids, no status, no lessons.",
  "type": "object",
  "required": [
    "format",
    "schemaVersion",
    "plans"
  ],
  "additionalProperties": true,
  "properties": {
    "format": {
      "const": "bunt.teachingplans",
      "description": "Says what the file holds. Always this value."
    },
    "schemaVersion": {
      "const": 1,
      "description": "The version of this schema. An app refuses a file of a newer version than it knows."
    },
    "plans": {
      "type": "array",
      "description": "One or more base plans. Recommended: one plan per file.",
      "minItems": 1,
      "maxItems": 50,
      "items": {
        "$ref": "#/$defs/plan"
      }
    }
  },
  "$defs": {
    "shortText": {
      "type": "string",
      "maxLength": 500,
      "description": "A single line. Line breaks and control characters are replaced by spaces."
    },
    "richText": {
      "$ref": "#/$defs/richTextDocument",
      "description": "Formatted text, as an object (never a string): see richTextDocument."
    },
    "plan": {
      "type": "object",
      "required": [
        "title"
      ],
      "additionalProperties": true,
      "properties": {
        "title": {
          "$ref": "#/$defs/shortText",
          "minLength": 1,
          "description": "The name of the plan, e.g. \"Mathematik 7\"."
        },
        "descriptionText": {
          "type": "string",
          "maxLength": 10000,
          "description": "What the plan is for, in plain text. Line breaks are kept. Longer text is cut to 10 000 characters, not refused."
        },
        "items": {
          "type": "array",
          "description": "The plan's folders and units, top level first, in the order they are taught. At most 2000 items per plan and 12 levels deep; recommended: at most three levels.",
          "items": {
            "$ref": "#/$defs/item"
          }
        }
      }
    },
    "item": {
      "oneOf": [
        {
          "$ref": "#/$defs/group"
        },
        {
          "$ref": "#/$defs/unit"
        }
      ]
    },
    "group": {
      "type": "object",
      "description": "A folder: a title, a colour and the items in it. Anything else it carries is dropped.",
      "required": [
        "kind",
        "title"
      ],
      "additionalProperties": true,
      "properties": {
        "kind": {
          "const": "group"
        },
        "title": {
          "$ref": "#/$defs/shortText",
          "minLength": 1,
          "description": "The name of the folder, short and telling: a few words, about 40 characters at most, e.g. \"Lineare Gleichungen\". It is read in the outline beside the plan, where a long title is cut off."
        },
        "color": {
          "type": "string",
          "pattern": "^#[0-9A-Fa-f]{6}$",
          "description": "The colour of the folder, #RRGGBB; a folder is never transparent. Absent: no colour. Bunt drops a colour it cannot read, and the alpha of a #RRGGBBAA colour."
        },
        "children": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/item"
          }
        }
      }
    },
    "unit": {
      "type": "object",
      "description": "A teaching unit: what is taught in one or more lessons.",
      "required": [
        "kind"
      ],
      "additionalProperties": true,
      "properties": {
        "kind": {
          "const": "unit"
        },
        "title": {
          "$ref": "#/$defs/shortText",
          "description": "The name of the unit, short and telling: a few words, about 40 characters at most, e.g. \"Terme aufstellen\". It is read in the outline beside the plan, where a long title is cut off, and it can become the name of a grading. What explains the topic goes into text. May be empty: the app then shows a placeholder."
        },
        "text": {
          "$ref": "#/$defs/richText",
          "description": "The content of the unit: goals, steps, materials."
        },
        "plannedLessonCount": {
          "type": "integer",
          "minimum": 0,
          "maximum": 32767,
          "description": "How many lessons the unit is meant to take, a guide for every course; it never decides whether a unit is done. 0 or absent: not planned yet."
        },
        "curriculumReference": {
          "$ref": "#/$defs/shortText",
          "description": "Where the curriculum asks for this, e.g. \"KLP 3.2\"."
        },
        "links": {
          "type": "array",
          "maxItems": 100,
          "description": "Web addresses attached to the unit. Only http and https.",
          "items": {
            "$ref": "#/$defs/link"
          }
        },
        "homework": {
          "type": "array",
          "maxItems": 100,
          "description": "The homework of the unit, in order. Each entry is rich text like the unit's text.",
          "items": {
            "$ref": "#/$defs/richText"
          }
        },
        "files": {
          "type": "array",
          "maxItems": 100,
          "description": "Files attached to the unit. Only in the archive form of a .buntplan (a ZIP archive with this JSON as plan.json): each file lies in the archive at its path. A plain JSON .buntplan with files is refused.",
          "items": {
            "$ref": "#/$defs/attachedFile"
          }
        }
      },
      "not": {
        "required": [
          "children"
        ]
      }
    },
    "attachedFile": {
      "type": "object",
      "required": [
        "title",
        "path"
      ],
      "additionalProperties": true,
      "properties": {
        "title": {
          "$ref": "#/$defs/shortText",
          "description": "The name of the attachment as the unit shows it."
        },
        "fileName": {
          "type": "string",
          "description": "The name the file is saved under, e.g. \"Arbeitsblatt Brüche.pdf\"; any language. Absent: the last part of path. Bunt keeps the last part after / or \\, drops control characters, direction marks and : < > \" | ? *, dots and spaces at the start and the end, puts _ in front of a name Windows reserves (CON, NUL, COM1 …) and cuts a name beyond 200 UTF-8 bytes, keeping a short extension."
        },
        "path": {
          "type": "string",
          "pattern": "^assets/[A-Za-z0-9][A-Za-z0-9._-]{0,99}$",
          "description": "Where the file lies in the archive, from the folder of plan.json on: assets/ and a name of ASCII letters, digits, '.', '_' and '-', e.g. \"assets/arbeitsblatt-1.pdf\". Several units may name the same file."
        }
      }
    },
    "link": {
      "type": "object",
      "required": [
        "title",
        "url"
      ],
      "additionalProperties": true,
      "properties": {
        "title": {
          "$ref": "#/$defs/shortText",
          "description": "May be empty: the app then shows the address."
        },
        "url": {
          "type": "string",
          "maxLength": 2048,
          "pattern": "^https?://[^/?#@\\s]+([/?#][^\\s]*)?$",
          "description": "An http or https address with a host and without a user name."
        }
      }
    },
    "richTextDocument": {
      "type": "object",
      "description": "A formatted text: a list of blocks, each one paragraph, heading, list item or table. Everything the Bunt editor can show can be written: headings, lists, checklists, indentation, alignment, bold, italic, underline, strikethrough, text and highlight colours, links and tables. Bunt reads a text less strictly than this schema: a value it cannot read at all (a string instead of an object, a value of the wrong type, a malformed colour, an alignment that does not exist) makes it refuse the whole file; what it can read leniently it takes (unknown keys are dropped, a missing list is empty, an indent beyond 8 becomes 8). Validate against this schema anyway: only a valid text is sure to look as written. At most 256 KB (UTF-8) as Bunt stores it: with an id of about 50 bytes for every block and every left-out default written out; recommended: up to about 1000 characters per unit.",
      "required": [
        "schemaVersion",
        "blocks"
      ],
      "additionalProperties": false,
      "properties": {
        "schemaVersion": {
          "const": 1,
          "description": "The version of the document format. Always 1."
        },
        "blocks": {
          "type": "array",
          "description": "The paragraphs, headings, list items and tables of the text, in order.",
          "items": {
            "$ref": "#/$defs/richTextBlock"
          }
        }
      }
    },
    "richTextBlock": {
      "type": "object",
      "description": "One paragraph, heading, list item or table. Bunt gives every block an id of its own; a file carries none.",
      "additionalProperties": false,
      "properties": {
        "style": {
          "$ref": "#/$defs/richTextBlockStyle"
        },
        "runs": {
          "type": "array",
          "description": "The text of the block, in pieces of one formatting each. Absent for a table, which holds its text in its cells.",
          "items": {
            "$ref": "#/$defs/richTextRun"
          }
        },
        "indent": {
          "type": "integer",
          "minimum": 0,
          "maximum": 8,
          "description": "Nesting level of a list item, or indentation of a paragraph. 0 or absent: none."
        },
        "alignment": {
          "enum": [
            "natural",
            "center",
            "trailing"
          ],
          "description": "Absent: natural, which follows the reading direction of the teacher's language."
        },
        "isChecked": {
          "type": "boolean",
          "description": "Only for a checklist item: whether it is done. Bunt imports every checklist unchecked and never exports a check: a check belongs to the plan it was set in."
        }
      }
    },
    "richTextBlockStyle": {
      "description": "What the block is. Absent: a paragraph.",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "type"
          ],
          "properties": {
            "type": {
              "const": "paragraph"
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "type",
            "level"
          ],
          "properties": {
            "type": {
              "const": "heading"
            },
            "level": {
              "enum": [
                1,
                2,
                3
              ]
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "type",
            "list"
          ],
          "properties": {
            "type": {
              "const": "list"
            },
            "list": {
              "enum": [
                "bullet",
                "ordered",
                "checklist"
              ]
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": [
            "type",
            "table"
          ],
          "properties": {
            "type": {
              "const": "table"
            },
            "table": {
              "$ref": "#/$defs/richTextTable"
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "richTextTable": {
      "type": "object",
      "description": "A table, as one block of its own. Every row has as many cells as the table has columns; a shorter row is filled with empty cells, a longer one is cut.",
      "required": [
        "rows"
      ],
      "additionalProperties": false,
      "properties": {
        "hasHeaderRow": {
          "type": "boolean",
          "description": "Whether the first row is shown as a header. Absent: true."
        },
        "columnCount": {
          "type": "integer",
          "minimum": 1,
          "maximum": 10,
          "description": "Absent: the number of cells of the widest row. More than 10 columns are cut to 10."
        },
        "rows": {
          "type": "array",
          "minItems": 1,
          "maxItems": 50,
          "description": "The rows, top to bottom. At most 50, the most the editor itself makes; Bunt keeps more that a file brings, and writes them out again.",
          "items": {
            "$ref": "#/$defs/richTextTableRow"
          }
        },
        "columnWidths": {
          "type": "array",
          "items": {
            "type": [
              "number",
              "null"
            ],
            "minimum": 64
          },
          "description": "One entry per column, in points: a fixed width, or null for automatic. Absent: every column automatic. A width below 64 is raised to 64."
        }
      }
    },
    "richTextTableRow": {
      "type": "object",
      "required": [
        "cells"
      ],
      "additionalProperties": false,
      "properties": {
        "cells": {
          "type": "array",
          "maxItems": 10,
          "description": "The cells, left to right.",
          "items": {
            "$ref": "#/$defs/richTextTableCell"
          }
        },
        "minimumHeight": {
          "type": "number",
          "minimum": 0,
          "description": "The least height of the row, in points. Absent: the height of its content. Content taller than this always makes the row taller."
        }
      }
    },
    "richTextTableCell": {
      "type": "object",
      "description": "One cell: text with character formatting only, no headings or lists; a new line inside the cell is \\n. An empty cell is {\"runs\": []}.",
      "required": [
        "runs"
      ],
      "additionalProperties": false,
      "properties": {
        "runs": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/richTextTableCellRun"
          }
        }
      }
    },
    "richTextRun": {
      "type": "object",
      "description": "A piece of text with one formatting.",
      "required": [
        "text"
      ],
      "additionalProperties": false,
      "properties": {
        "text": {
          "type": "string",
          "pattern": "^[^\\n\\r\\u2029]*$",
          "description": "No line break: a new line is a new block. (Bunt cuts a run that holds one into a block per line, and a cell's into spaces.)"
        },
        "traits": {
          "type": "integer",
          "minimum": 0,
          "maximum": 15,
          "description": "Added up: 1 bold, 2 italic, 4 underline, 8 strikethrough. Absent: none."
        },
        "link": {
          "type": "string",
          "pattern": "^[hH][tT][tT][pP][sS]?://",
          "description": "Only http and https; any other address is removed, the words stay."
        },
        "foregroundColor": {
          "type": "string",
          "pattern": "^#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$",
          "description": "Text colour, #RRGGBB or #RRGGBBAA. Absent: the normal text colour, which follows light and dark mode; prefer that for ordinary text."
        },
        "highlightColor": {
          "type": "string",
          "pattern": "^#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$",
          "description": "Highlight behind the text, #RRGGBB or #RRGGBBAA. Absent: none."
        }
      }
    },
    "richTextTableCellRun": {
      "type": "object",
      "description": "A piece of a cell's text with one formatting.",
      "required": [
        "text"
      ],
      "additionalProperties": false,
      "properties": {
        "text": {
          "type": "string",
          "pattern": "^[^\\r\\u2029]*$",
          "description": "A line break inside a cell is written as \\n."
        },
        "traits": {
          "type": "integer",
          "minimum": 0,
          "maximum": 15,
          "description": "Added up: 1 bold, 2 italic, 4 underline, 8 strikethrough. Absent: none."
        },
        "link": {
          "type": "string",
          "pattern": "^[hH][tT][tT][pP][sS]?://",
          "description": "Only http and https; any other address is removed, the words stay."
        },
        "foregroundColor": {
          "type": "string",
          "pattern": "^#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$",
          "description": "Text colour, #RRGGBB or #RRGGBBAA. Absent: the normal text colour, which follows light and dark mode; prefer that for ordinary text."
        },
        "highlightColor": {
          "type": "string",
          "pattern": "^#[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$",
          "description": "Highlight behind the text, #RRGGBB or #RRGGBBAA. Absent: none."
        }
      }
    }
  }
}
