diff --git a/.changeset/warm-scissors-live.md b/.changeset/warm-scissors-live.md new file mode 100644 index 00000000..8df5cb1f --- /dev/null +++ b/.changeset/warm-scissors-live.md @@ -0,0 +1,5 @@ +--- +"openapi-zod-client": patch +--- + +When a property from an external json or yaml file starts with a number, e.g. 1st, instead of first, the generated Zod-Schema is corrupt. The change in the wrapWithQuotesIfNeeded method makes sure, that any property starting with a number is wrapped in quotes. diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 7f626d9c..6d7986a5 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -15,7 +15,7 @@ export function normalizeString(text: string) { } export const wrapWithQuotesIfNeeded = (str: string) => { - if (/^\w+$/.test(str)) { + if (/^[a-zA-Z]\w*$/.test(str)) { return str; } diff --git a/lib/tests/ref-in-another-file.test.ts b/lib/tests/ref-in-another-file.test.ts index 74b2e026..c11bea03 100644 --- a/lib/tests/ref-in-another-file.test.ts +++ b/lib/tests/ref-in-another-file.test.ts @@ -19,7 +19,7 @@ test("ref-in-another-file", async () => { "parameters": [], "path": "/robots.txt", "requestFormat": "json", - "response": "z.object({ name: z.string(), completed: z.boolean() }).passthrough()", + "response": "z.object({ name: z.string(), completed: z.boolean(), "0_property_starting_with_number": z.number() }).passthrough()", }, ], "issues": { diff --git a/lib/tests/ref-in-another-file/single-schema.yaml b/lib/tests/ref-in-another-file/single-schema.yaml index 1112e105..09283a28 100644 --- a/lib/tests/ref-in-another-file/single-schema.yaml +++ b/lib/tests/ref-in-another-file/single-schema.yaml @@ -5,6 +5,9 @@ properties: type: string completed: type: boolean + 0_property_starting_with_number: + type: number required: - name - completed + - 0_property_starting_with_number