{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.schemafirst.org/schemas/utm-standard.schema.json",
  "title": "SchemaFirst UTM Standard",
  "description": "Structure of the SchemaFirst UTM Standard document.",
  "type": "object",
  "required": ["$id", "name", "id", "version", "status", "repository", "inputs", "parameters", "minimum_conforming_set", "campaign_naming", "url_assembly", "generation", "provenance_classes", "organization_profile", "versioning", "references"],
  "$defs": {
    "requirement": {
      "enum": ["required", "recommended", "recommended_where_applicable", "optional"]
    },
    "parameterName": {
      "enum": ["utm_source", "utm_medium", "utm_campaign", "utm_id", "utm_source_platform", "utm_content", "utm_term", "utm_creative_format", "utm_marketing_tactic"]
    },
    "parameter": {
      "type": "object",
      "required": ["name", "requirement", "value_source", "derivation", "semantics", "default_basis", "overridable", "google_meaning"],
      "additionalProperties": false,
      "properties": {
        "name": { "$ref": "#/$defs/parameterName" },
        "requirement": { "$ref": "#/$defs/requirement" },
        "value_source": { "type": "string" },
        "derivation": { "type": "string" },
        "semantics": { "const": "google_defined" },
        "default_basis": { "enum": ["google_defined", "schemafirst_default"] },
        "overridable": { "enum": [false, "requirement", "source_records_only"] },
        "google_meaning": { "type": "string" }
      }
    }
  },
  "properties": {
    "$schema": { "type": "string", "format": "uri" },
    "$id": { "type": "string", "format": "uri" },
    "name": { "type": "string" },
    "id": { "const": "utm" },
    "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+$" },
    "status": { "enum": ["draft", "review", "stable", "deprecated"] },
    "updated_at": { "type": "string", "format": "date" },
    "repository": { "type": "string", "format": "uri" },
    "license": { "type": "string" },
    "summary": { "type": "string" },
    "principle": { "type": "string" },
    "source_registry": {
      "type": "object",
      "required": ["index", "record_url_template"],
      "properties": {
        "index": { "type": "string", "format": "uri" },
        "record_url_template": { "type": "string" },
        "versioned_independently": { "type": "boolean" }
      }
    },
    "inputs": {
      "type": "object",
      "required": ["required"],
      "properties": {
        "required": { "type": "array", "items": { "type": "string" } },
        "optional": { "type": "array", "items": { "type": "string" } },
        "definitions": { "type": "object", "additionalProperties": { "type": "string" } }
      }
    },
    "parameters": {
      "type": "array",
      "items": { "$ref": "#/$defs/parameter" },
      "minItems": 3
    },
    "minimum_conforming_set": {
      "type": "array",
      "items": { "$ref": "#/$defs/parameterName" },
      "contains": { "const": "utm_campaign" }
    },
    "campaign_naming": {
      "type": "object",
      "required": ["rule", "precedence", "slug_algorithm", "constraints"],
      "properties": {
        "rule": { "type": "string" },
        "precedence": { "type": "array", "items": { "type": "string" } },
        "slug_algorithm": { "type": "array", "items": { "type": "string" } },
        "constraints": {
          "type": "object",
          "required": ["pattern"],
          "properties": {
            "pattern": { "type": "string", "format": "regex" }
          }
        },
        "must_not_add_unless_part_of_identity": { "type": "array", "items": { "type": "string" } },
        "examples": { "type": "array" }
      }
    },
    "value_format": { "type": "object" },
    "url_assembly": {
      "type": "object",
      "required": ["parameter_order", "rules"],
      "properties": {
        "parameter_order": { "type": "array", "items": { "$ref": "#/$defs/parameterName" } },
        "rules": { "type": "array", "items": { "type": "string" } }
      }
    },
    "generation": {
      "type": "object",
      "required": ["steps", "missing_context"],
      "properties": {
        "steps": { "type": "array", "items": { "type": "string" } },
        "missing_context": {
          "type": "object",
          "required": ["behavior", "response"],
          "properties": {
            "response": {
              "type": "object",
              "required": ["status", "missing"],
              "properties": {
                "status": { "const": "requires_context" },
                "missing": { "type": "array", "items": { "type": "string" } }
              }
            }
          }
        }
      }
    },
    "provenance_classes": {
      "type": "object",
      "required": ["google_defined", "schemafirst_default", "organization_override_permitted"],
      "additionalProperties": { "type": "string" }
    },
    "organization_profile": {
      "type": "object",
      "required": ["extends_value", "precedence", "permitted", "not_permitted"]
    },
    "versioning": { "type": "object" },
    "references": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["title", "url"],
        "properties": {
          "title": { "type": "string" },
          "url": { "type": "string", "format": "uri" },
          "covers": { "type": "string" }
        }
      }
    }
  }
}
