validateEvent()

Validates an event envelope and returns a branded validated value on success.

Signature

function validateEvent<T>(
  event: EventEnvelope<T>,
  options?: { maxClockDriftMs?: bigint; now?: () => bigint; includeWarnings?: boolean },
): ValidationResult<ValidatedEventEnvelope<T>>
function validateEvent(
  event: unknown,
  options?: { maxClockDriftMs?: bigint; now?: () => bigint; includeWarnings?: boolean },
): ValidationResult<ValidatedEventEnvelope<unknown>>
function validateEvent<T>(
  event: unknown,
  options?: { maxClockDriftMs?: bigint; now?: () => bigint; includeWarnings?: boolean },
): ValidationResult<ValidatedEventEnvelope<T>>

Use this when you need to validate event-envelope input directly before ordering or other downstream processing.

Usage

import { validateEvent } from "causal-order"

const validation = validateEvent(event, {
  includeWarnings: true,
})

if (!validation.valid) {
  console.error(validation.errors)
} else {
  console.log(validation.value)
  console.log(validation.warnings)
}

Details

  • valid: true returns a branded value plus any warnings.
  • valid: false returns structured errors and warnings.
  • event.id must be a non-empty string.
  • event.nodeId must be a non-empty string.
  • event.clock must pass HLC validation.
  • event.sequence, when present, must be a non-negative bigint.