Add the responsability of parse the data attribute
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
fb92f710ea
commit
3f13000e35
|
@ -3,6 +3,11 @@ const Commons = require("./commons.js");
|
|||
const Cloudevent = require("../../cloudevent.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 = [];
|
||||
allowed_content_types.push(Constants.MIME_JSON);
|
||||
|
||||
|
@ -47,8 +52,11 @@ function validate_args(payload, attributes) {
|
|||
throw {message: "attributes is null or undefined"};
|
||||
}
|
||||
|
||||
if((typeof payload) !== "object"){
|
||||
throw {message: "payload must be an object", erros: [typeof payload]};
|
||||
if((typeof payload) !== "object" && (typeof payload) !== "string"){
|
||||
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
|
||||
if(!allowed_content_types
|
||||
.includes(sanity_headers[Constants.HEADER_CONTENT_TYPE])){
|
||||
throw {message: "invalid content type",
|
||||
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]};
|
||||
throw {
|
||||
message: "invalid content type",
|
||||
errors: [sanity_headers[Constants.HEADER_CONTENT_TYPE]]
|
||||
};
|
||||
}
|
||||
|
||||
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"){
|
||||
throw {message: "invalid spec version",
|
||||
errors:
|
||||
[sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]};
|
||||
throw {
|
||||
message: "invalid spec version",
|
||||
errors: [sanity_headers[Constants.BINARY_HEADERS_02.SPEC_VERSION]]
|
||||
};
|
||||
}
|
||||
|
||||
// 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
|
||||
cloudevent.data(payload);
|
||||
cloudevent.data(parsedPayload);
|
||||
|
||||
// Checks the event spec
|
||||
cloudevent.format();
|
||||
|
|
Loading…
Reference in New Issue