1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-13 13:48:59 +02:00

feat: modify clean context function to return removed properties

This commit is contained in:
Thomas Heartman 2024-04-05 09:52:33 +02:00
parent 0a247ab704
commit 0fb3f9460d
No known key found for this signature in database
GPG Key ID: BD1F880DAED1EE78
3 changed files with 47 additions and 18 deletions

View File

@ -1,15 +1,15 @@
import { cleanContext } from './clean-context';
test('strips invalid context properties from the context', async () => {
const invalidJsonTypes = {
object: {},
array: [],
true: true,
false: false,
number: 123,
null: null,
};
const invalidJsonTypes = {
object: {},
array: [],
true: true,
false: false,
number: 123,
null: null,
};
test('strips invalid context properties from the context', async () => {
const validValues = {
appName: 'test',
};
@ -19,7 +19,7 @@ test('strips invalid context properties from the context', async () => {
...validValues,
};
const cleanedContext = cleanContext(inputContext);
const { context: cleanedContext } = cleanContext(inputContext);
expect(cleanedContext).toStrictEqual(validValues);
});
@ -29,7 +29,25 @@ test("doesn't add non-existing properties", async () => {
appName: 'test',
};
const output = cleanContext(input);
const { context: output } = cleanContext(input);
expect(output).toStrictEqual(input);
});
test('it returns the names of all the properties it removed', async () => {
const { removedProperties } = cleanContext({
appName: 'test',
...invalidJsonTypes,
});
const invalidProperties = Object.keys(invalidJsonTypes);
// verify that the two lists contain all the same elements
expect(removedProperties).toEqual(
expect.arrayContaining(invalidProperties),
);
expect(invalidProperties).toEqual(
expect.arrayContaining(removedProperties),
);
});

View File

@ -1,16 +1,26 @@
import type { SdkContextSchema } from '../../openapi';
export const cleanContext = (context: SdkContextSchema): SdkContextSchema => {
export const cleanContext = (
context: SdkContextSchema,
): { context: SdkContextSchema; removedProperties: string[] } => {
const { appName, ...otherContextFields } = context;
const removedProperties: string[] = [];
const cleanedContextFields = Object.fromEntries(
Object.entries(otherContextFields).filter(
([key, value]) => key === 'properties' || typeof value === 'string',
),
Object.entries(otherContextFields).filter(([key, value]) => {
if (key === 'properties' || typeof value === 'string') {
return true;
}
removedProperties.push(key);
return false;
}),
);
return {
...cleanedContextFields,
appName,
context: {
...cleanedContextFields,
appName,
},
removedProperties,
};
};

View File

@ -126,7 +126,8 @@ export class PlaygroundService {
),
);
const contexts = generateObjectCombinations(cleanContext(context));
const { context: cleanedContext } = cleanContext(context);
const contexts = generateObjectCombinations(cleanedContext);
validateQueryComplexity(
environments.length,