Handle invalid json strings

This commit is contained in:
Dennis Eichhorn 2018-12-02 10:35:55 +01:00
parent a7d6bf4ec8
commit 4e400bf372
3 changed files with 45 additions and 0 deletions

View File

@ -64,6 +64,13 @@
*/
jsOMS.UI.ActionManager.prototype.bindElement = function (e)
{
if (!jsOMS.isValidJson(e.getAttribute('data-action'))) {
jsOMS.Log.Logger.instance.error('Invalid json string: \'' + e.getAttribute('data-action') + '\'');
return;
}
// todo: validate json, if invalid log error
const listeners = JSON.parse(e.getAttribute('data-action')),
listenerLength = listeners.length,
self = this;

View File

@ -63,6 +63,25 @@
return str.replace(new RegExp("^[" + char + "]*"), '');
};
/**
* Validate json string
*
* @param {string} jsonString String to validate
*
* @function
*
* @since 1.0.0
*/
jsOMS.isValidJson = function (jsonString)
{
try {
JSON.parse(jsonString);
} catch (e) {
return false;
}
return true;
};
/**
* Count string in string
*

View File

@ -390,6 +390,25 @@
}
};
/**
* Validate json string
*
* @param {string} jsonString String to validate
*
* @function
*
* @since 1.0.0
*/
jsOMS.isValidJson = function (jsonString)
{
try {
JSON.parse(jsonString);
} catch (e) {
return false;
}
return true;
};
/**
* Merging two arrays recursively
*