Validate ensures the persisted network message is complete and internally consistent.
()
| 1420 | |
| 1421 | // Validate ensures the persisted network message is complete and internally consistent. |
| 1422 | func (e NetworkConversationMessage) Validate() error { |
| 1423 | if err := requireField(e.MessageID, "network message id"); err != nil { |
| 1424 | return err |
| 1425 | } |
| 1426 | if err := (NetworkChannelRef{WorkspaceID: e.WorkspaceID, Channel: e.Channel}).Validate(); err != nil { |
| 1427 | return err |
| 1428 | } |
| 1429 | if err := requireField(e.Direction, "network message direction"); err != nil { |
| 1430 | return err |
| 1431 | } |
| 1432 | direction := strings.TrimSpace(e.Direction) |
| 1433 | if direction != e.Direction { |
| 1434 | return fmt.Errorf("store: unsupported network message direction %q", e.Direction) |
| 1435 | } |
| 1436 | switch direction { |
| 1437 | case typesSentKey, "received": |
| 1438 | default: |
| 1439 | return fmt.Errorf("store: unsupported network message direction %q", e.Direction) |
| 1440 | } |
| 1441 | if err := requireField(e.PeerFrom, "network message peer_from"); err != nil { |
| 1442 | return err |
| 1443 | } |
| 1444 | if err := requireField(e.Kind, "network message kind"); err != nil { |
| 1445 | return err |
| 1446 | } |
| 1447 | if err := validateNetworkMessageKind(e.Kind); err != nil { |
| 1448 | return err |
| 1449 | } |
| 1450 | if err := e.validateConversationFields(); err != nil { |
| 1451 | return err |
| 1452 | } |
| 1453 | if _, err := NormalizeNetworkPeerIDs(e.Mentions, "network message mentions"); err != nil { |
| 1454 | return err |
| 1455 | } |
| 1456 | if len(e.Body) == 0 { |
| 1457 | return fmt.Errorf("store: network message body is required") |
| 1458 | } |
| 1459 | if !json.Valid(e.Body) { |
| 1460 | return fmt.Errorf("store: network message body must be valid JSON") |
| 1461 | } |
| 1462 | if networkRawJSONContainsClaimToken(e.Body) { |
| 1463 | return fmt.Errorf("store: network message body contains raw claim_token material") |
| 1464 | } |
| 1465 | if err := validateNetworkMessageExtJSON(e.ExtJSON); err != nil { |
| 1466 | return err |
| 1467 | } |
| 1468 | if networkConversationMessageContainsRawClaimToken(e) { |
| 1469 | return fmt.Errorf("store: network message entry contains raw claim_token material") |
| 1470 | } |
| 1471 | return nil |
| 1472 | } |
| 1473 | |
| 1474 | func validateNetworkMessageExtJSON(raw json.RawMessage) error { |
| 1475 | trimmed := strings.TrimSpace(string(raw)) |
no test coverage detected