Skip to content

Commit 9357ca7

Browse files
committed
remove faker arg validation
1 parent b640fc0 commit 9357ca7

File tree

2 files changed

+28
-63
lines changed

2 files changed

+28
-63
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,8 @@ import type { CollectionState } from '../../modules/collection-tab';
1616
import { default as collectionTabReducer } from '../../modules/collection-tab';
1717
import type { ConnectionInfo } from '@mongodb-js/connection-info';
1818
import type { MockDataSchemaResponse } from '@mongodb-js/compass-generative-ai';
19-
import type { SinonSandbox } from 'sinon';
20-
import sinon from 'sinon';
21-
22-
// eslint-disable-next-line @typescript-eslint/no-require-imports
23-
const { faker } = require('@faker-js/faker/locale/en');
2419

2520
describe('MockDataGeneratorModal', () => {
26-
let sandbox: SinonSandbox;
27-
28-
beforeEach(() => {
29-
sandbox = sinon.createSandbox();
30-
});
31-
32-
afterEach(() => {
33-
sandbox.restore();
34-
});
35-
3621
async function renderModal({
3722
isOpen = true,
3823
currentStep = MockDataGeneratorStep.SCHEMA_CONFIRMATION,
@@ -286,7 +271,15 @@ describe('MockDataGeneratorModal', () => {
286271
{
287272
fieldPath: 'email',
288273
mongoType: 'string',
289-
fakerMethod: 'internet.emailAddress',
274+
fakerMethod: 'internet',
275+
fakerArgs: [],
276+
isArray: false,
277+
probability: 1.0,
278+
},
279+
{
280+
fieldPath: 'username',
281+
mongoType: 'string',
282+
fakerMethod: 'noSuchMethod',
290283
fakerArgs: [],
291284
isArray: false,
292285
probability: 1.0,
@@ -329,9 +322,6 @@ describe('MockDataGeneratorModal', () => {
329322
});
330323

331324
it('shows correct values for the faker schema editor', async () => {
332-
sandbox.stub(faker, 'person').returns('Jane');
333-
sandbox.stub(faker, 'number').returns(30);
334-
sandbox.stub(faker, 'internet').throws(new Error('Invalid faker method'));
335325
await renderModal({ mockServices: mockServicesWithMockDataResponse });
336326

337327
// advance to the schema editor step
@@ -365,6 +355,14 @@ describe('MockDataGeneratorModal', () => {
365355
'Please select a function or we will default fill this field with the string "Unrecognized"'
366356
)
367357
).to.exist;
358+
359+
// select the "username" field
360+
userEvent.click(screen.getByText('username'));
361+
expect(screen.getByText('username')).to.exist;
362+
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
363+
expect(screen.getByLabelText('Faker Function')).to.have.value(
364+
'Unrecognized'
365+
);
368366
});
369367

370368
it('disables the Next button when the faker schema mapping is not confirmed', async () => {

packages/compass-collection/src/modules/collection-tab.ts

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ import type {
4040
MockDataGeneratorState,
4141
} from '../components/mock-data-generator-modal/types';
4242

43-
/* eslint-disable @typescript-eslint/no-require-imports */
44-
const { faker } = require('@faker-js/faker/locale/en');
43+
// @ts-expect-error TypeScript warns us about importing ESM module from CommonJS module, but we can ignore since this code will be consumed by webpack.
44+
import { faker } from '@faker-js/faker/locale/en';
4545

4646
const DEFAULT_SAMPLE_SIZE = 100;
4747

@@ -694,56 +694,23 @@ const validateFakerSchema = (
694694
logger: Logger
695695
) => {
696696
return fakerSchema.content.fields.map((field) => {
697-
const { fakerMethod, fakerArgs } = field;
697+
const { fakerMethod } = field;
698698

699-
const [first, second] = fakerMethod.split('.');
700-
try {
701-
// Try with arguments first
702-
const method = (faker as any)?.[first]?.[second];
703-
if (typeof method !== 'function') {
704-
throw new Error(`Faker method ${fakerMethod} is not a function`);
705-
}
706-
method(...fakerArgs);
707-
return field;
708-
} catch (error) {
709-
// If that fails and there are arguments, try without arguments
710-
if (fakerArgs.length > 0) {
711-
try {
712-
const method = (faker as any)?.[first]?.[second];
713-
if (typeof method !== 'function') {
714-
throw new Error(`Faker method ${fakerMethod} is not a function`);
715-
}
716-
method();
717-
return field;
718-
} catch (error) {
719-
logger.debug(
720-
mongoLogId(1_001_000_371),
721-
'Collection',
722-
'Failed to validate faker schema with arguments',
723-
{
724-
error: error instanceof Error ? error.message : String(error),
725-
fakerMethod,
726-
fakerArgs,
727-
}
728-
);
729-
}
730-
}
731-
logger.debug(
732-
mongoLogId(1_001_000_372),
699+
const [firstLevel, secondLevel] = fakerMethod.split('.');
700+
if (typeof (faker as any)[firstLevel]?.[secondLevel] !== 'function') {
701+
logger.log.warn(
702+
mongoLogId(1_001_000_365),
733703
'Collection',
734-
'Failed to validate faker schema',
735-
{
736-
error: error instanceof Error ? error.message : String(error),
737-
fakerMethod,
738-
fakerArgs,
739-
}
704+
'Invalid faker method',
705+
{ fakerMethod }
740706
);
741707
return {
742708
...field,
743709
fakerMethod: UNRECOGNIZED_FAKER_METHOD,
744-
fakerArgs: [],
745710
};
746711
}
712+
713+
return field;
747714
});
748715
};
749716

0 commit comments

Comments
 (0)