{
  "$id": "https://apps.ediphai.com/v1/schemas/Envelope.json",
  "title": "Envelope",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "record_type": {
      "type": "string",
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)*$"
    },
    "record_type_version": {
      "type": "integer",
      "exclusiveMinimum": 0,
      "maximum": 9007199254740991
    },
    "record_id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9._:-]{1,128}$",
      "description": "Unique within (source, record_type)"
    },
    "revision": {
      "type": "integer",
      "exclusiveMinimum": 0,
      "maximum": 9007199254740991,
      "description": "Monotonic per record; a millisecond timestamp is allowed"
    },
    "occurred_at": {
      "type": "string",
      "pattern": "^(\\d{4})(?:-(\\d{2})(?:-(\\d{2})(?:T(\\d{2}):(\\d{2}):(\\d{2})(?:\\.\\d{1,9})?(?:Z|[+-](\\d{2}):(\\d{2})))?)?)?$",
      "description": "An RFC 3339 timestamp with an offset (2026-10-12T19:04:11Z), a day (2003-05-04), a month (2003-05) or a year (2003)"
    },
    "subjects": {
      "minItems": 1,
      "maxItems": 50,
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "role": {
            "type": "string",
            "pattern": "^[a-z][a-z0-9_]{0,63}$",
            "description": "Identifier for every section, item, role, level, qualifier and declarer: a lowercase letter, then lowercase letters, digits or underscores, at most 64 characters"
          },
          "external_id": {
            "type": "string",
            "pattern": "^[A-Za-z0-9._:-]{1,128}$",
            "description": "One namespace per source across all its record types"
          },
          "claims": {
            "type": "object",
            "properties": {
              "source_refs": {
                "maxItems": 10,
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "system": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 64,
                      "description": "A referenceable people source in the church's registry, otherwise 422"
                    },
                    "id": {
                      "type": "string",
                      "minLength": 1,
                      "maxLength": 128
                    },
                    "basis": {
                      "type": "string",
                      "enum": [
                        "staff_assigned",
                        "staff_entered",
                        "lookup_confirmed",
                        "self_reported"
                      ]
                    }
                  },
                  "required": [
                    "system",
                    "id",
                    "basis"
                  ],
                  "additionalProperties": false
                }
              },
              "first_name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 200
              },
              "last_name": {
                "type": "string",
                "minLength": 1,
                "maxLength": 200
              },
              "date_of_birth": {
                "type": "string",
                "pattern": "^(\\d{4})(?:-(\\d{2})(?:-(\\d{2})(?:T(\\d{2}):(\\d{2}):(\\d{2})(?:\\.\\d{1,9})?(?:Z|[+-](\\d{2}):(\\d{2})))?)?)?$",
                "description": "A full date (1986-03-14)"
              },
              "birth_year": {
                "description": "For sources that know only the year. Never send it with date_of_birth",
                "type": "integer",
                "minimum": 1000,
                "maximum": 9999
              },
              "email": {
                "type": "string",
                "minLength": 3,
                "maxLength": 320
              },
              "phone_e164": {
                "type": "string",
                "format": "e164",
                "pattern": "^\\+[1-9]\\d{6,14}$"
              },
              "phone_verified": {
                "type": "boolean"
              }
            },
            "additionalProperties": false,
            "not": {
              "required": [
                "date_of_birth",
                "birth_year"
              ]
            }
          }
        },
        "required": [
          "role",
          "external_id"
        ],
        "additionalProperties": false
      }
    },
    "consent": {
      "description": "Optional provenance, stored as sent, never interpreted",
      "type": "object",
      "properties": {
        "captured_at": {
          "type": "string",
          "pattern": "^(\\d{4})(?:-(\\d{2})(?:-(\\d{2})(?:T(\\d{2}):(\\d{2}):(\\d{2})(?:\\.\\d{1,9})?(?:Z|[+-](\\d{2}):(\\d{2})))?)?)?$",
          "description": "An RFC 3339 timestamp with an offset (2026-10-12T19:04:11Z), a day (2003-05-04), a month (2003-05) or a year (2003)"
        },
        "policy_ref": {
          "type": "string",
          "maxLength": 200
        }
      },
      "additionalProperties": {}
    },
    "body": {
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {},
      "description": "Validated against record_type@record_type_version and its definition"
    },
    "source_detail": {
      "description": "At most 32 KB; stored, never modelled",
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {}
    }
  },
  "required": [
    "record_type",
    "record_type_version",
    "record_id",
    "revision",
    "occurred_at",
    "subjects",
    "body"
  ],
  "additionalProperties": false
}
