Skip to content

Commit cbe46b8

Browse files
committed
remove faker arg validation
1 parent 37fda7f commit cbe46b8

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,
@@ -323,7 +308,15 @@ describe('MockDataGeneratorModal', () => {
323308
{
324309
fieldPath: 'email',
325310
mongoType: 'string',
326-
fakerMethod: 'internet.emailAddress',
311+
fakerMethod: 'internet',
312+
fakerArgs: [],
313+
isArray: false,
314+
probability: 1.0,
315+
},
316+
{
317+
fieldPath: 'username',
318+
mongoType: 'string',
319+
fakerMethod: 'noSuchMethod',
327320
fakerArgs: [],
328321
isArray: false,
329322
probability: 1.0,
@@ -366,9 +359,6 @@ describe('MockDataGeneratorModal', () => {
366359
});
367360

368361
it('shows correct values for the faker schema editor', async () => {
369-
sandbox.stub(faker, 'person').returns('Jane');
370-
sandbox.stub(faker, 'number').returns(30);
371-
sandbox.stub(faker, 'internet').throws(new Error('Invalid faker method'));
372362
await renderModal({ mockServices: mockServicesWithMockDataResponse });
373363

374364
// advance to the schema editor step
@@ -402,6 +392,14 @@ describe('MockDataGeneratorModal', () => {
402392
'Please select a function or we will default fill this field with the string "Unrecognized"'
403393
)
404394
).to.exist;
395+
396+
// select the "username" field
397+
userEvent.click(screen.getByText('username'));
398+
expect(screen.getByText('username')).to.exist;
399+
expect(screen.getByLabelText('JSON Type')).to.have.value('string');
400+
expect(screen.getByLabelText('Faker Function')).to.have.value(
401+
'Unrecognized'
402+
);
405403
});
406404

407405
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
@@ -43,8 +43,8 @@ import type {
4343
MockDataGeneratorState,
4444
} from '../components/mock-data-generator-modal/types';
4545

46-
/* eslint-disable @typescript-eslint/no-require-imports */
47-
const { faker } = require('@faker-js/faker/locale/en');
46+
// @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.
47+
import { faker } from '@faker-js/faker/locale/en';
4848

4949
const DEFAULT_SAMPLE_SIZE = 100;
5050

@@ -704,56 +704,23 @@ const validateFakerSchema = (
704704
logger: Logger
705705
) => {
706706
return fakerSchema.content.fields.map((field) => {
707-
const { fakerMethod, fakerArgs } = field;
707+
const { fakerMethod } = field;
708708

709-
const [first, second] = fakerMethod.split('.');
710-
try {
711-
// Try with arguments first
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(...fakerArgs);
717-
return field;
718-
} catch (error) {
719-
// If that fails and there are arguments, try without arguments
720-
if (fakerArgs.length > 0) {
721-
try {
722-
const method = (faker as any)?.[first]?.[second];
723-
if (typeof method !== 'function') {
724-
throw new Error(`Faker method ${fakerMethod} is not a function`);
725-
}
726-
method();
727-
return field;
728-
} catch (error) {
729-
logger.debug(
730-
mongoLogId(1_001_000_371),
731-
'Collection',
732-
'Failed to validate faker schema with arguments',
733-
{
734-
error: error instanceof Error ? error.message : String(error),
735-
fakerMethod,
736-
fakerArgs,
737-
}
738-
);
739-
}
740-
}
741-
logger.debug(
742-
mongoLogId(1_001_000_372),
709+
const [firstLevel, secondLevel] = fakerMethod.split('.');
710+
if (typeof (faker as any)[firstLevel]?.[secondLevel] !== 'function') {
711+
logger.log.warn(
712+
mongoLogId(1_001_000_365),
743713
'Collection',
744-
'Failed to validate faker schema',
745-
{
746-
error: error instanceof Error ? error.message : String(error),
747-
fakerMethod,
748-
fakerArgs,
749-
}
714+
'Invalid faker method',
715+
{ fakerMethod }
750716
);
751717
return {
752718
...field,
753719
fakerMethod: UNRECOGNIZED_FAKER_METHOD,
754-
fakerArgs: [],
755720
};
756721
}
722+
723+
return field;
757724
});
758725
};
759726

0 commit comments

Comments
 (0)