]> git.giacomolli.uk - onehouse/blobdiff - protocol/wire_envelope.go
Publish typed values with bounded repeat timing
[onehouse] / protocol / wire_envelope.go
index 5e8b778e4613508b4945f4189dc8c9eaf6e3db8f..c3723468d416a54ab11d1dc504c766496d1cc293 100644 (file)
@@ -77,19 +77,32 @@ func validateEnvelope(envelope wireEnvelope) error {
                return fmt.Errorf("unsupported schema %d", envelope.Schema)
        }
 
                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")
        }
 
                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")
        }
 
                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")
        }
 
                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 {
                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
 }
 
        return nil
 }