@itentialopensource/adapter-utils
Advanced tools
Comparing version 4.14.0 to 4.14.1
## 4.14.1 [08-29-2019] | ||
* fix for parse field | ||
See merge request itentialopensource/adapter-utils!111 | ||
--- | ||
## 4.14.0 [08-29-2019] | ||
@@ -3,0 +11,0 @@ |
@@ -65,13 +65,2 @@ /* @copyright Itential, LLC 2018-9 */ | ||
if (type === 'string') { | ||
// if the string is parseable JSON | ||
if (dataSchema && dataSchema.parse) { | ||
try { | ||
return JSON.parse(object[0]); | ||
} catch (ex) { | ||
log.warn(`${origin}: Could not parse data in field`); | ||
} | ||
} | ||
} | ||
return object[0]; | ||
@@ -85,13 +74,2 @@ } | ||
if (type === 'string') { | ||
// if the string is parseable JSON | ||
if (dataSchema && dataSchema.parse) { | ||
try { | ||
return JSON.parse(object); | ||
} catch (ex) { | ||
log.warn(`${origin}: Could not parse data in field`); | ||
} | ||
} | ||
} | ||
return object; | ||
@@ -291,2 +269,41 @@ } | ||
/** | ||
* @summary Checks to see if any fields in the schema need to be parsed | ||
* | ||
* @function parseFields | ||
* @param {Object} retObject - the object to extract the data from | ||
* @param {Array} dataSchema - the schema of data to extract | ||
* | ||
* @return {Object} the return object with parsed data | ||
*/ | ||
function parseFields(retObject, dataSchema) { | ||
const origin = `${id}-translatorUtil-parseFields`; | ||
log.trace(origin); | ||
const myReturn = retObject; | ||
const schemaKeys = Object.keys(dataSchema); | ||
// loop through all of the properties in the schema | ||
for (let k = 0; k < schemaKeys.length; k += 1) { | ||
const field = dataSchema[schemaKeys[k]]; | ||
// if object or array need rercursion | ||
if (field.type === 'object' && field.properties && myReturn && myReturn[schemaKeys[k]]) { | ||
myReturn[schemaKeys[k]] = parseFields(myReturn[schemaKeys[k]], field.properties); | ||
} | ||
if (field.type === 'array' && field.items && myReturn && myReturn[schemaKeys[k]]) { | ||
myReturn[schemaKeys[k]] = parseFields(myReturn[schemaKeys[k]], field.items); | ||
} | ||
// if string and we need to parse this field | ||
if (field.type === 'string' && field.parse && myReturn && myReturn[schemaKeys[k]]) { | ||
try { | ||
myReturn[schemaKeys[k]] = JSON.parse(myReturn[schemaKeys[k]]); | ||
} catch (ex) { | ||
log.warn(`${origin}: Could not parse data in field`); | ||
} | ||
} | ||
} | ||
return myReturn; | ||
} | ||
/** | ||
* INTERNAL FUNCTION | ||
@@ -311,3 +328,3 @@ * | ||
// add any defaults to the data | ||
const combinedEntity = propUtilInst.mergeProperties(returnObj, propUtilInst.setDefaults(entitySchema)); | ||
let combinedEntity = propUtilInst.mergeProperties(returnObj, propUtilInst.setDefaults(entitySchema)); | ||
@@ -338,2 +355,12 @@ // validate the entity against the schema | ||
// see if we need to parse fields | ||
// only if translating | ||
if (entitySchema.translate) { | ||
// must have properties | ||
if (entitySchema.properties) { | ||
// get the schema keys | ||
combinedEntity = parseFields(combinedEntity, entitySchema.properties); | ||
} | ||
} | ||
// return the resulting IAP entity | ||
@@ -786,3 +813,3 @@ return combinedEntity; | ||
* @param {String} type - the internal error type (optional). | ||
* @param {String} variables - the variables to put into the error message (optional). | ||
* @param {Array} variables - the variables to put into the error message (optional). | ||
* @param {Integer} sysCode - the error code from the other system (optional). | ||
@@ -789,0 +816,0 @@ * @param {Object} sysRes - the raw response from the other system (optional). |
{ | ||
"name": "@itentialopensource/adapter-utils", | ||
"version": "4.14.0", | ||
"version": "4.14.1", | ||
"description": "Itential Adapter Utility Libraries", | ||
@@ -5,0 +5,0 @@ "scripts": { |
336728
22
7420