fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #1728 from georgesmith46/fix/zod-pattern-escape

authored by

Lubos and committed by
GitHub
295b4884 f5a53876

+31 -42
+5
.changeset/twenty-dryers-fly.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix: correctly generate zod regex expressions when using patterns
+1 -10
packages/openapi-ts/src/plugins/zod/plugin.ts
··· 527 527 } 528 528 529 529 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 539 530 stringExpression = compiler.callExpression({ 540 531 functionName: compiler.propertyAccessExpression({ 541 532 expression: stringExpression, 542 533 name: regexIdentifier, 543 534 }), 544 - parameters: [compiler.regularExpressionLiteral({ text })], 535 + parameters: [compiler.regularExpressionLiteral({ text: schema.pattern })], 545 536 }); 546 537 } 547 538
+1 -2
packages/openapi-ts/test/__snapshots__/2.0.x/plugins/@hey-api/schemas/default/schemas.gen.ts
··· 604 604 }, 605 605 patternWithNewline: { 606 606 type: 'string', 607 - pattern: `aaa 608 - bbb` 607 + pattern: 'aaa\\nbbb' 609 608 }, 610 609 patternWithBacktick: { 611 610 type: 'string',
+3 -3
packages/openapi-ts/test/__snapshots__/2.0.x/plugins/zod/default/zod.gen.ts
··· 227 227 name: z.string().max(255), 228 228 enabled: z.boolean().readonly().optional(), 229 229 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(), 233 233 patternWithNewline: z.string().regex(/aaa\nbbb/).optional(), 234 234 patternWithBacktick: z.string().regex(/aaa`bbb/).optional() 235 235 });
+1 -2
packages/openapi-ts/test/__snapshots__/3.0.x/plugins/@hey-api/schemas/default/schemas.gen.ts
··· 1204 1204 }, 1205 1205 patternWithNewline: { 1206 1206 type: 'string', 1207 - pattern: `aaa 1208 - bbb` 1207 + pattern: 'aaa\\nbbb' 1209 1208 }, 1210 1209 patternWithBacktick: { 1211 1210 type: 'string',
+3 -3
packages/openapi-ts/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts
··· 462 462 name: z.string().max(255), 463 463 enabled: z.boolean().readonly().optional(), 464 464 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(), 468 468 patternWithNewline: z.string().regex(/aaa\nbbb/).optional(), 469 469 patternWithBacktick: z.string().regex(/aaa`bbb/).optional() 470 470 });
+1 -1
packages/openapi-ts/test/__snapshots__/3.0.x/validators/zod.gen.ts
··· 4 4 5 5 export const zFoo: z.ZodTypeAny = z.union([ 6 6 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(), 8 8 bar: z.object({ 9 9 foo: z.lazy(() => { 10 10 return zFoo;
+1 -2
packages/openapi-ts/test/__snapshots__/3.1.x/plugins/@hey-api/schemas/default/schemas.gen.ts
··· 1194 1194 }, 1195 1195 patternWithNewline: { 1196 1196 type: 'string', 1197 - pattern: `aaa 1198 - bbb` 1197 + pattern: 'aaa\\nbbb' 1199 1198 }, 1200 1199 patternWithBacktick: { 1201 1200 type: 'string',
+3 -3
packages/openapi-ts/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts
··· 457 457 name: z.string().max(255), 458 458 enabled: z.boolean().readonly().optional(), 459 459 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(), 463 463 patternWithNewline: z.string().regex(/aaa\nbbb/).optional(), 464 464 patternWithBacktick: z.string().regex(/aaa`bbb/).optional() 465 465 });
+1 -1
packages/openapi-ts/test/__snapshots__/3.1.x/validators/zod.gen.ts
··· 4 4 5 5 export const zFoo: z.ZodTypeAny = z.union([ 6 6 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(), 8 8 bar: z.object({ 9 9 foo: z.lazy(() => { 10 10 return zFoo;
+1 -2
packages/openapi-ts/test/__snapshots__/test/generated/v2/schemas.gen.ts.snap
··· 602 602 }, 603 603 patternWithNewline: { 604 604 type: 'string', 605 - pattern: `aaa 606 - bbb` 605 + pattern: 'aaa\\nbbb' 607 606 }, 608 607 patternWithBacktick: { 609 608 type: 'string',
+1 -2
packages/openapi-ts/test/__snapshots__/test/generated/v3-schemas-form/schemas.gen.ts.snap
··· 1093 1093 }, 1094 1094 patternWithNewline: { 1095 1095 type: 'string', 1096 - pattern: `aaa 1097 - bbb` 1096 + pattern: 'aaa\\nbbb' 1098 1097 }, 1099 1098 patternWithBacktick: { 1100 1099 type: 'string',
+1 -2
packages/openapi-ts/test/__snapshots__/test/generated/v3-schemas-json/schemas.gen.ts.snap
··· 1201 1201 }, 1202 1202 patternWithNewline: { 1203 1203 type: 'string', 1204 - pattern: `aaa 1205 - bbb` 1204 + pattern: 'aaa\\nbbb' 1206 1205 }, 1207 1206 patternWithBacktick: { 1208 1207 type: 'string',
+1 -2
packages/openapi-ts/test/__snapshots__/test/generated/v3-schemas-name/schemas.gen.ts.snap
··· 1201 1201 }, 1202 1202 patternWithNewline: { 1203 1203 type: 'string', 1204 - pattern: `aaa 1205 - bbb` 1204 + pattern: 'aaa\\nbbb' 1206 1205 }, 1207 1206 patternWithBacktick: { 1208 1207 type: 'string',
+1 -1
packages/openapi-ts/test/spec/2.0.x/full.json
··· 1481 1481 }, 1482 1482 "patternWithNewline": { 1483 1483 "type": "string", 1484 - "pattern": "aaa\nbbb" 1484 + "pattern": "aaa\\nbbb" 1485 1485 }, 1486 1486 "patternWithBacktick": { 1487 1487 "type": "string",
+1 -1
packages/openapi-ts/test/spec/3.0.x/full.json
··· 2932 2932 }, 2933 2933 "patternWithNewline": { 2934 2934 "type": "string", 2935 - "pattern": "aaa\nbbb" 2935 + "pattern": "aaa\\nbbb" 2936 2936 }, 2937 2937 "patternWithBacktick": { 2938 2938 "type": "string",
+1 -1
packages/openapi-ts/test/spec/3.0.x/validators.json
··· 40 40 }, 41 41 "Baz": { 42 42 "default": "baz", 43 - "pattern": "foo\nbar", 43 + "pattern": "foo\\nbar", 44 44 "readOnly": true, 45 45 "type": "string" 46 46 }
+1 -1
packages/openapi-ts/test/spec/3.1.x/full.json
··· 2914 2914 }, 2915 2915 "patternWithNewline": { 2916 2916 "type": "string", 2917 - "pattern": "aaa\nbbb" 2917 + "pattern": "aaa\\nbbb" 2918 2918 }, 2919 2919 "patternWithBacktick": { 2920 2920 "type": "string",
+1 -1
packages/openapi-ts/test/spec/3.1.x/validators.json
··· 39 39 }, 40 40 "Baz": { 41 41 "default": "baz", 42 - "pattern": "foo\nbar", 42 + "pattern": "foo\\nbar", 43 43 "readOnly": true, 44 44 "type": "string" 45 45 }
+1 -1
packages/openapi-ts/test/spec/v2.json
··· 1509 1509 }, 1510 1510 "patternWithNewline": { 1511 1511 "type": "string", 1512 - "pattern": "aaa\nbbb" 1512 + "pattern": "aaa\\nbbb" 1513 1513 }, 1514 1514 "patternWithBacktick": { 1515 1515 "type": "string",
+1 -1
packages/openapi-ts/test/spec/v3.json
··· 2908 2908 }, 2909 2909 "patternWithNewline": { 2910 2910 "type": "string", 2911 - "pattern": "aaa\nbbb" 2911 + "pattern": "aaa\\nbbb" 2912 2912 }, 2913 2913 "patternWithBacktick": { 2914 2914 "type": "string",