diff --git a/src/event/spec.ts b/src/event/spec.ts index ec0bf07..1d308bb 100644 --- a/src/event/spec.ts +++ b/src/event/spec.ts @@ -28,14 +28,21 @@ export function validateCloudEvent(event: CloudEventV03 | CloudEventV1): boolean if (!isValidAgainstSchemaV1(event)) { throw new ValidationError("invalid payload", isValidAgainstSchemaV1.errors); } - return true; } else if (event.specversion === Version.V03) { if (!isValidAgainstSchemaV03(event)) { throw new ValidationError("invalid payload", isValidAgainstSchemaV03.errors); } - return checkDataContentEncoding(event); + checkDataContentEncoding(event); + } else { + return false; } - return false; + // attribute names must all be lowercase + for (const key in event) { + if (key !== key.toLowerCase()) { + throw new ValidationError(`invalid attribute name: ${key}`); + } + } + return true; } function checkDataContentEncoding(event: CloudEventV03): boolean { diff --git a/src/event/validation.ts b/src/event/validation.ts index ca43534..6279cce 100644 --- a/src/event/validation.ts +++ b/src/event/validation.ts @@ -19,7 +19,7 @@ export class ValidationError extends TypeError { // @ts-ignore errors?.reduce( (accum: string, err: Record) => - (accum as string).concat(` + accum.concat(` ${err instanceof Object ? JSON.stringify(err) : err}`), message, ) diff --git a/src/message/http/index.ts b/src/message/http/index.ts index ad6356f..029965e 100644 --- a/src/message/http/index.ts +++ b/src/message/http/index.ts @@ -151,7 +151,7 @@ function getVersion(mode: Mode, headers: Headers, body: string | Record { expect(HTTP.isEvent(message)).to.be.true; }); + it("Respects extension attribute casing (even if against spec)", () => { + // Now create a message that is an event + const message = { + body: `{ "greeting": "hello" }`, + headers: { + [CONSTANTS.CE_HEADERS.ID]: "1234", + [CONSTANTS.CE_HEADERS.SOURCE]: "test", + [CONSTANTS.CE_HEADERS.TYPE]: "test.event", + [CONSTANTS.CE_HEADERS.SPEC_VERSION]: Version.V1, + "ce-LUNCH": "tacos", + }, + }; + expect(HTTP.isEvent(message)).to.be.true; + const event: CloudEvent = HTTP.toEvent(message); + expect(event.LUNCH).to.equal("tacos"); + expect(function () { + event.validate(); + }).to.throw("invalid attribute name: LUNCH"); + }); + it("Can detect CloudEvent binary Messages with weird versions", () => { // Now create a message that is an event const message = {