1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-23 13:46:45 +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'; import { cleanContext } from './clean-context';
test('strips invalid context properties from the context', async () => { const invalidJsonTypes = {
const invalidJsonTypes = {
object: {}, object: {},
array: [], array: [],
true: true, true: true,
false: false, false: false,
number: 123, number: 123,
null: null, null: null,
}; };
test('strips invalid context properties from the context', async () => {
const validValues = { const validValues = {
appName: 'test', appName: 'test',
}; };
@ -19,7 +19,7 @@ test('strips invalid context properties from the context', async () => {
...validValues, ...validValues,
}; };
const cleanedContext = cleanContext(inputContext); const { context: cleanedContext } = cleanContext(inputContext);
expect(cleanedContext).toStrictEqual(validValues); expect(cleanedContext).toStrictEqual(validValues);
}); });
@ -29,7 +29,25 @@ test("doesn't add non-existing properties", async () => {
appName: 'test', appName: 'test',
}; };
const output = cleanContext(input); const { context: output } = cleanContext(input);
expect(output).toStrictEqual(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'; 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 { appName, ...otherContextFields } = context;
const removedProperties: string[] = [];
const cleanedContextFields = Object.fromEntries( const cleanedContextFields = Object.fromEntries(
Object.entries(otherContextFields).filter( Object.entries(otherContextFields).filter(([key, value]) => {
([key, value]) => key === 'properties' || typeof value === 'string', if (key === 'properties' || typeof value === 'string') {
), return true;
}
removedProperties.push(key);
return false;
}),
); );
return { return {
context: {
...cleanedContextFields, ...cleanedContextFields,
appName, 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( validateQueryComplexity(
environments.length, environments.length,