{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://core.canidex.cloud/schema/canidex-dogs-1.0.schema.json",
  "title": "CANIDEX dog collection 1.0",
  "description": "Exchange format for dogs (E315): a collection with header and a list of dogs. No personal data.",
  "type": "object",
  "required": [
    "format",
    "version",
    "source",
    "dogs"
  ],
  "properties": {
    "format": {
      "const": "canidex.dogs"
    },
    "version": {
      "type": "string",
      "pattern": "^1\\."
    },
    "generated_at": {
      "type": "string",
      "format": "date-time"
    },
    "source": {
      "type": "object",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "url": {
          "type": "string",
          "format": "uri"
        },
        "code": {
          "type": "string"
        },
        "license": {
          "type": "string"
        }
      }
    },
    "dogs": {
      "type": "array",
      "maxItems": 50000,
      "items": {
        "$ref": "#/$defs/dog"
      }
    }
  },
  "$defs": {
    "date": {
      "type": "string",
      "pattern": "^\\d{4}(-(0[1-9]|1[0-2])(-(0[1-9]|[12]\\d|3[01]))?)?$",
      "description": "YYYY, YYYY-MM or YYYY-MM-DD"
    },
    "parent": {
      "type": "object",
      "description": "ref of a dog in the same delivery, CANIDEX id, or name with registration number",
      "anyOf": [
        {
          "required": [
            "ref"
          ]
        },
        {
          "required": [
            "id"
          ]
        },
        {
          "required": [
            "name"
          ]
        }
      ],
      "properties": {
        "ref": {
          "type": "string"
        },
        "id": {
          "type": "string",
          "pattern": "^CXD-[0-9A-HJKMNP-TV-Z]{26}$"
        },
        "name": {
          "type": "string"
        },
        "registration": {
          "type": "string"
        }
      }
    },
    "dog": {
      "type": "object",
      "required": [
        "ref",
        "name",
        "breed"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "owner"
            ]
          },
          {
            "required": [
              "owners"
            ]
          },
          {
            "required": [
              "breeder"
            ]
          },
          {
            "required": [
              "breeders"
            ]
          },
          {
            "required": [
              "handler"
            ]
          },
          {
            "required": [
              "person"
            ]
          },
          {
            "required": [
              "people"
            ]
          },
          {
            "required": [
              "address"
            ]
          },
          {
            "required": [
              "email"
            ]
          },
          {
            "required": [
              "phone"
            ]
          }
        ]
      },
      "properties": {
        "ref": {
          "type": "string",
          "minLength": 1,
          "maxLength": 100,
          "description": "stable key of the supplier, unique in the file"
        },
        "id": {
          "type": "string",
          "pattern": "^CXD-[0-9A-HJKMNP-TV-Z]{26}$"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "kennel": {
          "type": "string"
        },
        "breed": {
          "type": "object",
          "anyOf": [
            {
              "required": [
                "fci"
              ]
            },
            {
              "required": [
                "name"
              ]
            }
          ],
          "properties": {
            "fci": {
              "type": "string",
              "description": "Code from the value list breeds: https://core.canidex.cloud/api/v1/exchange/values/breeds"
            },
            "name": {
              "type": "string"
            },
            "variety": {
              "type": "string",
              "description": "Code from the value list varieties: https://core.canidex.cloud/api/v1/exchange/values/varieties"
            }
          }
        },
        "sex": {
          "enum": [
            "MALE",
            "FEMALE",
            "UNKNOWN"
          ]
        },
        "born": {
          "$ref": "#/$defs/date"
        },
        "died": {
          "$ref": "#/$defs/date"
        },
        "colour": {
          "type": "string",
          "description": "Code from the value list colours: https://core.canidex.cloud/api/v1/exchange/values/colours"
        },
        "marking": {
          "type": "string",
          "description": "Code from the value list markings: https://core.canidex.cloud/api/v1/exchange/values/markings"
        },
        "identifiers": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "type",
              "value"
            ],
            "properties": {
              "type": {
                "enum": [
                  "MICROCHIP",
                  "TATTOO",
                  "DNA_PROFILE",
                  "OTHER"
                ]
              },
              "value": {
                "type": "string"
              },
              "issuer": {
                "type": "string"
              }
            }
          }
        },
        "registrations": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "number"
            ],
            "properties": {
              "number": {
                "type": "string",
                "minLength": 1
              },
              "registry": {
                "type": "string"
              },
              "date": {
                "$ref": "#/$defs/date"
              }
            }
          }
        },
        "sire": {
          "$ref": "#/$defs/parent"
        },
        "dam": {
          "$ref": "#/$defs/parent"
        },
        "health": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "type": "string",
                "description": "Code from the value list health-types: https://core.canidex.cloud/api/v1/exchange/values/health-types"
              },
              "result": {
                "type": "string",
                "description": "Value of the scale of the health type (health-types)"
              },
              "date": {
                "$ref": "#/$defs/date"
              },
              "raw": {
                "type": "string"
              }
            },
            "anyOf": [
              {
                "required": [
                  "result"
                ]
              },
              {
                "required": [
                  "raw"
                ]
              }
            ]
          }
        },
        "genetics": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "test"
            ],
            "properties": {
              "test": {
                "type": "string",
                "description": "Code from the value list genetic-tests: https://core.canidex.cloud/api/v1/exchange/values/genetic-tests"
              },
              "marker": {
                "type": "string",
                "description": "Code from the value list markers: https://core.canidex.cloud/api/v1/exchange/values/markers"
              },
              "result": {
                "enum": [
                  "CLEAR",
                  "CARRIER",
                  "AFFECTED"
                ],
                "description": "grading of the result"
              },
              "genotype": {
                "type": "string"
              },
              "date": {
                "$ref": "#/$defs/date"
              },
              "raw": {
                "type": "string",
                "description": "result in the original wording"
              }
            },
            "anyOf": [
              {
                "required": [
                  "result"
                ]
              },
              {
                "required": [
                  "raw"
                ]
              }
            ]
          }
        },
        "titles": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "name"
            ],
            "properties": {
              "code": {
                "type": "string",
                "description": "Code from the value list titles: https://core.canidex.cloud/api/v1/exchange/values/titles"
              },
              "name": {
                "type": "string"
              },
              "date": {
                "$ref": "#/$defs/date"
              }
            }
          }
        },
        "results": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "event",
              "values"
            ],
            "properties": {
              "event": {
                "type": "object",
                "required": [
                  "name"
                ],
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string",
                    "description": "Code from the value list event-types: https://core.canidex.cloud/api/v1/exchange/values/event-types"
                  },
                  "date": {
                    "$ref": "#/$defs/date"
                  },
                  "place": {
                    "type": "string"
                  },
                  "organizer": {
                    "type": "string"
                  }
                }
              },
              "class": {
                "type": "string",
                "description": "Code from the value list event-classes: https://core.canidex.cloud/api/v1/exchange/values/event-classes"
              },
              "values": {
                "type": "object",
                "additionalProperties": {
                  "type": [
                    "string",
                    "number",
                    "boolean"
                  ]
                },
                "description": "Keys: codes from the value list result-types; values from their scale"
              }
            }
          }
        },
        "measurements": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "height_cm"
            ],
            "properties": {
              "height_cm": {
                "type": "number"
              },
              "size_class": {
                "type": "string"
              },
              "date": {
                "$ref": "#/$defs/date"
              }
            }
          }
        },
        "colour_raw": {
          "type": "string",
          "description": "Colour in the original wording"
        },
        "marking_raw": {
          "type": "string",
          "description": "Marking in the original wording"
        }
      }
    }
  }
}
