Description
With recent API changes, a CloudEvent
is nicely represented as a plain old JS object. For example, for most purposes, this is actually a valid CE.
const ce = {
source: '/',
type: 'example',
specversion: 'v1.0',
id: 'asd0aan3412juv'
}
However, because we want to have spec compliance checks, and provide default values (e.g. a UUID or timestamp), we have a constructor and a class. The example above can be written as:
const ce = new CloudEvent({
source: '/',
type: 'example',
});
Here, code in the constructor is providing default values for id
and specversion
.
However this introduces some tricky edge cases. For example, extensions. According to the spec, extensions must exist as siblings of the CE properties. And JS being as loosey goosey as it is, this means a user can do some things outside of the spec boundaries. For example
const ce = new CloudEvent({ source: '/', type: 'example' });
ce[extension-1] = // <- note invalid extension name
{
foo: 'bar' // <-- note invalid extension value
};
A solution to this is to
- At the bottom of the
CloudEvent
constructor, callthis.validate()
- Make the
CloudEvent
object read only just after validation withObject.freeze(this);
- Provide a "builder" of some kind that enables cloning an event with new properties/extensions, e.g.
myEvent.cloneWith( { extension: 'value' });
(this could be a function onCloudEvent
or a class unto itself.
Note that both #228 and #229 should be implemented and added to the validation step as a part of this.
Related: #29