{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:architecture-standards:schema:audit-event",
  "title": "Audit event",
  "description": "Structural contract for an audit JSON event.",
  "$comment": "Nonnormative projection of OBS-AUD-002 and OBS-AUD-003. Normative text prevails.",
  "type": "object",
  "required": [
    "timestamp",
    "telemetry.type",
    "event.id",
    "event.name",
    "event.schema_version",
    "service.name",
    "service.version",
    "deployment.environment.name",
    "actor.type",
    "action",
    "object.type",
    "result"
  ],
  "properties": {
    "timestamp": {
      "type": "string",
      "format": "date-time",
      "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]+(?:Z|[+]00:00)$"
    },
    "telemetry.type": {
      "const": "audit"
    },
    "event.id": {
      "type": "string",
      "pattern": "\\S"
    },
    "event.name": {
      "type": "string",
      "pattern": "\\S"
    },
    "event.schema_version": {
      "type": "integer",
      "minimum": 1
    },
    "service.name": {
      "type": "string",
      "pattern": "\\S"
    },
    "service.version": {
      "type": "string",
      "pattern": "\\S"
    },
    "deployment.environment.name": {
      "type": "string",
      "pattern": "\\S"
    },
    "actor.type": {
      "type": "string",
      "pattern": "\\S"
    },
    "actor.id": {
      "type": "string",
      "pattern": "\\S"
    },
    "action": {
      "type": "string",
      "pattern": "\\S"
    },
    "object.type": {
      "type": "string",
      "pattern": "\\S"
    },
    "object.id": {
      "type": "string",
      "pattern": "\\S"
    },
    "result": {
      "enum": ["success", "failure"]
    },
    "failure.reason": {
      "type": "string",
      "pattern": "\\S"
    },
    "source.address": {
      "type": "string",
      "pattern": "\\S"
    },
    "trace_id": {
      "type": "string",
      "pattern": "^[0-9a-f]{32}$",
      "not": {
        "const": "00000000000000000000000000000000"
      }
    },
    "span_id": {
      "type": "string",
      "pattern": "^[0-9a-f]{16}$",
      "not": {
        "const": "0000000000000000"
      }
    },
    "request_id": {
      "type": "string",
      "pattern": "\\S"
    },
    "correlation_id": {
      "type": "string",
      "pattern": "\\S"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "actor.type": {
            "const": "anonymous"
          }
        },
        "required": ["actor.type"]
      },
      "then": {
        "not": {
          "required": ["actor.id"]
        }
      },
      "else": {
        "required": ["actor.id"]
      }
    },
    {
      "if": {
        "properties": {
          "result": {
            "const": "failure"
          }
        },
        "required": ["result"]
      },
      "then": {
        "required": ["failure.reason"]
      },
      "else": {
        "not": {
          "required": ["failure.reason"]
        }
      }
    }
  ],
  "dependentRequired": {
    "trace_id": ["span_id"],
    "span_id": ["trace_id"]
  },
  "additionalProperties": true
}
