{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "kinsketch:genogram-file:2",
  "title": "Genogram file, format version 2",
  "description": "What Kinsketch writes when a genogram is saved and what it accepts when one is opened, for format version 2. Properties not described here are ignored on open, and a missing optional property takes the default given. Text is measured in characters (Unicode code points), and on open the control characters XML cannot hold (U+0000 to U+001F except tab, line feed and carriage return, and U+FFFE and U+FFFF) are dropped from text. Twins listed apart are put next to each other in the birth order. A note on a relationship line, or on the whole drawing, is never drawn on the picture. Beyond what this schema states, the app refuses a file in which: an id is used more than once anywhere in the file; the index person, a partner, a child, a twin, a person's household, or either end of a relationship line names something that is not in the file; a partner is not a person (has a kind other than person); someone is listed as their own parent, or is their own ancestor through any chain of parents; a child is listed twice under one couple, or has more than three sets of parents, or more than one set of birth parents (link birth); a twin is not one of that couple's children, or is in more than one set of twins of that couple; a relationship line joins someone to themselves; more than three relationship lines join the same two people, or two lines between the same two people are the same line (the same kind and, for a kind with an arrow, the same direction). A file of format version 1 is held to genogram-v1.schema.json and the rules of version 1, and brought up to date as it opens; it is never written again as version 1.",
  "type": "object",
  "required": ["format", "version", "genogram"],
  "properties": {
    "format": { "const": "genogram" },
    "version": { "const": 2 },
    "app": { "type": "string", "description": "The app and version that wrote the file." },
    "createdAt": { "type": "string", "description": "ISO 8601 time the genogram was first saved. A value that is not a date is ignored." },
    "savedAt": { "type": "string", "description": "ISO 8601 time of this save. A value that is not a date is ignored." },
    "genogram": { "$ref": "#/$defs/genogram" }
  },
  "$defs": {
    "id": { "type": "string", "pattern": "^[A-Za-z0-9_-]{1,40}$" },
    "name": {
      "description": "A short piece of text with something to show: at least one character that is neither white space nor a control character the app drops.",
      "type": "string",
      "minLength": 1,
      "pattern": "[^\\s\\u0000-\\u0008\\u000B\\u000C\\u000E-\\u001F\\uFFFE\\uFFFF]"
    },
    "genogram": {
      "type": "object",
      "properties": {
        "title": { "type": "string", "maxLength": 80 },
        "note": {
          "description": "About the whole drawing: who it was made with, what it is for. The app shows it in the details panel, the text outline and the printed notes table, never on the picture.",
          "type": "string",
          "maxLength": 300
        },
        "indexPersonId": {
          "description": "The person the genogram is about, drawn with a double outline.",
          "anyOf": [{ "$ref": "#/$defs/id" }, { "type": "null" }]
        },
        "people": { "type": "array", "maxItems": 400, "items": { "$ref": "#/$defs/person" } },
        "unions": { "type": "array", "maxItems": 800, "items": { "$ref": "#/$defs/union" } },
        "bonds": { "type": "array", "maxItems": 1600, "items": { "$ref": "#/$defs/bond" } },
        "households": { "type": "array", "maxItems": 40, "items": { "$ref": "#/$defs/household" } }
      }
    },
    "person": {
      "type": "object",
      "required": ["id"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "kind": { "enum": ["person", "pet", "pregnancy", "stillbirth", "miscarriage", "abortion"], "default": "person" },
        "sex": { "enum": ["male", "female", "nonbinary", "unknown"], "default": "unknown" },
        "name": { "type": "string", "maxLength": 60 },
        "born": { "type": "string", "maxLength": 24 },
        "died": { "type": "string", "maxLength": 24 },
        "deceased": { "type": "boolean" },
        "causeOfDeath": { "type": "string", "maxLength": 40, "description": "Shown with the date of death, for someone who has died." },
        "age": { "type": "string", "maxLength": 24, "description": "Empty means the app works it out from the years." },
        "orientation": { "enum": ["unspecified", "gay", "bisexual"], "default": "unspecified" },
        "transgender": { "type": "boolean" },
        "illness": { "enum": ["none", "active", "remission"], "default": "none" },
        "substance": { "enum": ["none", "suspected", "active", "recovery"], "default": "none" },
        "conditions": {
          "description": "Named physical or psychological conditions, written beside the symbol. The shading of the symbol comes from illness and substance.",
          "type": "array",
          "maxItems": 3,
          "uniqueItems": true,
          "items": { "allOf": [{ "$ref": "#/$defs/name" }, { "maxLength": 40 }] }
        },
        "smoker": { "type": "boolean" },
        "obesity": { "type": "boolean" },
        "languageProblem": { "type": "boolean" },
        "secret": { "type": "boolean" },
        "migration": { "enum": ["none", "multicultural", "immigrated"], "default": "none" },
        "location": { "type": "string", "maxLength": 40 },
        "income": { "type": "string", "maxLength": 40 },
        "occupation": { "type": "string", "maxLength": 40 },
        "education": { "type": "string", "maxLength": 40 },
        "connections": { "type": "array", "maxItems": 3, "items": { "type": "string", "maxLength": 24 } },
        "households": {
          "description": "The households this person lives in: none, one, or two homes.",
          "type": "array",
          "maxItems": 2,
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/id" }
        },
        "notes": { "type": "string", "maxLength": 300 }
      }
    },
    "union": {
      "description": "One or two partners and the children who belong to them together.",
      "type": "object",
      "required": ["id", "partners"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "partners": { "type": "array", "minItems": 1, "maxItems": 2, "uniqueItems": true, "items": { "$ref": "#/$defs/id" } },
        "type": {
          "description": "The kind of relationship: how the couple line is drawn.",
          "enum": ["married", "committed", "partnered", "affair"],
          "default": "married"
        },
        "state": {
          "description": "How the relationship stands now: the marks across the line. Divorced and remarried are only for a married couple.",
          "enum": ["together", "separated", "divorced", "reconciled", "remarried"],
          "default": "together"
        },
        "secret": { "type": "boolean" },
        "note": { "type": "string", "maxLength": 40 },
        "children": {
          "description": "In birth order, oldest first.",
          "type": "array",
          "items": {
            "type": "object",
            "required": ["person"],
            "properties": {
              "person": { "$ref": "#/$defs/id" },
              "link": {
                "description": "How the child joined this family. Kinship: in the care of relatives or a guardian.",
                "enum": ["birth", "adopted", "foster", "kinship", "donor", "surrogate"],
                "default": "birth"
              },
              "note": { "type": "string", "maxLength": 24 }
            }
          }
        },
        "twins": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["ids"],
            "properties": {
              "ids": { "type": "array", "minItems": 2, "maxItems": 12, "uniqueItems": true, "items": { "$ref": "#/$defs/id" } },
              "identical": { "type": "boolean" }
            }
          }
        }
      },
      "if": { "required": ["state"], "properties": { "state": { "enum": ["divorced", "remarried"] } } },
      "then": { "properties": { "type": { "const": "married" } } }
    },
    "bond": {
      "description": "A relationship line between two people. Up to three different lines can join the same two people. Direction matters for focused, caretaker, neglect and the abuse kinds: it runs from 'from' to 'to'.",
      "type": "object",
      "required": ["id", "from", "to"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "from": { "$ref": "#/$defs/id" },
        "to": { "$ref": "#/$defs/id" },
        "kind": {
          "enum": [
            "close",
            "fused",
            "distant",
            "hostile",
            "close-hostile",
            "fused-hostile",
            "cutoff",
            "cutoff-repaired",
            "focused",
            "caretaker",
            "spiritual",
            "physical-abuse",
            "emotional-abuse",
            "sexual-abuse",
            "neglect"
          ],
          "default": "close"
        },
        "note": {
          "description": "About this line. The app shows it in the details panel, the text outline and the printed notes table, never on the picture.",
          "type": "string",
          "maxLength": 160
        }
      }
    },
    "household": {
      "type": "object",
      "required": ["id"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "name": { "type": "string", "maxLength": 40 }
      }
    }
  }
}
