Skip to content

Conversation

@breandanh-kf
Copy link

@breandanh-kf breandanh-kf commented Nov 28, 2025

Hey!

Firstly, thank you taking the time to make and maintain this library!

I've just (within the last month or so) started using it and after a quick search noticed it was missing this feature, and wanted to contribue back to the community with it.

I hope I've created this the way you're expecting for suggested changes. Of course, looking for feedback, I expect that I have probably missed something.

The changes made are an extension of the changes made by @cs97dah:
#89

I've just re-moulded them to match the new structure of the code (which seems to have changed since this PR was first created). I've also included some other use-case examples in the tests based on the features documented by fastify themselves: https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#serialization.

This change allows for the following schema definition example:

fastify.post('/url', {
  schema: {
    response: {
      200: {
        description: 'Response schema that support different content types'
        content: {
          'application/json': {
            schema: z.object({
              name: z.string(),
              image: z.string(),
              address: z.string(),
            })
          },
          'application/vnd.v1+json': {
            schema: z.arrayOf(TestSchema)
          }
        }
      },
      '3xx': {
        content: {
          'application/vnd.v2+json': {
            schema: z.object({
              fullName: z.string(),
              phone: z.string()
            })
          }
        }
      },
      default: {
        content: {
          // */* is match-all content-type
          '*/*': {
            schema: z.object({
              desc: z.string()
            })
          }
        }
      }
    }
  }
});

It should (if accepted) resolve the following issues:

@breandanh-kf breandanh-kf marked this pull request as ready for review November 28, 2025 17:03
@Mikescops
Copy link

@kibertoad @turkerdev any updates on this PR?

@acidoxee
Copy link

I'm very interested as well in this PR's resolution! I'm trying to migrate from @fastify/type-provider-typebox to fastify-type-provider-zod, and right now we can't do a 1-1 migration since detailed schemas with content don't seem supported.

Is there a blocker to this PR?

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 this pull request may close these issues.

3 participants