{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vetdatastandards.com/schemas/v0.1/patient.schema.json",
  "title": "Patient",
  "description": "An animal. primaryClientId is optional. Do not invent a client to satisfy this schema.",
  "type": "object",
  "additionalProperties": false,
  "required": ["id", "practiceId", "name", "species", "source"],
  "properties": {
    "id": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "source": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/source" },
    "matchKeys": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/matchKeys" },
    "createdAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "updatedAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "deletedAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
    "active": { "type": "boolean" },
    "extensions": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/extensions" },
    "practiceId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "primaryClientId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
    "name": { "type": "string", "minLength": 1, "maxLength": 128 },
    "species": {
      "type": "string",
      "enum": [
        "canine",
        "feline",
        "avian",
        "rabbit",
        "small_mammal",
        "reptile",
        "equine",
        "bovine",
        "ovine",
        "caprine",
        "porcine",
        "camelid",
        "other",
        "unknown"
      ]
    },
    "speciesRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
    "breed": { "type": "string", "minLength": 1, "maxLength": 128 },
    "sex": {
      "type": "string",
      "enum": ["male", "female", "other", "unknown"]
    },
    "sexRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
    "neuterStatus": {
      "type": "string",
      "enum": ["intact", "neutered", "unknown"]
    },
    "neuterStatusRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
    "dateOfBirth": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/date" },
    "colour": { "type": "string", "minLength": 1, "maxLength": 64 },
    "microchip": { "type": "string", "minLength": 1, "maxLength": 64 },
    "status": {
      "type": "string",
      "enum": ["active", "inactive", "deceased", "transferred", "other"]
    },
    "statusRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
    "accountReference": { "type": "string", "minLength": 1, "maxLength": 64 },
    "identifiers": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["system", "value"],
        "properties": {
          "system": {
            "type": "string",
            "minLength": 1,
            "maxLength": 128,
            "description": "Identifier system label, for example ueln or official-tag. Prefer reverse-domain when the system is a vendor."
          },
          "value": { "type": "string", "minLength": 1, "maxLength": 128 }
        }
      }
    },
    "weights": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["recordedAt", "valueKg"],
        "properties": {
          "recordedAt": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/dateTime" },
          "valueKg": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/nonNegativeDecimal" }
        }
      }
    },
    "allergies": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["substance"],
        "properties": {
          "substance": { "type": "string", "minLength": 1, "maxLength": 255 },
          "reaction": { "type": "string", "minLength": 1, "maxLength": 512 },
          "severity": {
            "type": "string",
            "enum": ["mild", "moderate", "severe", "unknown"]
          },
          "severityRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" }
        },
        "if": {
          "properties": { "severity": { "const": "unknown" } },
          "required": ["severity"]
        },
        "then": {
          "required": ["severityRaw"]
        }
      }
    },
    "relatedParties": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["clientId", "role"],
        "properties": {
          "clientId": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/id" },
          "role": {
            "type": "string",
            "enum": ["owner", "keeper", "trainer", "other"]
          },
          "roleRaw": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/rawText" },
          "sharePercent": { "$ref": "https://vetdatastandards.com/schemas/v0.1/common.schema.json#/$defs/nonNegativeDecimal" }
        },
        "if": {
          "properties": { "role": { "const": "other" } },
          "required": ["role"]
        },
        "then": {
          "required": ["roleRaw"]
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": { "species": { "enum": ["other", "unknown"] } },
        "required": ["species"]
      },
      "then": { "required": ["speciesRaw"] }
    },
    {
      "if": {
        "properties": { "sex": { "enum": ["other", "unknown"] } },
        "required": ["sex"]
      },
      "then": { "required": ["sexRaw"] }
    },
    {
      "if": {
        "properties": { "neuterStatus": { "const": "unknown" } },
        "required": ["neuterStatus"]
      },
      "then": { "required": ["neuterStatusRaw"] }
    },
    {
      "if": {
        "properties": { "status": { "const": "other" } },
        "required": ["status"]
      },
      "then": { "required": ["statusRaw"] }
    }
  ]
}
