{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pedigree-json.example/schema/0.4.json",
  "title": "Pedigree JSON v0.4",
  "description": "Structure and types only. Cross-field rules (V1-V18) are in docs/PEDIGREE-JSON-0.4.md section 12.",
  "oneOf": [
    {
      "$ref": "#/$defs/pedigree"
    },
    {
      "$ref": "#/$defs/collection"
    }
  ],
  "$defs": {
    "id": {
      "type": "integer",
      "minimum": 1
    },
    "date": {
      "type": "string",
      "pattern": "^[0-9]{4}(-[0-9]{2}(-[0-9]{2})?)?$"
    },
    "duration": {
      "type": "string",
      "pattern": "^P(?=\\d|T\\d)(\\d+Y)?(\\d+M)?(\\d+W)?(\\d+D)?(T(\\d+H)?(\\d+M)?(\\d+S)?)?$"
    },
    "dateOrDuration": {
      "anyOf": [
        {
          "$ref": "#/$defs/date"
        },
        {
          "$ref": "#/$defs/duration"
        }
      ]
    },
    "familyId": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[^:]+$"
    },
    "globalRef": {
      "type": "string",
      "pattern": "^[^:]+:[0-9]+$"
    },
    "path": {
      "type": "string",
      "pattern": "^[FMPCS]*$"
    },
    "deidentified": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "pattern": "^[a-z_]+(\\.[a-z_]+)*$"
      }
    },
    "ext": {
      "type": "object",
      "propertyNames": {
        "pattern": "^[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)+$"
      }
    },
    "coding": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^[^:|\\s]+:\\S+$"
        },
        {
          "type": "object",
          "properties": {
            "system": {
              "type": "string"
            },
            "code": {
              "type": "string"
            },
            "display": {
              "type": "string"
            }
          },
          "required": [
            "system",
            "code"
          ],
          "additionalProperties": false
        }
      ]
    },
    "reference": {
      "type": "object",
      "properties": {
        "href": {
          "type": "string",
          "format": "uri-reference"
        },
        "mediaType": {
          "type": "string"
        },
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "title": {
          "type": "string"
        },
        "rel": {
          "type": "string"
        },
        "data": {
          "type": "string",
          "contentEncoding": "base64"
        },
        "size": {
          "type": "integer",
          "minimum": 0
        }
      },
      "additionalProperties": false,
      "oneOf": [
        {
          "required": [
            "href"
          ],
          "not": {
            "required": [
              "data"
            ]
          }
        },
        {
          "required": [
            "data",
            "mediaType",
            "sha256"
          ],
          "not": {
            "required": [
              "href"
            ]
          }
        }
      ]
    },
    "link": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "properties": {
            "href": {
              "type": "string",
              "format": "uri-reference"
            },
            "mediaType": {
              "type": "string"
            },
            "sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "title": {
              "type": "string"
            },
            "rel": {
              "type": "string"
            }
          },
          "additionalProperties": false,
          "required": [
            "href"
          ]
        }
      ]
    },
    "note": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "properties": {
            "text": {
              "type": "string"
            },
            "author": {
              "type": "string"
            },
            "date": {
              "$ref": "#/$defs/date"
            }
          },
          "required": [
            "text"
          ],
          "additionalProperties": false
        }
      ]
    },
    "identifier": {
      "type": "object",
      "properties": {
        "system": {
          "type": "string"
        },
        "value": {
          "type": "string"
        }
      },
      "required": [
        "value"
      ],
      "additionalProperties": false
    },
    "attestation": {
      "type": "object",
      "properties": {
        "method": {
          "enum": [
            "safe-harbor",
            "expert-determination",
            "other"
          ]
        },
        "by": {
          "type": "string"
        },
        "date": {
          "$ref": "#/$defs/date"
        },
        "statement": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "condition": {
      "type": "object",
      "properties": {
        "label": {
          "type": "string"
        },
        "codes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/coding"
          }
        },
        "site": {
          "enum": [
            "breast",
            "ovary",
            "prostate",
            "pancreas",
            "colorectum",
            "endometrium",
            "stomach",
            "small_bowel",
            "urothelial",
            "hepatobiliary",
            "brain",
            "sebaceous_skin"
          ]
        }
      },
      "required": [
        "label"
      ],
      "additionalProperties": false
    },
    "healthEntry": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^[A-Za-z0-9_.-]+$"
        },
        {
          "type": "object",
          "properties": {
            "condition": {
              "type": "string",
              "pattern": "^[A-Za-z0-9_.-]+$"
            },
            "status": {
              "enum": [
                "affected",
                "unaffected",
                "carrier",
                "presymptomatic",
                "unknown"
              ]
            },
            "onset": {
              "$ref": "#/$defs/dateOrDuration"
            },
            "source": {
              "enum": [
                "reported",
                "documented"
              ]
            },
            "notes": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/note"
              }
            },
            "laterality": {
              "enum": [
                "left",
                "right",
                "bilateral"
              ]
            },
            "receptors": {
              "type": "object",
              "properties": {
                "er": {
                  "enum": [
                    "positive",
                    "negative",
                    "unknown"
                  ]
                },
                "pr": {
                  "enum": [
                    "positive",
                    "negative",
                    "unknown"
                  ]
                },
                "her2": {
                  "enum": [
                    "positive",
                    "negative",
                    "unknown"
                  ]
                }
              },
              "additionalProperties": false
            },
            "histology": {
              "type": "string"
            },
            "location": {
              "enum": [
                "proximal",
                "distal"
              ]
            },
            "markers": {
              "type": "object",
              "properties": {
                "msi": {
                  "enum": [
                    "high",
                    "low",
                    "stable",
                    "unknown"
                  ]
                },
                "ihc": {
                  "type": "object",
                  "properties": {
                    "MLH1": {
                      "enum": [
                        "present",
                        "absent",
                        "unknown"
                      ]
                    },
                    "MSH2": {
                      "enum": [
                        "present",
                        "absent",
                        "unknown"
                      ]
                    },
                    "MSH6": {
                      "enum": [
                        "present",
                        "absent",
                        "unknown"
                      ]
                    },
                    "PMS2": {
                      "enum": [
                        "present",
                        "absent",
                        "unknown"
                      ]
                    }
                  },
                  "additionalProperties": false
                }
              },
              "additionalProperties": false
            }
          },
          "required": [
            "condition"
          ],
          "additionalProperties": false
        }
      ]
    },
    "name": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "properties": {
            "given": {
              "type": "string"
            },
            "middle": {
              "type": "string"
            },
            "family": {
              "type": "string"
            },
            "text": {
              "type": "string"
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "parentRef": {
      "oneOf": [
        {
          "$ref": "#/$defs/id"
        },
        {
          "type": "object",
          "properties": {
            "id": {
              "$ref": "#/$defs/id"
            },
            "type": {
              "enum": [
                "bio",
                "adopt",
                "step",
                "foster"
              ]
            }
          },
          "required": [
            "id"
          ],
          "additionalProperties": false
        }
      ]
    },
    "kin": {
      "type": "object",
      "properties": {
        "of": {
          "$ref": "#/$defs/id"
        },
        "rel": {
          "type": "string",
          "minLength": 1
        },
        "system": {
          "type": "string"
        },
        "side": {
          "enum": [
            "paternal",
            "maternal"
          ]
        }
      },
      "required": [
        "of",
        "rel"
      ],
      "additionalProperties": false
    },
    "infer": {
      "type": "object",
      "properties": {
        "candidates": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          },
          "minItems": 1
        },
        "chosen": {
          "$ref": "#/$defs/path"
        },
        "level": {
          "enum": [
            1,
            2,
            3
          ]
        },
        "basis": {
          "type": "string"
        }
      },
      "required": [
        "candidates",
        "level"
      ],
      "additionalProperties": false
    },
    "slot": {
      "enum": [
        "whole",
        "N",
        "S",
        "E",
        "W",
        "NW",
        "NE",
        "SW",
        "SE"
      ]
    },
    "color": {
      "type": "string",
      "pattern": "^#[0-9a-fA-F]{6}$"
    },
    "legend": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "color": {
            "$ref": "#/$defs/color"
          },
          "slot": {
            "$ref": "#/$defs/slot"
          }
        },
        "required": [
          "color"
        ],
        "additionalProperties": false
      }
    },
    "personView": {
      "type": "object",
      "properties": {
        "pos": {
          "type": "array",
          "items": {
            "type": "number"
          },
          "minItems": 2,
          "maxItems": 2
        },
        "fill": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "condition": {
                "type": "string"
              },
              "slot": {
                "$ref": "#/$defs/slot"
              },
              "color": {
                "$ref": "#/$defs/color"
              }
            },
            "required": [
              "condition"
            ],
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "pedigreeView": {
      "type": "object",
      "properties": {
        "show": {
          "type": "object",
          "properties": {
            "title": {
              "type": "boolean"
            },
            "subtitle": {
              "type": "boolean"
            },
            "footer": {
              "type": "boolean"
            }
          },
          "additionalProperties": false
        },
        "labels": {
          "type": "object",
          "properties": {
            "name": {
              "type": "boolean"
            },
            "dob": {
              "type": "boolean"
            },
            "health": {
              "type": "array",
              "items": {
                "type": "boolean"
              }
            }
          },
          "additionalProperties": false
        },
        "legend": {
          "$ref": "#/$defs/legend"
        },
        "rules": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "field": {
                "type": "string",
                "minLength": 1,
                "maxLength": 64
              },
              "when": {
                "enum": [
                  "equal",
                  "not_equal",
                  "greater",
                  "less",
                  "true",
                  "false",
                  "not_null",
                  "always",
                  "constant"
                ]
              },
              "value": {
                "type": "string",
                "maxLength": 120
              },
              "show": {
                "enum": [
                  "UL",
                  "UR",
                  "LL",
                  "LR",
                  "left",
                  "right",
                  "subtext_1",
                  "subtext_2",
                  "subtext_3"
                ]
              },
              "color": {
                "$ref": "#/$defs/color"
              }
            },
            "required": [
              "field",
              "when",
              "show"
            ],
            "additionalProperties": false
          },
          "maxItems": 50
        },
        "notes": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "text": {
                "type": "string"
              },
              "headline": {
                "type": "string"
              },
              "pos": {
                "type": "array",
                "items": {
                  "type": "number"
                },
                "minItems": 2,
                "maxItems": 2
              },
              "size": {
                "type": "number",
                "minimum": 6,
                "maximum": 96
              },
              "color": {
                "$ref": "#/$defs/color"
              },
              "bold": {
                "type": "boolean"
              }
            },
            "required": [
              "text",
              "pos"
            ],
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "person": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "sex": {
          "enum": [
            "M",
            "F",
            "U",
            "O"
          ]
        },
        "name": {
          "$ref": "#/$defs/name"
        },
        "dob": {
          "$ref": "#/$defs/date"
        },
        "dod": {
          "$ref": "#/$defs/date"
        },
        "deceased": {
          "type": "boolean"
        },
        "age": {
          "type": "integer",
          "minimum": 0,
          "maximum": 130
        },
        "age_at_death": {
          "type": "integer",
          "minimum": 0,
          "maximum": 130
        },
        "proband": {
          "type": "boolean"
        },
        "placeholder": {
          "type": "boolean"
        },
        "health": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/healthEntry"
          }
        },
        "ancestry": {
          "type": "object",
          "properties": {
            "ashkenazi_jewish": {
              "type": "boolean"
            },
            "race_ethnicity": {
              "enum": [
                "white",
                "black_african_american",
                "hispanic_latina",
                "asian",
                "pacific_islander",
                "native_american_alaska_native",
                "other",
                "unknown"
              ]
            },
            "hispanic_us_born": {
              "type": "boolean"
            },
            "subgroup": {
              "enum": [
                "chinese",
                "japanese",
                "filipino",
                "other_asian",
                "hawaiian",
                "other_pacific_islander"
              ]
            }
          },
          "additionalProperties": false
        },
        "genetics": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "gene": {
                "type": "string",
                "pattern": "^[A-Z0-9-]+$"
              },
              "result": {
                "enum": [
                  "pathogenic",
                  "likely_pathogenic",
                  "vus",
                  "negative"
                ]
              },
              "variant": {
                "type": "string"
              },
              "zygosity": {
                "enum": [
                  "heterozygous",
                  "homozygous",
                  "unknown"
                ]
              },
              "date": {
                "$ref": "#/$defs/date"
              }
            },
            "required": [
              "gene",
              "result"
            ],
            "additionalProperties": false
          }
        },
        "risk_factors": {
          "type": "object",
          "properties": {
            "menarche_age": {
              "type": "integer",
              "minimum": 5,
              "maximum": 25
            },
            "first_live_birth_age": {
              "type": "integer",
              "minimum": 10,
              "maximum": 60
            },
            "parity": {
              "type": "integer",
              "minimum": 0,
              "maximum": 30
            },
            "menopausal": {
              "type": "boolean"
            },
            "menopause_age": {
              "type": "integer",
              "minimum": 25,
              "maximum": 70
            },
            "hrt": {
              "type": "boolean"
            },
            "hrt_years": {
              "type": "number",
              "minimum": 0,
              "maximum": 80
            },
            "oral_contraceptives": {
              "enum": [
                "never",
                "former",
                "current"
              ]
            },
            "bmi": {
              "type": "number",
              "minimum": 10,
              "maximum": 80
            },
            "height_cm": {
              "type": "number",
              "minimum": 100,
              "maximum": 230
            },
            "benign_biopsies": {
              "type": "integer",
              "minimum": 0,
              "maximum": 50
            },
            "atypical_hyperplasia": {
              "type": "boolean"
            },
            "lcis": {
              "type": "boolean"
            },
            "breast_density": {
              "enum": [
                "a",
                "b",
                "c",
                "d"
              ]
            },
            "alcohol_g_per_day": {
              "type": "number",
              "minimum": 0,
              "maximum": 500
            }
          },
          "additionalProperties": false
        },
        "parents": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/parentRef"
          },
          "maxItems": 2
        },
        "kin": {
          "$ref": "#/$defs/kin"
        },
        "infer": {
          "$ref": "#/$defs/infer"
        },
        "same_as": {
          "$ref": "#/$defs/globalRef"
        },
        "notes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/note"
          }
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/link"
          }
        },
        "attachments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reference"
          }
        },
        "identifiers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "deidentified": {
          "$ref": "#/$defs/deidentified"
        },
        "view": {
          "$ref": "#/$defs/personView"
        },
        "ext": {
          "$ref": "#/$defs/ext"
        }
      },
      "required": [
        "id",
        "sex"
      ],
      "additionalProperties": false
    },
    "couple": {
      "type": "object",
      "properties": {
        "partners": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/id"
          },
          "minItems": 2,
          "maxItems": 2
        },
        "consanguineous": {
          "type": "boolean"
        }
      },
      "required": [
        "partners"
      ],
      "additionalProperties": false
    },
    "indexEntry": {
      "type": "object",
      "properties": {
        "family": {
          "$ref": "#/$defs/familyId"
        },
        "id": {
          "$ref": "#/$defs/id"
        },
        "path": {
          "$ref": "#/$defs/path"
        },
        "paths": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/path"
          }
        },
        "chosen": {
          "$ref": "#/$defs/path"
        },
        "level": {
          "enum": [
            0,
            1,
            2,
            3
          ]
        },
        "status": {
          "enum": [
            "affected",
            "carrier"
          ]
        }
      },
      "required": [
        "id",
        "level"
      ],
      "additionalProperties": false
    },
    "index": {
      "type": "object",
      "properties": {
        "of": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "proband": {
          "$ref": "#/$defs/id"
        },
        "conditions": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/indexEntry"
            }
          }
        }
      },
      "required": [
        "conditions"
      ],
      "additionalProperties": false
    },
    "meta": {
      "type": "object",
      "properties": {
        "family": {
          "$ref": "#/$defs/familyId"
        },
        "title": {
          "type": "string"
        },
        "subtitle": {
          "type": "string"
        },
        "footer": {
          "type": "string"
        },
        "text": {
          "type": "string"
        },
        "notes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/note"
          }
        },
        "narrative": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "$ref": "#/$defs/reference"
            }
          ]
        },
        "as_of": {
          "$ref": "#/$defs/date"
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/link"
          }
        },
        "attachments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reference"
          }
        },
        "identifiers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "deidentified": {
          "$ref": "#/$defs/deidentified"
        },
        "attestation": {
          "$ref": "#/$defs/attestation"
        },
        "ext": {
          "$ref": "#/$defs/ext"
        }
      },
      "additionalProperties": false
    },
    "collectionMeta": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string"
        },
        "text": {
          "type": "string"
        },
        "notes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/note"
          }
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/link"
          }
        },
        "deidentified": {
          "$ref": "#/$defs/deidentified"
        },
        "attestation": {
          "$ref": "#/$defs/attestation"
        },
        "ext": {
          "$ref": "#/$defs/ext"
        }
      },
      "additionalProperties": false
    },
    "familyMeta": {
      "type": "object",
      "properties": {
        "family": {
          "$ref": "#/$defs/familyId"
        },
        "title": {
          "type": "string"
        },
        "subtitle": {
          "type": "string"
        },
        "footer": {
          "type": "string"
        },
        "text": {
          "type": "string"
        },
        "notes": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/note"
          }
        },
        "narrative": {
          "oneOf": [
            {
              "type": "string"
            },
            {
              "$ref": "#/$defs/reference"
            }
          ]
        },
        "as_of": {
          "$ref": "#/$defs/date"
        },
        "links": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/link"
          }
        },
        "attachments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/reference"
          }
        },
        "identifiers": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/identifier"
          }
        },
        "deidentified": {
          "$ref": "#/$defs/deidentified"
        },
        "attestation": {
          "$ref": "#/$defs/attestation"
        },
        "ext": {
          "$ref": "#/$defs/ext"
        }
      },
      "required": [
        "family"
      ],
      "additionalProperties": false
    },
    "pedigreeBody": {
      "type": "object",
      "properties": {
        "conditions": {
          "type": "object",
          "propertyNames": {
            "pattern": "^[A-Za-z0-9_.-]+$"
          },
          "additionalProperties": {
            "$ref": "#/$defs/condition"
          }
        },
        "people": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/person"
          }
        },
        "siblings": {
          "type": "array",
          "items": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/id"
            },
            "minItems": 2
          }
        },
        "couples": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/couple"
          }
        }
      },
      "additionalProperties": false
    },
    "pedigree": {
      "type": "object",
      "properties": {
        "format": {
          "const": "pedigree-json/0.4"
        },
        "meta": {
          "$ref": "#/$defs/meta"
        },
        "pedigree": {
          "$ref": "#/$defs/pedigreeBody"
        },
        "view": {
          "$ref": "#/$defs/pedigreeView"
        },
        "index": {
          "$ref": "#/$defs/index"
        }
      },
      "required": [
        "format",
        "pedigree"
      ],
      "additionalProperties": false
    },
    "family": {
      "type": "object",
      "properties": {
        "meta": {
          "$ref": "#/$defs/familyMeta"
        },
        "pedigree": {
          "$ref": "#/$defs/pedigreeBody"
        },
        "view": {
          "$ref": "#/$defs/pedigreeView"
        },
        "index": {
          "$ref": "#/$defs/index"
        }
      },
      "required": [
        "meta",
        "pedigree"
      ],
      "additionalProperties": false
    },
    "collection": {
      "type": "object",
      "properties": {
        "format": {
          "const": "pedigree-json/0.4"
        },
        "meta": {
          "$ref": "#/$defs/collectionMeta"
        },
        "families": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/family"
          },
          "minItems": 1
        },
        "index": {
          "$ref": "#/$defs/index"
        }
      },
      "required": [
        "format",
        "families"
      ],
      "additionalProperties": false
    }
  }
}
