Skip to content

Commit f121304

Browse files
Validate impressionsDisabled type
1 parent dd9a447 commit f121304

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

src/sdkClient/__tests__/clientInputValidation.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,24 @@ describe('clientInputValidationDecorator', () => {
107107

108108
expect(logSpy).not.toBeCalled();
109109
});
110+
111+
test('should ignore the properties in the 4th argument if an empty object is passed', () => {
112+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { impressionsDisabled: true })).toBe(EVALUATION_RESULT);
113+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, { impressionsDisabled: true });
114+
115+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { impressionsDisabled: {} })).toBe(EVALUATION_RESULT);
116+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, undefined);
117+
118+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { impressionsDisabled: 'true' })).toBe(EVALUATION_RESULT);
119+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, undefined);
120+
121+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { impressionsDisabled: null })).toBe(EVALUATION_RESULT);
122+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, undefined);
123+
124+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { impressionsDisabled: false })).toBe(EVALUATION_RESULT);
125+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, undefined); // impressionsDisabled false is the default behavior, so we don't pass it along
126+
127+
expect(clientWithValidation.getTreatment('key', 'ff', undefined, { properties: undefined })).toBe(EVALUATION_RESULT);
128+
expect(client.getTreatment).toHaveBeenLastCalledWith('key', 'ff', undefined, undefined);
129+
});
110130
});

src/utils/inputValidation/__tests__/eventProperties.spec.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { ERROR_NOT_PLAIN_OBJECT, ERROR_SIZE_EXCEEDED, WARN_SETTING_NULL, WARN_TRIMMING_PROPERTIES } from '../../../logger/constants';
1+
import { ERROR_NOT_BOOLEAN, ERROR_NOT_PLAIN_OBJECT, ERROR_SIZE_EXCEEDED, WARN_SETTING_NULL, WARN_TRIMMING_PROPERTIES } from '../../../logger/constants';
22
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
33

4-
import { validateEventProperties } from '../eventProperties';
4+
import { validateEventProperties, validateImpressionsDisabled } from '../eventProperties';
55

66
function calculateSize(obj: any) {
77
// we calculate the expected size.
@@ -193,3 +193,47 @@ describe('INPUT VALIDATION for Event Properties', () => {
193193
expect(loggerMock.error).toBeCalledWith(ERROR_SIZE_EXCEEDED, ['some_method_eventProps']); // Should log an error.
194194
});
195195
});
196+
197+
describe('INPUT VALIDATION for Impressions disabled', () => {
198+
199+
afterEach(() => { loggerMock.mockClear(); });
200+
201+
const impressionsDisabledInvalidValues = [
202+
[],
203+
() => { },
204+
'something',
205+
NaN,
206+
-Infinity,
207+
Infinity,
208+
new Promise(res => res),
209+
Symbol('asd'),
210+
new Map()
211+
];
212+
213+
test('Not setting impressionsDisabled is acceptable', () => {
214+
expect(validateImpressionsDisabled(loggerMock, undefined, 'some_method_eventProps')).toBeUndefined();
215+
216+
expect(loggerMock.error).not.toBeCalled(); // Should not log any errors.
217+
expect(loggerMock.warn).not.toBeCalled(); // It should have not logged any warnings.
218+
});
219+
220+
test('When setting a value for impressionsDisabled, only booleans are acceptable', () => {
221+
222+
impressionsDisabledInvalidValues.forEach(val => {
223+
expect(validateImpressionsDisabled(loggerMock, val, 'some_method_eventProps')).toBeUndefined();
224+
expect(loggerMock.error).toBeCalledWith(ERROR_NOT_BOOLEAN, ['some_method_eventProps', 'impressionsDisabled']); // Should log an error.
225+
loggerMock.error.mockClear();
226+
});
227+
228+
expect(loggerMock.warn).not.toBeCalled(); // It should have not logged any warnings.
229+
});
230+
231+
test('It should return the impressionsDisabled value when valid', () => {
232+
expect(validateImpressionsDisabled(loggerMock, true, 'some_method_eventProps')).toBeTruthy();
233+
expect(validateImpressionsDisabled(loggerMock, false, 'some_method_eventProps')).toBeFalsy();
234+
235+
expect(loggerMock.error).not.toBeCalled(); // Should not log any errors.
236+
expect(loggerMock.warn).not.toBeCalled(); // It should have not logged any warnings.
237+
});
238+
239+
});

src/utils/inputValidation/eventProperties.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isObject, isString, isFiniteNumber, isBoolean } from '../lang';
22
import { objectAssign } from '../lang/objectAssign';
33
import SplitIO from '../../../types/splitio';
44
import { ILogger } from '../../logger/types';
5-
import { ERROR_NOT_PLAIN_OBJECT, ERROR_SIZE_EXCEEDED, WARN_SETTING_NULL, WARN_TRIMMING_PROPERTIES } from '../../logger/constants';
5+
import { ERROR_NOT_PLAIN_OBJECT, ERROR_NOT_BOOLEAN, ERROR_SIZE_EXCEEDED, WARN_SETTING_NULL, WARN_TRIMMING_PROPERTIES } from '../../logger/constants';
66

77
const ECMA_SIZES = {
88
NULL: 0, // While on the JSON it's going to occupy more space, we'll take it as 0 for the approximation.
@@ -14,6 +14,17 @@ const MAX_PROPERTIES_AMOUNT = 300;
1414
const MAX_EVENT_SIZE = 1024 * 32;
1515
const BASE_EVENT_SIZE = 1024; // We assume 1kb events without properties (avg measured)
1616

17+
export function validateImpressionsDisabled(log: ILogger, maybeImpressionsDisabled: any, method: string): boolean | undefined {
18+
if (maybeImpressionsDisabled === undefined) return;
19+
20+
if (maybeImpressionsDisabled === null || !isBoolean(maybeImpressionsDisabled)) {
21+
log.error(ERROR_NOT_BOOLEAN, [method, 'impressionsDisabled']);
22+
return;
23+
}
24+
25+
return maybeImpressionsDisabled;
26+
}
27+
1728
export function validateEventProperties(log: ILogger, maybeProperties: any, method: string): { properties: SplitIO.Properties | null | false, size: number } {
1829
if (maybeProperties == undefined) return { properties: null, size: BASE_EVENT_SIZE }; // eslint-disable-line eqeqeq
1930

@@ -72,7 +83,7 @@ export function validateEvaluationOptions(log: ILogger, maybeOptions: any, metho
7283
const properties = validateEventProperties(log, maybeOptions.properties, method).properties;
7384
let options = properties && Object.keys(properties).length > 0 ? { properties } : undefined;
7485

75-
const impressionsDisabled = maybeOptions.impressionsDisabled;
86+
const impressionsDisabled = validateImpressionsDisabled(log, maybeOptions.impressionsDisabled, method);
7687
if (!impressionsDisabled) return options;
7788

7889
return options ? { ...options, impressionsDisabled } : { impressionsDisabled };

0 commit comments

Comments
 (0)