Southclaws / supervillain

Converts Go structs to Zod schemas
MIT License
76 stars 6 forks source link

Support for inline struct fields #14

Closed m4tty-d closed 2 months ago

m4tty-d commented 2 months ago

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.