Skip to content

Commit 5ffa0a0

Browse files
Merge pull request #217 from kotaro0522/main
[zod-mock] Fix parseString
2 parents 9d03494 + 6b01c57 commit 5ffa0a0

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

packages/zod-mock/src/lib/zod-mock.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ describe('zod-mock', () => {
113113
return;
114114
});
115115

116+
it('Should manually mock string key names to set values when the validation has regex', () => {
117+
const schema = z.object({
118+
telephone: z.string().regex(/^\+[1-9]\d{1,14}$/)
119+
});
120+
121+
const stringMap = {
122+
telephone: () => '+919367788755',
123+
};
124+
125+
const mockData = generateMock(schema, { stringMap });
126+
127+
expect(mockData.telephone).toEqual('+919367788755');
128+
129+
return;
130+
});
131+
116132
it('should convert values produced by Faker to string when the schema type is string.', () => {
117133
const schema = z.object({
118134
number: z.string(),

packages/zod-mock/src/lib/zod-mock.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ function parseString(
125125
zodRef: z.ZodString,
126126
options?: GenerateMockOptions
127127
): string | number | boolean {
128+
// Prioritize user provided generators.
129+
if (options?.keyName && options.stringMap) {
130+
// min/max length handling is not applied here
131+
const generator = options.stringMap[options.keyName];
132+
if (generator) {
133+
return generator();
134+
}
135+
}
136+
128137
const fakerInstance = options?.faker || faker;
129138
const { checks = [] } = zodRef._def;
130139

@@ -142,14 +151,6 @@ function parseString(
142151
}
143152

144153
const lowerCaseKeyName = options?.keyName?.toLowerCase();
145-
// Prioritize user provided generators.
146-
if (options?.keyName && options.stringMap) {
147-
// min/max length handling is not applied here
148-
const generator = options.stringMap[options.keyName];
149-
if (generator) {
150-
return generator();
151-
}
152-
}
153154
const stringOptions: {
154155
min?: number;
155156
max?: number;

0 commit comments

Comments
 (0)