Skip to content

Commit

Permalink
event: remove js side event attributes validations since its already …
Browse files Browse the repository at this point in the history
…done on native side
  • Loading branch information
arnaud-roland committed May 29, 2024
1 parent 3bb5af5 commit 4873e0e
Showing 1 changed file with 1 addition and 104 deletions.
105 changes: 1 addition & 104 deletions src/BatchEventAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,86 +36,12 @@ export class BatchEventAttributes {
public constructor() {
this._attributes = {};
}

private addTags(tags: Array<string>): BatchEventAttributes {
tags.forEach(tag => {
if (typeof tag === 'undefined') {
Log(false, 'BatchEventData - A tag is required');
return this;
}

if (isString(tag)) {
if (tag.length === 0 || tag.length > Consts.EventDataStringMaxLength) {
Log(
false,
"BatchEventData - Tags can't be empty or longer than " +
Consts.EventDataStringMaxLength +
" characters. Ignoring tag '" +
tag +
"'."
);
return this;
}
} else {
Log(false, 'BatchEventData - Tag argument must be a string');
return this;
}
if (tags.length >= Consts.EventDataMaxTags) {
Log(false, 'BatchEventData - Event data cannot hold more than ' + Consts.EventDataMaxTags + " tags. Ignoring tag: '" + tag + "'");
return this;
}
});
this._attributes['$tags'] = {
type: TypedEventAttributeType.StringArray,
value: tags,
};
return this;
}

private checkBeforePuttingAttribute(key: string, value: unknown): void {
if (!isString(key)) {
Log(false, 'BatchEventData - Key must be a string');
throw new Error();
}

if (!Consts.AttributeKeyRegexp.test(key || '')) {
if (key !== '$tags' && key !== '$label') {
Log(
false,
'BatchEventData - Invalid key. Please make sure that the key is made of letters, underscores and numbers only (a-zA-Z0-9_).' +
"It also can't be longer than 30 characters. Ignoring attribute '" +
key +
"'"
);
throw new Error();
}
}

if (typeof value === 'undefined' || value === null) {
Log(false, 'BatchEventData - Value cannot be undefined or null');
throw new Error();
}

if (Object.keys(this._attributes).length >= Consts.EventDataMaxValues && !Object.prototype.hasOwnProperty.call(this._attributes, key)) {
Log(
false,
'BatchEventData - Event data cannot hold more than ' + Consts.EventDataMaxValues + " attributes. Ignoring attribute: '" + key + "'"
);
throw new Error();
}
}

private prepareAttributeKey(key: string): string {
return key.toLowerCase();
}

public putDate(key: string, value: number): BatchEventAttributes {
key = this.prepareAttributeKey(key);
try {
this.checkBeforePuttingAttribute(key, value);
} catch {
return this;
}

this._attributes[key] = {
type: TypedEventAttributeType.Date,
Expand All @@ -127,23 +53,6 @@ export class BatchEventAttributes {

public putURL(key: string, url: string): BatchEventAttributes {
key = this.prepareAttributeKey(key);
try {
this.checkBeforePuttingAttribute(key, url);
} catch {
return this;
}

if (url.length > Consts.EventDataURLMaxLength) {
Log(
false,
"BatchEventData - Event data can't be longer than " +
Consts.EventDataURLMaxLength +
" characters. Ignoring event data value '" +
url +
"'."
);
return this;
}

this._attributes[key] = {
type: TypedEventAttributeType.URL,
Expand All @@ -159,18 +68,6 @@ export class BatchEventAttributes {
): BatchEventAttributes {
key = this.prepareAttributeKey(key);

try {
this.checkBeforePuttingAttribute(key, value);
} catch {
return this;
}

// Check if data contains legacy tags
if (key == '$tags' && isStringArray(value)) {
this.addTags(value);
return this;
}

let typedAttrValue: ITypedEventAttribute | undefined;
if (isString(value)) {
typedAttrValue = {
Expand Down Expand Up @@ -207,7 +104,7 @@ export class BatchEventAttributes {
value: array,
};
} else {
Log(false, 'BatchEventData - Invalid attribute value type. Must be a string, number or boolean');
Log(false, 'BatchEventAttributes - Invalid attribute value type. Must be a string, number or boolean');
return this;
}

Expand Down

0 comments on commit 4873e0e

Please sign in to comment.