Skip to content

A CloudEvent should be readonly but provide a way to augment itself #233

Closed
@lance

Description

@lance

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

  1. At the bottom of the CloudEvent constructor, call this.validate()
  2. Make the CloudEvent object read only just after validation with Object.freeze(this);
  3. 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 on CloudEvent 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

Metadata

Metadata

Assignees

Labels

module/libRelated to the main source codetype/enhancementNew feature or requestversion/3.xIssues related to the 3.0 release of this library

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions