Add the responsability of parse the data attribute

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-21 10:56:30 -03:00
parent fb92f710ea
commit 3f13000e35
1 changed files with 24 additions and 8 deletions

View File

@ -3,6 +3,11 @@ const Commons = require("./commons.js");
const Cloudevent = require("../../cloudevent.js"); const Cloudevent = require("../../cloudevent.js");
const Spec02 = require("../../specs/spec_0_2.js"); const Spec02 = require("../../specs/spec_0_2.js");
const JSONParser = require("../../formats/json/parser.js");
const parser_by_type = {};
parser_by_type[Constants.MIME_JSON] = new JSONParser();
const allowed_content_types = []; const allowed_content_types = [];
allowed_content_types.push(Constants.MIME_JSON); allowed_content_types.push(Constants.MIME_JSON);
@ -47,8 +52,11 @@ function validate_args(payload, attributes) {
throw {message: "attributes is null or undefined"}; throw {message: "attributes is null or undefined"};
} }
if((typeof payload) !== "object"){ if((typeof payload) !== "object" && (typeof payload) !== "string"){
throw {message: "payload must be an object", erros: [typeof payload]}; throw {
message: "payload must be an object or a string",
errors: [typeof payload]
};
} }
} }
@ -66,8 +74,10 @@ Receiver.prototype.check = function(payload, headers) {
// Validation Level 1 // Validation Level 1
if(!allowed_content_types if(!allowed_content_types
.includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){ .includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){
throw {message: "invalid content type", throw {
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]}; message: "invalid content type",
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]
};
} }
for(i in required_headers){ for(i in required_headers){
@ -77,9 +87,10 @@ Receiver.prototype.check = function(payload, headers) {
} }
if(sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){ if(sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION] !== "0.2"){
throw {message: "invalid spec version", throw {
errors: message: "invalid spec version",
[sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]}; errors: [sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]
};
} }
// No erros! Its contains the minimum required attributes // No erros! Its contains the minimum required attributes
@ -103,8 +114,13 @@ Receiver.prototype.parse = function(payload, headers) {
} }
} }
// Parses the payload
var parsedPayload =
parser_by_type[sanity_headers[Constants.HEADER_CONTENT_TYPE]]
.parse(payload);
// Sets the data // Sets the data
cloudevent.data(payload); cloudevent.data(parsedPayload);
// Checks the event spec // Checks the event spec
cloudevent.format(); cloudevent.format();