Skip to content

Commit 2bd26d6

Browse files
committed
Employ JSON Schema to validate payloads
Signed-off-by: Fabio José <[email protected]>
1 parent 6f066de commit 2bd26d6

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lib/specs/spec_0_2.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
var uuid = require("uuid/v4");
22
var empty = require("is-empty");
3+
var Ajv = require("ajv");
4+
5+
const schema = require("../../ext/spec_0_2.json");
6+
7+
// Default options
8+
const ajv = new Ajv();
9+
10+
const validate = ajv.compile(schema);
311

412
function Spec02(){
513
this.payload = {
@@ -13,22 +21,12 @@ function Spec02(){
1321
*/
1422
Spec02.prototype.check = function(){
1523

16-
if(empty(this.payload["type"])) {
17-
throw {message: "'type' is invalid"};
18-
}
24+
var valid = validate(this.payload);
1925

20-
if(empty(this.payload["specversion"])) {
21-
throw {message: "'specversion' is invalid"};
26+
if(!valid) {
27+
throw {message: "invalid payload"};
2228
}
2329

24-
if(this.payload["specversion"] !== "0.2") {
25-
throw {message: "'specversion' value is invalid: '"
26-
+ this.payload["specversion"] + "'"};
27-
}
28-
29-
if(empty(this.payload["id"])) {
30-
throw {message: "'id' is invalid"};
31-
}
3230
};
3331

3432
Spec02.prototype.type = function(_type){

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"homepage": "https://github.com/cloudevents/sdk-javascript#readme",
3232
"dependencies": {
33+
"ajv": "^6.7.0",
3334
"axios": "0.18.0",
3435
"is-empty": "1.2.0",
3536
"uri-js": "4.2.2",

test/cloudevent_spec_0_2.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
7373
cloudevent.type("");
7474
expect(cloudevent.format.bind(cloudevent))
7575
.to
76-
.throw("'type' is invalid");
76+
.throw("invalid payload");
7777
});
7878

7979
it("must be a non-empty string", () => {
@@ -95,7 +95,15 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
9595
cloudevent.spec.payload.specversion = "";
9696
expect(cloudevent.format.bind(cloudevent))
9797
.to
98-
.throw("'specversion' is invalid");
98+
.throw("invalid payload");
99+
cloudevent.spec.payload.specversion = "0.2";
100+
});
101+
102+
it("should throw an error when the value is not '0.2'", () => {
103+
cloudevent.spec.payload.specversion = "0.4";
104+
expect(cloudevent.format.bind(cloudevent))
105+
.to
106+
.throw("invalid payload");
99107
cloudevent.spec.payload.specversion = "0.2";
100108
});
101109
});
@@ -105,7 +113,7 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => {
105113
cloudevent.id("");
106114
expect(cloudevent.format.bind(cloudevent))
107115
.to
108-
.throw("'id' is invalid");
116+
.throw("invalid payload");
109117
});
110118
it("must be a non-empty string", () => {
111119
cloudevent.id("my.id-0x0090");

0 commit comments

Comments
 (0)