From 8915048791d7fb4727ab59cb64e42f77b1afe331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Mon, 10 Jun 2019 21:20:04 -0300 Subject: [PATCH] Spec as parser parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/formats/json/parser.js | 12 ++++++------ test/formats/json/parser_test.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/formats/json/parser.js b/lib/formats/json/parser.js index b914b1a..115b6ae 100644 --- a/lib/formats/json/parser.js +++ b/lib/formats/json/parser.js @@ -2,8 +2,8 @@ var Spec02 = require("../../specs/spec_0_2.js"); const spec02 = new Spec02(); -function JSONParser() { - +function JSONParser(_spec) { + this.spec = (_spec) ? _spec : new Spec02(); } /** @@ -35,10 +35,10 @@ function validate_and_parse_as_json(payload) { /* * Level 1 of validation: is that follow a spec? */ -function validate_spec(payload) { +function validate_spec(payload, spec) { - // is that follow spec 0.2? - spec02.check(payload); + // is that follow the spec? + spec.check(payload); return payload; } @@ -49,7 +49,7 @@ JSONParser.prototype.parse = function(payload) { var valid0 = validate_and_parse_as_json(payload); // Level 1 of validation: is that follow a spec? - var valid1 = validate_spec(valid0); + var valid1 = validate_spec(valid0, this.spec); return valid1; } diff --git a/test/formats/json/parser_test.js b/test/formats/json/parser_test.js index 8619988..41b6e75 100644 --- a/test/formats/json/parser_test.js +++ b/test/formats/json/parser_test.js @@ -97,7 +97,7 @@ describe("JSON Event Format Parser", () => { .data(data) .toString(); - var parser = new Parser(); + var parser = new Parser(new Cloudevent.specs["0.2"]()); // act var actual = parser.parse(payload);