Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrap properties beginning with a number in quotes #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/warm-scissors-live.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion lib/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/ref-in-another-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions lib/tests/ref-in-another-file/single-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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