Skip to content

Commit b99f728

Browse files
authored
fix: ensure that event data can be an array, number, boolean or null (#281)
The schema incorrectly limits data values to only object and string. This is incorrect, since JSON can be an array, boolean, a single number or null as well. This commit modifies the schema to allow for array, boolean and null, and adds tests. Fixes: #280 Signed-off-by: Lance Ball <lball@redhat.com>
1 parent c76dda6 commit b99f728

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/event/schemas.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const schemaV1 = {
1010
type: "string",
1111
},
1212
data: {
13-
type: ["object", "string"],
13+
type: ["object", "string", "array", "number", "boolean", "null"],
1414
},
1515
data_base64: {
1616
type: "string",
@@ -89,7 +89,7 @@ export const schemaV03 = {
8989
type: "string",
9090
},
9191
data: {
92-
type: ["object", "string"],
92+
type: ["object", "string", "array", "number", "boolean", "null"],
9393
},
9494
event: {
9595
properties: {

test/integration/cloud_event_test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,38 @@ describe("A 1.0 CloudEvent", () => {
101101
expect(ce.data).to.deep.equal({ lunch: "tacos" });
102102
});
103103

104+
it("can be constructed with data as an Array", () => {
105+
const ce = new CloudEvent({
106+
...fixture,
107+
data: [{ lunch: "tacos" }, { supper: "sushi" }],
108+
});
109+
expect(ce.data).to.deep.equal([{ lunch: "tacos" }, { supper: "sushi" }]);
110+
});
111+
112+
it("can be constructed with data as a number", () => {
113+
const ce = new CloudEvent({
114+
...fixture,
115+
data: 100,
116+
});
117+
expect(ce.data).to.equal(100);
118+
});
119+
120+
it("can be constructed with null data", () => {
121+
const ce = new CloudEvent({
122+
...fixture,
123+
data: null,
124+
});
125+
expect(ce.data).to.equal(null);
126+
});
127+
128+
it("can be constructed with data as a boolean", () => {
129+
const ce = new CloudEvent({
130+
...fixture,
131+
data: true,
132+
});
133+
expect(ce.data).to.be.true;
134+
});
135+
104136
it("can be constructed with extensions", () => {
105137
const extensions = {
106138
extensionkey: "extension-value",

0 commit comments

Comments
 (0)