X-Git-Url: https://git.giacomolli.uk/onehouse/blobdiff_plain/4bba0e163f1a4dc07b34fd9e4dc403de1f9b4cb9..ed48ff78adf6b195fc729e6c58772edc3a09def2:/protocol/wire_envelope.go diff --git a/protocol/wire_envelope.go b/protocol/wire_envelope.go index 5e8b778..c372346 100644 --- a/protocol/wire_envelope.go +++ b/protocol/wire_envelope.go @@ -77,19 +77,32 @@ func validateEnvelope(envelope wireEnvelope) error { return fmt.Errorf("unsupported schema %d", envelope.Schema) } - if !ValidID(envelope.MessageID) { + err := validateMetadata(envelope.Metadata) + if err != nil { + return fmt.Errorf("validate metadata: %w", err) + } + + if !isObject(envelope.Payload) { + return fmt.Errorf("payload must be a JSON object") + } + + return nil +} + +func validateMetadata(metadata Metadata) error { + if !ValidID(metadata.MessageID) { return fmt.Errorf("message ID must be a canonical ULID") } - if envelope.CorrelationID != "" && !ValidID(envelope.CorrelationID) { + if metadata.CorrelationID != "" && !ValidID(metadata.CorrelationID) { return fmt.Errorf("correlation ID must be a canonical ULID") } - if envelope.CausationID != "" && !ValidID(envelope.CausationID) { + if metadata.CausationID != "" && !ValidID(metadata.CausationID) { return fmt.Errorf("causation ID must be a canonical ULID") } - for _, timestamp := range []*time.Time{envelope.SentAt, envelope.ExpiresAt} { + for _, timestamp := range []*time.Time{metadata.SentAt, metadata.ExpiresAt} { if timestamp != nil { _, offset := timestamp.Zone() if offset != 0 { @@ -98,10 +111,6 @@ func validateEnvelope(envelope wireEnvelope) error { } } - if !isObject(envelope.Payload) { - return fmt.Errorf("payload must be a JSON object") - } - return nil }