[js][analytics] logEvent now validates argument types (fixes #846)

This commit is contained in:
Salakar 2018-03-05 00:27:49 +00:00
parent 6f0fd97a7e
commit d1e86f59ab
1 changed files with 18 additions and 3 deletions

View File

@ -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