From d1e86f59ab4f6e05faaa65b502ca4b188223514e Mon Sep 17 00:00:00 2001 From: Salakar Date: Mon, 5 Mar 2018 00:27:49 +0000 Subject: [PATCH] [js][analytics] logEvent now validates argument types (fixes #846) --- lib/modules/analytics/index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/modules/analytics/index.js b/lib/modules/analytics/index.js index 94588b93..3cba2d47 100644 --- a/lib/modules/analytics/index.js +++ b/lib/modules/analytics/index.js @@ -4,6 +4,7 @@ */ import ModuleBase from '../../utils/ModuleBase'; import { getNativeModule } from '../../utils/native'; +import { isString, isObject } from '../../utils'; import type App from '../core/app'; @@ -44,23 +45,37 @@ export default class Analytics extends ModuleBase { * @return {Promise} */ logEvent(name: string, params: Object = {}): void { + if (!isString(name)) { + throw new Error( + `analytics.logEvent(): First argument 'name' is required and must be a string value.` + ); + } + + if (typeof params !== 'undefined' && !isObject(params)) { + throw new Error( + `analytics.logEvent(): Second optional argument 'params' must be an object if provided.` + ); + } + // check name is not a reserved event name if (ReservedEventNames.includes(name)) { throw new Error( - `event name '${name}' is a reserved event name and can not be used.` + `analytics.logEvent(): event name '${name}' is a reserved event name and can not be used.` ); } // name format validation if (!AlphaNumericUnderscore.test(name)) { throw new Error( - `Event name '${name}' is invalid. Names should contain 1 to 32 alphanumeric characters or underscores.` + `analytics.logEvent(): Event name '${name}' is invalid. Names should contain 1 to 32 alphanumeric characters or underscores.` ); } // maximum number of allowed params check if (params && Object.keys(params).length > 25) - throw new Error('Maximum number of parameters exceeded (25).'); + throw new Error( + 'analytics.logEvent(): Maximum number of parameters exceeded (25).' + ); // Parameter names can be up to 24 characters long and must start with an alphabetic character // and contain only alphanumeric characters and underscores. Only String, long and double param