{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Multi-Media Interchange Format",
  "description": "The JSON-LD objects exchanged by CLAMS services.",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "metadata": {
      "$ref": "#/definitions/mmifMetadata"
    },
    "documents": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/annotation"
      },
      "minLength": 1
    },
    "views": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/view"
      }
    }
  },
  "required": [
    "metadata",
    "documents",
    "views"
  ],
  "definitions": {
    "strStrMap": {
      "type": "object",
      "patternProperties": {
        ".+": {
          "anyOf": [
            {"type": "string"},
            {"type": "array", "items": { "type": "string" }}
          ]
        }
      }
    },
    "mmifMetadata": {
      "type": "object",
      "properties": {
        "mmif": {
          "type": "string",
          "format": "uri",
          "minLength": 7
        }
      },
      "required": [
        "mmif"
      ]
    },
    "viewMetadata": {
      "type": "object",
      "properties": {
        "timestamp": {
          "type": "string",
          "format": "date-time"
        },
        "app": {
          "type": "string",
          "format": "uri",
          "minLength": 7
        },
        "contains": {
          "type": "object",
          "additionalProperties": false,
          "patternProperties": {
            "^https?:\/\/": {
              "type": "object"
            }
          }
        },
        "error": {
          "type": "object", 
          "properties": {
            "message": {
              "type": "string",
              "minLength": 1
            },
            "stackTrace": {
              "type": "string"
            }
          },
          "required": ["message"]
        },
        "warnings": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "minLength": 1
        },
        "parameters": {
          "$ref": "#/definitions/strStrMap"
        },
        "appConfiguration": {
          "type": "object"
        }
      },
      "oneOf": [
        {
          "required": [
            "app",
            "contains"
          ]
        },
        {
          "required": [
            "app",
            "warnings"
          ]
        },
        {
          "required": [
            "app",
            "error"
          ]
        }
      ]
    },
    "text": {
      "type": "object",
      "properties": {
        "@value": {
          "type": "string"
        },
        "@language": {
          "type": "string"
        }
      },
      "additionalProperties": false,
      "required": [
        "@value"
      ]
    },
    "view": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "metadata": {
          "$ref": "#/definitions/viewMetadata"
        },
        "annotations": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/annotation"
          }
        }
      },
      "additionalProperties": false,
      "required": [
        "id",
        "metadata",
        "annotations"
      ]
    },
    "annotation": {
      "type": "object",
      "properties": {
        "@type": {
          "type": "string",
          "minLength": 1
        },
        "properties": {
          "$ref": "#/definitions/annotationProperties"
        }
      },
      "required": [
        "@type",
        "properties"
      ],
      "additionalProperties": false
    },
    "annotationProperties": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        }
      },
      "required": [
        "id"
      ]
    }
  }
}

