Skip to content

Commit

Permalink
[js][analytics] logEvent now validates argument types (fixes #846)
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Mar 5, 2018
1 parent 6f0fd97 commit d1e86f5
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/modules/analytics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down

1 comment on commit d1e86f5

@sibelius
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work

Please sign in to comment.