Validating extension types
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
		
							parent
							
								
									0d8c8a13ae
								
							
						
					
					
						commit
						1905266d1f
					
				| 
						 | 
				
			
			@ -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 + "'"};
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue