From c18f4caadec9533777596fe129b282888aa6307c Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 28 Jul 2020 14:50:55 -0400 Subject: [PATCH 1/2] fix: ensure that event data can be an array or number The schema incorrectly limits data values to only object and string. This is incorrect, since JSON can be an array or a single number as well. Fixes: https://github.com/cloudevents/sdk-javascript/issues/280 Signed-off-by: Lance Ball --- src/event/schemas.ts | 4 ++-- test/integration/cloud_event_test.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/event/schemas.ts b/src/event/schemas.ts index 6591e3f3..aed9f0ca 100644 --- a/src/event/schemas.ts +++ b/src/event/schemas.ts @@ -10,7 +10,7 @@ export const schemaV1 = { type: "string", }, data: { - type: ["object", "string"], + type: ["object", "string", "array", "number"], }, data_base64: { type: "string", @@ -89,7 +89,7 @@ export const schemaV03 = { type: "string", }, data: { - type: ["object", "string"], + type: ["object", "string", "array", "number"], }, event: { properties: { diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index 7de5da77..78690141 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -101,6 +101,22 @@ describe("A 1.0 CloudEvent", () => { expect(ce.data).to.deep.equal({ lunch: "tacos" }); }); + it("can be constructed with data as an Array", () => { + const ce = new CloudEvent({ + ...fixture, + data: [{ lunch: "tacos" }, { supper: "sushi" }], + }); + expect(ce.data).to.deep.equal([{ lunch: "tacos" }, { supper: "sushi" }]); + }); + + it("can be constructed with data as a number", () => { + const ce = new CloudEvent({ + ...fixture, + data: 100, + }); + expect(ce.data).to.equal(100); + }); + it("can be constructed with extensions", () => { const extensions = { extensionkey: "extension-value", From 62217f51d809a177e69e6a124895873c21ba2fb9 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 28 Jul 2020 15:08:12 -0400 Subject: [PATCH 2/2] fixup: add boolean and null as valid data types Signed-off-by: Lance Ball --- src/event/schemas.ts | 4 ++-- test/integration/cloud_event_test.ts | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/event/schemas.ts b/src/event/schemas.ts index aed9f0ca..51d21e14 100644 --- a/src/event/schemas.ts +++ b/src/event/schemas.ts @@ -10,7 +10,7 @@ export const schemaV1 = { type: "string", }, data: { - type: ["object", "string", "array", "number"], + type: ["object", "string", "array", "number", "boolean", "null"], }, data_base64: { type: "string", @@ -89,7 +89,7 @@ export const schemaV03 = { type: "string", }, data: { - type: ["object", "string", "array", "number"], + type: ["object", "string", "array", "number", "boolean", "null"], }, event: { properties: { diff --git a/test/integration/cloud_event_test.ts b/test/integration/cloud_event_test.ts index 78690141..de733c66 100644 --- a/test/integration/cloud_event_test.ts +++ b/test/integration/cloud_event_test.ts @@ -117,6 +117,22 @@ describe("A 1.0 CloudEvent", () => { expect(ce.data).to.equal(100); }); + it("can be constructed with null data", () => { + const ce = new CloudEvent({ + ...fixture, + data: null, + }); + expect(ce.data).to.equal(null); + }); + + it("can be constructed with data as a boolean", () => { + const ce = new CloudEvent({ + ...fixture, + data: true, + }); + expect(ce.data).to.be.true; + }); + it("can be constructed with extensions", () => { const extensions = { extensionkey: "extension-value",