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

Support for inline struct fields #14

Closed
m4tty-d opened this issue Jun 28, 2024 · 0 comments · Fixed by #15
Closed

Support for inline struct fields #14

m4tty-d opened this issue Jun 28, 2024 · 0 comments · Fixed by #15

Comments

@m4tty-d
Copy link
Contributor

m4tty-d commented Jun 28, 2024

Hey!

I just ran into that the current implementation doesn't properly handle inline struct fields.

When a struct embeds another struct with the json:",inline" tag, the fields from the embedded struct should be flattened into the parent struct's schema.

Here's a test case that demonstrates the issue:

func TestInlineStructField(t *testing.T) {
	type TestInline struct {
		InlineField1 string  `json:"inlineField1"`
		InlineField2 *string `json:"inlineField2,omitempty"`
	}

	type Test struct {
		*TestInline `json:",inline"`
		TestField   string `json:"testField"`
	}

	assert.Equal(t,
		`export const TestSchema = z.object({
  inlineField1: z.string(),
  inlineField2: z.string().optional(),
  testField: z.string(),
})
export type Test = z.infer<typeof TestSchema>

`, StructToZodSchema(Test{}))
}

I've already created a fix, will open a pull request soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant