astahmer / openapi-zod-client

Generate a zodios (typescript http client with zod validation) from an OpenAPI spec (json/yaml)
openapi-zod-client.vercel.app
717 stars 80 forks source link

Add zod brand support #265

Closed MalpenZibo closed 7 months ago

MalpenZibo commented 7 months ago

This PR adds zod brand feature support.

It's very useful when dealing with IDs.

For example

"/entity/{entityId}":
  parameters:
    - schema:
        type: string
        format: uuid
      name: entityId
      in: path
      required: true
      description: identifier of the entity to be updated

In this and other cases, it could be useful to get as an input parameter something like z.string().uuid().brand("EntityId").

With this change, we could add to our openapi-spec the property x-brand to achieve this.

"/entity/{entityId}":
  parameters:
    - schema:
        type: string
        format: uuid
        x-brand: EntityId
      name: entityId
      in: path
      required: true
      description: identifier of the entity to be updated
vercel[bot] commented 7 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
openapi-zod-client ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 18, 2024 2:58pm
astahmer commented 7 months ago

hey, sorry this library is already bloated with a thousand options and edge-cases, I'd rather not add one more

you can probably use the endpointDefinitionRefiner https://github.com/astahmer/openapi-zod-client/blob/main/lib/src/template-context.ts#L369C2-L376C37 from https://github.com/astahmer/openapi-zod-client/issues/173 for your specific use-case

if not, I guess you could create a callback to edit the resulting zod schema (and another one for TS types) so that you can refine it to your needs (and everyone could just hook into it to fit their specific needs as well), but I think the current endpointDefinitionRefiner already allows that