{
  "$id": "https://developers.e-rezept.ch/2024-10/chmed16a-schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "e-prescription",
  "description": "e-prescription according to chmed16a specification",
  "required": [
    "Patient",
    "Medicaments",
    "MedType",
    "Id",
    "Auth",
    "Dt"
  ], 
  "type": "object",
  "properties": {
    "rev": {
      "type": "integer"
    },
    "Patient": {
      "type": "object",
      "required": ["FName", "LName", "BDt"],
      "properties": {
        "FName": {
          "type": "string"
        },
        "LName": {
          "type": "string"
        },
        "BDt": {
          "type": "string"
        },
        "Gender": {
          "type": "integer",
          "enum": [
            1,
            2
          ]
        },
        "Street": {
          "type": "string"
        },
        "Zip": {
          "type": "string",
          "minLength": 4,
          "maxLength": 10,
          "pattern": "^[0-9]{4,5}$"
        },
        "Ids": {
          "type": "array"
        }
      }
    },
    "HcPerson": {
      "type":"object",
        "required": ["FName", "LName", "Gln"],
        "properties": {
          "Gln": {
           "type": "string",
           "minLength": 13,
           "maxLength": 13,
           "pattern": "^[0-9]*$"
          },
          "FName": {
           "type": "string"
          },
          "LName": {
           "type": "string"
          },
          "Zsr": {
            "type": "string",
            "minLength": 0,
            "maxLength": 7   
          }
        }
    },
    "HcOrg": {
      "type":"object",
        "required": ["Name", "Street", "Zip", "City"],      
        "properties": {
          "Name": {
           "type": "string"
          },
          "NameAffix": {
           "type": "string"
          },
          "Street": {
           "type": "string"
          },
          "Zip": {
           "type": "string",
           "minLength": 4,
           "maxLength": 5,
           "pattern": "^[0-9]{4,5}$"
          },
          "City": {
           "type": "string"
          },
          "Country": {
           "type": "string"
          },
          "Zsr": {
            "type": "string",
            "minLength": 0,
            "maxLength": 7
          },
          "Phone": {
           "type": "string"
          },
          "Email": {
           "type": "string"
          }
        }
    },  
    "Medicaments": {
      "type": "array",
      "required": ["Id", "IdType"],
      "minItems": 1,
      "items": {
        "type": "object",
        "properties": {
          "Id": {
            "type": "string"
          },
          "IdType": {
            "type": "integer",
            "enum": [
              1,
              2,
              3
            ]
          },
          "Pos": {
            "type": "array",
            "minItems": 1,
            "items": {
              "type": "object",
              "properties": {
                "DtTo": {
                  "type": "string",
                  "format": "date"
                },
                "D": {
                  "type": "array",
                  "minItems": 1,
                  "maxItems": 4,
                  "items": {
                    "type": "number"
                  }
                }
              },
              "required": [
                "D"
              ]
            }
          },
          "Unit": {
            "type": "string"
          },
          "AppInstr": {
            "type": "string"
          },
          "Rep": {
            "type": "integer",
            "minimum": 0,
            "maximum": 24
          },
          "NbPack": {
            "type": "integer"
          },
          "Subs": {
            "type": "integer",
            "enum": [
              0,
              1
            ]
          }
        },
        "required": [
          "Id",
          "IdType"
        ],
        "allOf": [
           {
            "if": {
              "properties": { "Pos": { "minItems": 1 }
              },
              		"required": [
                      "Pos"
                      ]
            },
            "then": {
              "required": ["Unit"]
            }
          }
        ]
      }
    },
    "MedType": {
      "type": "number",
      "enum": [
        3
      ]
    },
    "Id": {
      "type": "string"
    },
    "Auth": {
      "type": "string",
      "minLength": 13,
      "maxLength": 13,
      "pattern": "^[0-9]*$"
    },
    "Zsr": {
      "type": "string"
    },
    "Dt": {
      "type": "string",
      "format": "date-time"
    },
    "Rmk": {
      "type": "string"
    },
    "PFields": {
      "type": "array"
    },
    "PSchema": {
      "type": "string"
    }
  },
  "if": {
    "properties": { "rev": { "enum": [2,3] }
      },
          "required": [
            "rev"
            ]
  },
  "then": {
    "required": ["HcOrg"]
  },
  "else": true,    
  "additionalProperties": false
}