{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://vdprotocol.org/schemas/vdp.v0-1.schema.json",
  "title": "View Descriptor Protocol (VDP) v0.1",
  "description": "Schema for VDP view descriptor documents. Validates standalone ViewDescriptor and MultiViewDescriptor payloads. Definitions in $defs can be referenced by other schemas for inline body transport (_view / _views). The discovery document served at /.well-known/vdp has its own schema: vdp-discovery.v0-1.schema.json.",

  "oneOf": [
    { "$ref": "#/$defs/ViewDescriptor" },
    { "$ref": "#/$defs/MultiViewDescriptor" }
  ],

  "$defs": {
    "ViewDescriptor": {
      "type": "object",
      "description": "A view descriptor identifying a root template URI and its dynamic slot assignments. Slots are recursive — each slot value is a ViewDescriptor, a DescriptorReference, or an array of either.",
      "properties": {
        "template": {
          "$ref": "#/$defs/TemplateURI"
        },
        "type": {
          "type": "string",
          "minLength": 1,
          "description": "Advisory media type (RFC 6838) of the template resource, e.g. text/x-qute. The Content-Type of the fetched template is authoritative."
        },
        "integrity": {
          "type": "string",
          "minLength": 1,
          "description": "Integrity metadata for the template resource in W3C Subresource Integrity format, e.g. sha384-<base64 digest>. A mismatch is treated as a template fetch failure."
        },
        "slots": {
          "$ref": "#/$defs/Slots"
        }
      },
      "required": ["template"],
      "additionalProperties": false
    },

    "DescriptorReference": {
      "type": "object",
      "description": "A reference to a standalone view descriptor resource, valid only as a slot value. The client fetches the URL and uses the result as the slot's descriptor. The URL may be a relative reference, resolved against the same base as the containing descriptor's template URIs.",
      "properties": {
        "descriptor": {
          "type": "string",
          "format": "uri-reference",
          "minLength": 1,
          "description": "URL of the referenced view descriptor resource."
        }
      },
      "required": ["descriptor"],
      "additionalProperties": false
    },

    "TemplateURI": {
      "type": "string",
      "format": "uri-reference",
      "minLength": 1,
      "description": "A URI identifying a template. The URI is an identifier first — which source supplies the template (app bundle, page, BFF-local store, network fetch) is deployment-specific and outside the protocol. The scheme is optional: a scheme-less identifier that does not begin with '/' (e.g. example.com/templates/card) is an opaque, host-qualified identity and is NOT resolved as an RFC 3986 relative reference (spec Section 5.4); the resolving code supplies a scheme only when it fetches over the network. Whenever a template is retrieved over the network, retrieval MUST use HTTPS (loopback addresses excepted for local development)."
    },

    "Slots": {
      "type": "object",
      "description": "A map of slot names to slot values. Each key is a named insertion point in the parent template. Each value is a SlotDescriptor (an inline ViewDescriptor or a DescriptorReference) or an array of SlotDescriptors rendered in sequence.",
      "additionalProperties": {
        "$ref": "#/$defs/SlotValue"
      }
    },

    "SlotDescriptor": {
      "description": "An inline ViewDescriptor or a DescriptorReference pointing at a standalone view descriptor resource.",
      "oneOf": [
        { "$ref": "#/$defs/ViewDescriptor" },
        { "$ref": "#/$defs/DescriptorReference" }
      ]
    },

    "SlotValue": {
      "description": "A single SlotDescriptor or an ordered array of SlotDescriptors. Arrays are rendered in sequence within the slot.",
      "oneOf": [
        { "$ref": "#/$defs/SlotDescriptor" },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/SlotDescriptor"
          },
          "minItems": 1
        }
      ]
    },

    "MultiViewDescriptor": {
      "type": "object",
      "description": "Multiple named view descriptors for a single API response. Clients SHOULD use the 'default' view when no specific view is requested.",
      "properties": {
        "views": {
          "type": "object",
          "description": "A map of view names to ViewDescriptors.",
          "additionalProperties": {
            "$ref": "#/$defs/ViewDescriptor"
          },
          "minProperties": 1
        }
      },
      "required": ["views"],
      "additionalProperties": false
    }
  }
}
