diff --git a/README.md b/README.md index 63dbb72..afc99c3 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,11 @@ var config = { url : "https://myserver.com" }; +/* + * To use HTTP Binary: + * Cloudevent.bindings["http-binary0.2"](config); + */ + // The binding instance var binding = Cloudevent.bindings["http-structured0.1"](config); diff --git a/ext/spec_0_2.json b/ext/spec_0_2.json index aa892a1..2848dc0 100644 --- a/ext/spec_0_2.json +++ b/ext/spec_0_2.json @@ -32,6 +32,9 @@ "time": { "$ref": "#/definitions/time" }, + "schemaurl": { + "$ref": "#/definitions/schemaurl" + }, "type": { "$ref": "#/definitions/type" }, @@ -58,6 +61,10 @@ "format": "date-time", "type": "string" }, + "schemaurl": { + "type": "string", + "format": "uri" + }, "type": { "type": "string", "minLength": 1 diff --git a/lib/specs/spec_0_2.js b/lib/specs/spec_0_2.js index 7dc29c8..ac105b7 100644 --- a/lib/specs/spec_0_2.js +++ b/lib/specs/spec_0_2.js @@ -16,8 +16,9 @@ const reserved = { const schema = require("../../ext/spec_0_2.json"); -// Default options -const ajv = new Ajv(); +const ajv = new Ajv({ + extendRefs: true // validate all keywords in the schemas with $ref (the default behaviour in versions before 5.0.0) +}); const validate = ajv.compile(schema); @@ -36,7 +37,7 @@ Spec02.prototype.check = function(){ var valid = validate(this.payload); if(!valid) { - throw {message: "invalid payload"}; + throw {message: "invalid payload", errors: validate.errors}; } }; diff --git a/test/cloudevent_spec_0_2.js b/test/cloudevent_spec_0_2.js index bdfbb16..212cd04 100644 --- a/test/cloudevent_spec_0_2.js +++ b/test/cloudevent_spec_0_2.js @@ -40,6 +40,14 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => { expect(cloudevent.format()).to.have.property("specversion"); }); + it("should throw an error when mandatory attribute is absent", () => { + delete cloudevent.spec.payload.source; + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("invalid payload"); + cloudevent.spec.payload.source = source; + }); + it("requires 'source'", () => { expect(cloudevent.format()).to.have.property("source"); }); @@ -60,6 +68,14 @@ describe("CloudEvents Spec 0.2 - JavaScript SDK", () => { expect(cloudevent.format()).to.have.property("schemaurl"); }); + it("should throw an error when 'schemaurl' is not an URI", () => { + cloudevent.spec.payload.schemaurl = "KKKKKK"; + expect(cloudevent.format.bind(cloudevent)) + .to + .throw("invalid payload"); + cloudevent.spec.payload.schemaurl = schemaurl; + }); + it("contains 'contenttype'", () => { cloudevent.contenttype(contenttype); expect(cloudevent.format()).to.have.property("contenttype");