Skip to content

Commit

Permalink
Rename flagSetsAreValid function to validateFlagSets for consistency …
Browse files Browse the repository at this point in the history
…with other validation function names
  • Loading branch information
EmilianoSanchez committed Dec 1, 2023
1 parent b2a2cb0 commit b06e1a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/sdkClient/clientInputValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { IReadinessManager } from '../readiness/types';
import { MaybeThenable } from '../dtos/types';
import { ISettings, SplitIO } from '../types';
import { isStorageSync } from '../trackers/impressionObserver/utils';
import { flagSetsAreValid } from '../utils/settingsValidation/splitFilters';
import { validateFlagSets } from '../utils/settingsValidation/splitFilters';

/**
* Decorator that validates the input before actually executing the client methods.
Expand All @@ -42,7 +42,7 @@ export function clientInputValidationDecorator<TClient extends SplitIO.IClient |
const attributes = validateAttributes(log, maybeAttributes, methodName);
const isNotDestroyed = validateIfNotDestroyed(log, readinessManager, methodName);
if (maybeFlagSetNameOrNames) {
flagSetOrFlagSets = flagSetsAreValid(log, methodName, maybeFlagSetNameOrNames, settings.sync.__splitFiltersValidation.groupedFilters.bySet);
flagSetOrFlagSets = validateFlagSets(log, methodName, maybeFlagSetNameOrNames, settings.sync.__splitFiltersValidation.groupedFilters.bySet);
}

validateIfOperational(log, readinessManager, methodName, splitOrSplits);
Expand Down
36 changes: 18 additions & 18 deletions src/utils/settingsValidation/__tests__/splitFilters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { STANDALONE_MODE, CONSUMER_MODE, CONSUMER_PARTIAL_MODE, LOCALHOST_MODE,
import { splitFilters, queryStrings, groupedFilters } from '../../../__tests__/mocks/fetchSpecificSplits';

// Test target
import { flagSetsAreValid, validateSplitFilters } from '../splitFilters';
import { validateFlagSets, validateSplitFilters } from '../splitFilters';
import { SETTINGS_SPLITS_FILTER, ERROR_INVALID, ERROR_EMPTY_ARRAY, ERROR_SETS_FILTER_EXCLUSIVE, WARN_SPLITS_FILTER_INVALID, WARN_SPLITS_FILTER_EMPTY, WARN_TRIMMING, WARN_SPLITS_FILTER_INVALID_SET, WARN_SPLITS_FILTER_LOWERCASE_SET, WARN_FLAGSET_NOT_CONFIGURED, WARN_SPLITS_FILTER_IGNORED } from '../../../logger/constants';

describe('validateSplitFilters', () => {
Expand Down Expand Up @@ -131,71 +131,71 @@ describe('validateSplitFilters', () => {
expect(loggerMock.error.mock.calls.length).toEqual(3);
});

test('flagSetsAreValid - Flag set validation for evaluations', () => {
test('validateFlagSets - Flag set validation for evaluations', () => {

let flagSetsFilter = ['set_1', 'set_2'];

// empty array
expect(flagSetsAreValid(loggerMock, 'test_method', [], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', [], flagSetsFilter)).toEqual([]);

// must start with a letter or number
expect(flagSetsAreValid(loggerMock, 'test_method', ['_set_1'], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['_set_1'], flagSetsFilter)).toEqual([]);
expect(loggerMock.warn.mock.calls[0]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['_set_1', regexp, '_set_1']]);

// can contain _a-z0-9
expect(flagSetsAreValid(loggerMock, 'test_method', ['set*1'], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['set*1'], flagSetsFilter)).toEqual([]);
expect(loggerMock.warn.mock.calls[1]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*1', regexp, 'set*1']]);

// have a max length of 50 characters
const longName = '1234567890_1234567890_1234567890_1234567890_1234567890';
expect(flagSetsAreValid(loggerMock, 'test_method', [longName], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', [longName], flagSetsFilter)).toEqual([]);
expect(loggerMock.warn.mock.calls[2]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, [longName, regexp, longName]]);

// both set names invalid -> empty list & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set*1', 'set*3'], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['set*1', 'set*3'], flagSetsFilter)).toEqual([]);
expect(loggerMock.warn.mock.calls[3]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*1', regexp, 'set*1']]);
expect(loggerMock.warn.mock.calls[4]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*3', regexp, 'set*3']]);

// only set_1 is valid => [set_1] & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_1', 'set*3'], flagSetsFilter)).toEqual(['set_1']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_1', 'set*3'], flagSetsFilter)).toEqual(['set_1']);
expect(loggerMock.warn.mock.calls[5]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*3', regexp, 'set*3']]);

// set_3 not included in configuration but set_1 included => [set_1] & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_1', 'set_3'], flagSetsFilter)).toEqual(['set_1']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_1', 'set_3'], flagSetsFilter)).toEqual(['set_1']);
expect(loggerMock.warn.mock.calls[6]).toEqual([WARN_FLAGSET_NOT_CONFIGURED, ['test_method', 'set_3']]);

// set_3 not included in configuration => [] & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_3'], flagSetsFilter)).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['set_3'], flagSetsFilter)).toEqual([]);
expect(loggerMock.warn.mock.calls[7]).toEqual([WARN_FLAGSET_NOT_CONFIGURED, ['test_method', 'set_3']]);

// empty config


// must start with a letter or number
expect(flagSetsAreValid(loggerMock, 'test_method', ['_set_1'], [])).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['_set_1'], [])).toEqual([]);
expect(loggerMock.warn.mock.calls[8]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['_set_1', regexp, '_set_1']]);

// can contain _a-z0-9
expect(flagSetsAreValid(loggerMock, 'test_method', ['set*1'], [])).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['set*1'], [])).toEqual([]);
expect(loggerMock.warn.mock.calls[9]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*1', regexp, 'set*1']]);

// have a max length of 50 characters
expect(flagSetsAreValid(loggerMock, 'test_method', [longName], [])).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', [longName], [])).toEqual([]);
expect(loggerMock.warn.mock.calls[10]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, [longName, regexp, longName]]);

// both set names invalid -> empty list & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set*1', 'set*3'], [])).toEqual([]);
expect(validateFlagSets(loggerMock, 'test_method', ['set*1', 'set*3'], [])).toEqual([]);
expect(loggerMock.warn.mock.calls[11]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*1', regexp, 'set*1']]);
expect(loggerMock.warn.mock.calls[12]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*3', regexp, 'set*3']]);

// only set_1 is valid => [set_1] & warn
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_1', 'set*3'], [])).toEqual(['set_1']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_1', 'set*3'], [])).toEqual(['set_1']);
expect(loggerMock.warn.mock.calls[13]).toEqual([WARN_SPLITS_FILTER_INVALID_SET, ['set*3', regexp, 'set*3']]);

// any set should be returned if there isn't flag sets in filter
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_1'], [])).toEqual(['set_1']);
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_1', 'set_2'], [])).toEqual(['set_1', 'set_2']);
expect(flagSetsAreValid(loggerMock, 'test_method', ['set_3'], [])).toEqual(['set_3']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_1'], [])).toEqual(['set_1']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_1', 'set_2'], [])).toEqual(['set_1', 'set_2']);
expect(validateFlagSets(loggerMock, 'test_method', ['set_3'], [])).toEqual(['set_3']);

});

Expand Down
4 changes: 2 additions & 2 deletions src/utils/settingsValidation/splitFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function queryStringBuilder(groupedFilters: Record<SplitIO.SplitFilterType, stri
}

/**
* Sanitizes set names list taking in account:
* Sanitizes set names list taking into account:
* - It should be lowercase
* - Must adhere the following regular expression /^[a-z0-9][_a-z0-9]{0,49}$/ that means
* - must start with a letter or number
Expand Down Expand Up @@ -188,7 +188,7 @@ export function validateSplitFilters(log: ILogger, maybeSplitFilters: any, mode:
return res;
}

export function flagSetsAreValid(log: ILogger, method: string, flagSets: string[], flagSetsInConfig: string[]): string[] {
export function validateFlagSets(log: ILogger, method: string, flagSets: string[], flagSetsInConfig: string[]): string[] {
const sets = validateSplits(log, flagSets, method, 'flag sets', 'flag set');
let toReturn = sets ? sanitizeFlagSets(log, sets) : [];
if (flagSetsInConfig.length > 0) {
Expand Down

0 comments on commit b06e1a2

Please sign in to comment.