Skip to content

Commit 81f9f2f

Browse files
committed
squash: valid types for ext values
Signed-off-by: Lucas Holmquist <[email protected]>
1 parent 45850e3 commit 81f9f2f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/event/cloudevent.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { v4 as uuidv4 } from "uuid";
22

33
import { CloudEventV1, validateV1, CloudEventV1Attributes, CloudEventV1OptionalAttributes } from "./v1";
44
import { CloudEventV03, validateV03, CloudEventV03Attributes, CloudEventV03OptionalAttributes } from "./v03";
5-
import { ValidationError, isBinary, asBase64 } from "./validation";
5+
import { ValidationError, isBinary, asBase64, isValidType } from "./validation";
66
import CONSTANTS from "../constants";
77
import { isString } from "util";
88

@@ -101,6 +101,13 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 {
101101
if (!key.match(/^[a-z0-9]{1,20}$/)) {
102102
throw new ValidationError("invalid extension name");
103103
}
104+
105+
// Value should be spec complient
106+
// https://github.com/cloudevents/spec/blob/master/spec.md#type-system
107+
if (!isValidType(value)) {
108+
throw new ValidationError("invalid extension value");
109+
}
110+
104111
this[key] = value;
105112
}
106113

test/integration/spec_1_tests.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ describe("CloudEvents Spec v1.0", () => {
8787
expect(cloudevent.cloneWith({ extdate: myDate }).validate()).to.equal(true);
8888
});
8989

90-
// even though the spec doesn't allow object types for
91-
// extensions, it could be JSON. And before a JS CE
92-
// is transmitted across the wire, this value will be
93-
// converted to JSON
94-
it("should be ok when the type is an object", () => {
95-
expect(cloudevent.cloneWith({ objectextension: { some: "object" } }).validate()).to.equal(true);
90+
it("should fail when the type is an object", () => {
91+
expect(() => {
92+
cloudevent.cloneWith({ objectextension: { some: "object" } });
93+
}).to.throw(ValidationError, "invalid extension value");
94+
});
95+
96+
it("should be ok when the type is an string converted from an object", () => {
97+
expect(cloudevent.cloneWith({ objectextension: JSON.stringify({ some: "object" }) }).validate()).to.equal(true);
9698
});
9799
});
98100

0 commit comments

Comments
 (0)