Skip to content

Commit 06258a8

Browse files
committed
increase max number size, allow faker.helpers.arrayElements, parse args before call, update ui label
1 parent b471586 commit 06258a8

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/faker-mapping-selector.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,23 @@ const labelStyles = css({
2727
fontWeight: 600,
2828
});
2929

30+
const parseFakerArg = (arg: FakerArg): string => {
31+
if (typeof arg === 'object' && arg !== null && 'json' in arg) {
32+
try {
33+
return JSON.stringify(JSON.parse(arg.json));
34+
} catch {
35+
return '';
36+
}
37+
}
38+
return arg.toString();
39+
};
40+
3041
const formatFakerFunctionCallWithArgs = (
3142
fakerFunction: string,
3243
fakerArgs: FakerArg[]
3344
) => {
34-
return `faker.${fakerFunction}(${fakerArgs.join(', ')})`;
45+
const parsedFakerArgs = fakerArgs.map(parseFakerArg);
46+
return `faker.${fakerFunction}(${parsedFakerArgs.join(', ')})`;
3547
};
3648

3749
interface Props {
@@ -85,11 +97,11 @@ const FakerMappingSelector = ({
8597
</Banner>
8698
) : (
8799
<>
88-
<Label htmlFor="sample-faker-function-call">
89-
Sample Faker Function Call
100+
<Label htmlFor="preview-faker-function-call">
101+
Preview Faker Function Call
90102
</Label>
91103
<Code
92-
id="sample-faker-function-call"
104+
id="preview-faker-function-call"
93105
language="javascript"
94106
copyable={false}
95107
>

packages/compass-collection/src/components/mock-data-generator-modal/utils.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ describe('Mock Data Generator Utils', () => {
3838
});
3939

4040
it('returns false for numbers that are too large', () => {
41-
expect(areFakerArgsValid([1001])).to.be.false;
42-
expect(areFakerArgsValid([-1001])).to.be.false;
43-
expect(areFakerArgsValid([{ json: { length: 1001 } } as any])).to.be
41+
expect(areFakerArgsValid([10001])).to.be.false;
42+
expect(areFakerArgsValid([-10001])).to.be.false;
43+
expect(areFakerArgsValid([{ json: { length: 10001 } } as any])).to.be
4444
.false;
45-
expect(areFakerArgsValid([{ json: { length: -1001 } } as any])).to.be
45+
expect(areFakerArgsValid([{ json: { length: -10001 } } as any])).to.be
4646
.false;
4747
expect(
48-
areFakerArgsValid([{ json: { width: 1001, height: 1001 } } as any])
48+
areFakerArgsValid([{ json: { width: 10001, height: 10001 } } as any])
4949
).to.be.false;
5050
});
5151

packages/compass-collection/src/components/mock-data-generator-modal/utils.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { faker } from '@faker-js/faker/locale/en';
55
const MAX_FAKER_ARGS_LENGTH = 10;
66
const MAX_FAKER_STRING_LENGTH = 1000;
77
const MAX_FAKER_ARGS_DEPTH = 3;
8-
const MAX_FAKER_NUMBER_SIZE = 1000;
8+
const MAX_FAKER_NUMBER_SIZE = 10000;
99

1010
/**
1111
* Checks if the provided faker arguments are valid.
@@ -30,8 +30,7 @@ export function areFakerArgsValid(
3030
for (const arg of fakerArgs) {
3131
if (arg === null || arg === undefined) {
3232
return false;
33-
}
34-
if (typeof arg === 'boolean') {
33+
} else if (typeof arg === 'boolean') {
3534
// booleans are always valid, continue
3635
continue;
3736
} else if (typeof arg === 'number') {
@@ -120,7 +119,11 @@ function getFakerModuleAndMethod(method: string) {
120119
}
121120

122121
function isAllowedHelper(moduleName: string, methodName: string) {
123-
return moduleName !== 'helpers' || methodName === 'arrayElement';
122+
return (
123+
moduleName !== 'helpers' ||
124+
methodName === 'arrayElement' ||
125+
methodName === 'arrayElements'
126+
);
124127
}
125128

126129
function canInvokeFakerMethod(fakerModule: unknown, methodName: string) {
@@ -141,7 +144,8 @@ function tryInvokeFakerMethod(
141144
// If args are present and safe, try calling with args
142145
if (args.length > 0 && areFakerArgsValid(args)) {
143146
try {
144-
callable(...args);
147+
const parsedArgs = parseFakerArgs(args);
148+
callable(...parsedArgs);
145149
return { isValid: true, fakerArgs: args };
146150
} catch {
147151
// Call with args failed. Fall through to trying without args
@@ -163,3 +167,13 @@ function tryInvokeFakerMethod(
163167
return { isValid: false, fakerArgs: [] };
164168
}
165169
}
170+
171+
// Parse the faker args to ensure we can call the method with the args
172+
function parseFakerArgs(args: FakerArg[]): FakerArg[] {
173+
return args.map((arg) => {
174+
if (typeof arg === 'object' && arg !== null && 'json' in arg) {
175+
return JSON.parse((arg as { json: string }).json);
176+
}
177+
return arg;
178+
});
179+
}

0 commit comments

Comments
 (0)