Why is event schema validation essential for maintaining reliable contract communication between event publishers and subscribers?
Answer
Basics: A platform event is defined by its fields, types, lengths and required flags. Subscribers rely on those fields existing and meaning the same thing. If a publisher changes a field type or drops a field, consumers can fail silently.
How it works:
- Define the platform event in Setup with typed fields, and mark must-have fields as required.
- On publish, Salesforce rejects events with missing required fields or wrong types, and the
SaveResultreports the error. - External clients get the Avro schema from Pub/Sub API
GetSchema. Every schema revision has a schema ID. - Subscribers decode with the schema that matches the event's ID.
Advanced / deep dive: Platform events do not support validation rules or before triggers, so business validation (such as an allowed status value) belongs in the publishing code, a shared publisher class, or a gateway. Compatibility rules for changes:
- Adding an optional field is usually safe.
- Removing, renaming or retyping a field is breaking.
- For breaking changes, create a new version of the event (for example
Order_Event_v2__e) and run both until subscribers migrate.
Include standard helpers in the payload design: an event ID or the built-in EventUuid for de-duplication, a version number, a business key, and a correlation ID. Keep payloads small and put identifiers rather than whole records in the event, to reduce schema churn. Publish a schema catalog and contract tests so teams see changes before release.
Code / config example:
public Boolean {
e.Order_Id__c !=
&& e.Version__c !=
&& allowed.contains(e.Status__c);
}