Skip to content

Commit

Permalink
Fix parseString
Browse files Browse the repository at this point in the history
  • Loading branch information
kotaro0522 committed Aug 15, 2024
1 parent 9d03494 commit 6b01c57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
16 changes: 16 additions & 0 deletions packages/zod-mock/src/lib/zod-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ describe('zod-mock', () => {
return;
});

it('Should manually mock string key names to set values when the validation has regex', () => {
const schema = z.object({
telephone: z.string().regex(/^\+[1-9]\d{1,14}$/)
});

const stringMap = {
telephone: () => '+919367788755',
};

const mockData = generateMock(schema, { stringMap });

expect(mockData.telephone).toEqual('+919367788755');

return;
});

it('should convert values produced by Faker to string when the schema type is string.', () => {
const schema = z.object({
number: z.string(),
Expand Down
17 changes: 9 additions & 8 deletions packages/zod-mock/src/lib/zod-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ function parseString(
zodRef: z.ZodString,
options?: GenerateMockOptions
): string | number | boolean {
// Prioritize user provided generators.
if (options?.keyName && options.stringMap) {
// min/max length handling is not applied here
const generator = options.stringMap[options.keyName];
if (generator) {
return generator();
}
}

const fakerInstance = options?.faker || faker;
const { checks = [] } = zodRef._def;

Expand All @@ -142,14 +151,6 @@ function parseString(
}

const lowerCaseKeyName = options?.keyName?.toLowerCase();
// Prioritize user provided generators.
if (options?.keyName && options.stringMap) {
// min/max length handling is not applied here
const generator = options.stringMap[options.keyName];
if (generator) {
return generator();
}
}
const stringOptions: {
min?: number;
max?: number;
Expand Down

0 comments on commit 6b01c57

Please sign in to comment.