{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.liquidemr.com/mdjsonschema",
  "title": "LiquidEMR MDJSON EHI Export Schema",
  "description": "Schema for LiquidEMR MDJSON EHI export packages (ONC 170.315(b)(10)).",
  "type": "object",
  "required": ["header", "export"],
  "properties": {
    "header": {
      "type": "object",
      "required": ["format", "mdjsonVersion", "schemaUrl"],
      "properties": {
        "format": { "const": "MDJSON" },
        "mdjsonVersion": { "type": "string" },
        "schemaUrl": { "type": "string", "format": "uri" },
        "exporter": {
          "type": "object",
          "properties": {
            "product": { "type": "string" },
            "module": { "type": "string" },
            "build": { "type": "string" }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "export": {
      "type": "object",
      "required": ["exportId", "generatedAt", "dataAsOf"],
      "properties": {
        "exportId": { "type": "string" },
        "generatedAt": { "type": "string", "format": "date-time" },
        "dataAsOf": { "type": "string", "format": "date-time" },
        "timezone": { "type": "string" }
      },
      "additionalProperties": true
    },

    "patients": {
      "type": "array",
      "items": { "$ref": "#/$defs/patient" }
    },

    "notes": {
      "type": "array",
      "items": { "$ref": "#/$defs/note" }
    }
  },

  "$defs": {
    "patient": {
      "type": "object",
      "required": ["PatientId"],
      "properties": {
        "PatientId": { "type": "string" },
        "MRN": { "type": "string" },
        "DOB": { "type": "string", "format": "date" }
      },
      "additionalProperties": true
    },

    "contentBlob": {
      "type": "object",
      "required": ["contentType", "encoding"],
      "properties": {
        "contentType": { "type": "string" },
        "encoding": { "const": "base64" },
        "data": { "type": "string" },
        "ref": { "type": "string" }
      },
      "additionalProperties": false,
      "oneOf": [
        { "required": ["data"] },
        { "required": ["ref"] }
      ]
    },

    "note": {
      "type": "object",
      "required": ["NoteId", "PatientId", "CreateAt"],
      "properties": {
        "NoteId": { "type": "string" },
        "PatientId": { "type": "string" },
        "CreateAt": { "type": "string" },
        "Date": { "type": "string", "format": "date" },
        "FileName": { "type": "string" },
        "Location": { "type": "string" },
        "content": { "$ref": "#/$defs/contentBlob" }
      },
      "additionalProperties": true
    }
  },

  "additionalProperties": true
}