Skip to content

Commit 295b488

Browse files
authored
Merge pull request #1728 from georgesmith46/fix/zod-pattern-escape
2 parents f5a5387 + cc8dabb commit 295b488

File tree

21 files changed

+31
-42
lines changed

21 files changed

+31
-42
lines changed

.changeset/twenty-dryers-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: correctly generate zod regex expressions when using patterns

packages/openapi-ts/src/plugins/zod/plugin.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -527,21 +527,12 @@ const stringTypeToZodSchema = ({
527527
}
528528

529529
if (schema.pattern) {
530-
const text = schema.pattern
531-
.replace(/\\/g, '\\\\') // backslashes
532-
.replace(/\n/g, '\\n') // newlines
533-
.replace(/\r/g, '\\r') // carriage returns
534-
.replace(/\t/g, '\\t') // tabs
535-
.replace(/\f/g, '\\f') // form feeds
536-
.replace(/\v/g, '\\v') // vertical tabs
537-
.replace(/'/g, "\\'") // single quotes
538-
.replace(/"/g, '\\"'); // double quotes
539530
stringExpression = compiler.callExpression({
540531
functionName: compiler.propertyAccessExpression({
541532
expression: stringExpression,
542533
name: regexIdentifier,
543534
}),
544-
parameters: [compiler.regularExpressionLiteral({ text })],
535+
parameters: [compiler.regularExpressionLiteral({ text: schema.pattern })],
545536
});
546537
}
547538

packages/openapi-ts/test/__snapshots__/2.0.x/plugins/@hey-api/schemas/default/schemas.gen.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,7 @@ export const ModelWithPatternSchema = {
604604
},
605605
patternWithNewline: {
606606
type: 'string',
607-
pattern: `aaa
608-
bbb`
607+
pattern: 'aaa\\nbbb'
609608
},
610609
patternWithBacktick: {
611610
type: 'string',

packages/openapi-ts/test/__snapshots__/2.0.x/plugins/zod/default/zod.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ export const zModelWithPattern = z.object({
227227
name: z.string().max(255),
228228
enabled: z.boolean().readonly().optional(),
229229
modified: z.string().datetime().readonly().optional(),
230-
id: z.string().regex(/^\\d{2}-\\d{3}-\\d{4}$/).optional(),
231-
text: z.string().regex(/^\\w+$/).optional(),
232-
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9\']*$/).optional(),
230+
id: z.string().regex(/^\d{2}-\d{3}-\d{4}$/).optional(),
231+
text: z.string().regex(/^\w+$/).optional(),
232+
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9']*$/).optional(),
233233
patternWithNewline: z.string().regex(/aaa\nbbb/).optional(),
234234
patternWithBacktick: z.string().regex(/aaa`bbb/).optional()
235235
});

packages/openapi-ts/test/__snapshots__/3.0.x/plugins/@hey-api/schemas/default/schemas.gen.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,8 +1204,7 @@ export const ModelWithPatternSchema = {
12041204
},
12051205
patternWithNewline: {
12061206
type: 'string',
1207-
pattern: `aaa
1208-
bbb`
1207+
pattern: 'aaa\\nbbb'
12091208
},
12101209
patternWithBacktick: {
12111210
type: 'string',

packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ export const zModelWithPattern = z.object({
462462
name: z.string().max(255),
463463
enabled: z.boolean().readonly().optional(),
464464
modified: z.string().datetime().readonly().optional(),
465-
id: z.string().regex(/^\\d{2}-\\d{3}-\\d{4}$/).optional(),
466-
text: z.string().regex(/^\\w+$/).optional(),
467-
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9\']*$/).optional(),
465+
id: z.string().regex(/^\d{2}-\d{3}-\d{4}$/).optional(),
466+
text: z.string().regex(/^\w+$/).optional(),
467+
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9']*$/).optional(),
468468
patternWithNewline: z.string().regex(/aaa\nbbb/).optional(),
469469
patternWithBacktick: z.string().regex(/aaa`bbb/).optional()
470470
});

packages/openapi-ts/test/__snapshots__/3.0.x/validators/zod.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { z } from 'zod';
44

55
export const zFoo: z.ZodTypeAny = z.union([
66
z.object({
7-
foo: z.string().regex(/^\\d{3}-\\d{2}-\\d{4}$/).optional(),
7+
foo: z.string().regex(/^\d{3}-\d{2}-\d{4}$/).optional(),
88
bar: z.object({
99
foo: z.lazy(() => {
1010
return zFoo;

packages/openapi-ts/test/__snapshots__/3.1.x/plugins/@hey-api/schemas/default/schemas.gen.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,8 +1194,7 @@ export const ModelWithPatternSchema = {
11941194
},
11951195
patternWithNewline: {
11961196
type: 'string',
1197-
pattern: `aaa
1198-
bbb`
1197+
pattern: 'aaa\\nbbb'
11991198
},
12001199
patternWithBacktick: {
12011200
type: 'string',

packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,9 @@ export const zModelWithPattern = z.object({
457457
name: z.string().max(255),
458458
enabled: z.boolean().readonly().optional(),
459459
modified: z.string().datetime().readonly().optional(),
460-
id: z.string().regex(/^\\d{2}-\\d{3}-\\d{4}$/).optional(),
461-
text: z.string().regex(/^\\w+$/).optional(),
462-
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9\']*$/).optional(),
460+
id: z.string().regex(/^\d{2}-\d{3}-\d{4}$/).optional(),
461+
text: z.string().regex(/^\w+$/).optional(),
462+
patternWithSingleQuotes: z.string().regex(/^[a-zA-Z0-9']*$/).optional(),
463463
patternWithNewline: z.string().regex(/aaa\nbbb/).optional(),
464464
patternWithBacktick: z.string().regex(/aaa`bbb/).optional()
465465
});

packages/openapi-ts/test/__snapshots__/3.1.x/validators/zod.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { z } from 'zod';
44

55
export const zFoo: z.ZodTypeAny = z.union([
66
z.object({
7-
foo: z.string().regex(/^\\d{3}-\\d{2}-\\d{4}$/).optional(),
7+
foo: z.string().regex(/^\d{3}-\d{2}-\d{4}$/).optional(),
88
bar: z.object({
99
foo: z.lazy(() => {
1010
return zFoo;

0 commit comments

Comments
 (0)