From 1905266d1f36d897a1973b56df5a623a18f8176b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Thu, 31 Oct 2019 17:50:54 -0300 Subject: [PATCH] Validating extension types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/specs/spec_1.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/specs/spec_1.js b/lib/specs/spec_1.js index 6944fea..3b4207e 100644 --- a/lib/specs/spec_1.js +++ b/lib/specs/spec_1.js @@ -1,11 +1,20 @@ const uuid = require("uuid/v4"); const empty = require("is-empty"); const Ajv = require("ajv"); +const URI = require("uri-js"); const { - asData + asData, + isBoolean, + isInteger, + isString, + isDate, + isBinary } = require("../utils/fun.js"); +const isValidType = (v) => + (isBoolean(v) || isInteger(v) || isString(v) || isDate(v) || isBinary(v)); + const RESERVED_ATTRIBUTES = { type: "type", specversion: "specversion", @@ -165,7 +174,11 @@ Spec1.prototype.getData = function() { Spec1.prototype.addExtension = function(key, value){ if(!RESERVED_ATTRIBUTES.hasOwnProperty(key)){ - this.payload[key] = value; + if(isValidType(value)){ + this.payload[key] = value; + } else { + throw {message: "Invalid type of extension value"}; + } } else { throw {message: "Reserved attribute name: '" + key + "'"}; }