Add `.meta()` to zod types
See original GitHub issueInspecting zod objects directly can be really useful for developing certain types of libraries. Of course tons of libs are already doing it - everything in the zod-to-x
section of the documentation for example. Zod already supports some forms of meta data (custom error messages are a sort of metadata), which is extremely useful for things like building forms.
What if it were possible to attach arbitrary information, (or even just a string) via a meta
function call? Having an explicit “meta” field could allow library developers to have their code react to zod schemas with much higher levels of customization, which could in turn allow us to improve developer experience in new ways.
The example I have in mind is this - Lets look at a tRPC procedure with a zod schema as an input validator:
getPosts: t.procedure.input(z.object({
id: z.string().meta("The id of the post."),
search: z.string().meta("Returns posts with this term in the body").
})).query(() => {
//...
}),
Here we’re attaching a description to each input parameter. Now we can do things like generating documentation directly from our validation schemas. Anyone who’s developing a library that inspects zod schemas would have much more flexibility in the types of APIs they’d be able to develop.
It doesn’t have to be a string, it could be any other typed object. One solution could be .meta<MetaType>()
, or maybe an optional interface for generating a meta enabled client (probably a lot more difficult to create?) export const z = createZodClient<Meta>()
Selfishly, I’m developing a tool that inspects TRPC router and generates a manual testing / documentation UI automatically, which generates forms based on input zod schemas. With a meta
function like this, it would allow developers to create documentation per input field with a minimal amount of effort. For me I’d just want a string but IDK if there are use cases for more complex objects or not
Would really love to add something like this, thoughts?
Issue Analytics
- State:
- Created 9 months ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
Yeah I understand the opinions in https://github.com/colinhacks/zod/issues/273. It could very easily lead to anti-patterns however I can also see the benefits of adding a meta field. At the moment I’m using https://github.com/asteasolutions/zod-to-openapi which extends Zod by binding to methods. Wrapping the zodobject eg meta(zodObject, { examples: 1}) feels clunky compared to zodobject.meta()
Would be very useful to be able to add more metadata officially which could be widely used by doco tools. eg.
examples, description
(I know description is already supported).Hi all,
This doesn’t seem like a big lift. However, I wonder how prevalent is this use for integrating into other APIs? I certainly see the benefit (esp since other libraries are already weaponizing the
.describe()
), but I mean specifically the integration path: adding objects to a schema?If we’re going to work on making API integration accessible, I’d love to look at the ecosystem at large and see how other top-shelf libraries open themselves up to extensibility. Is it enough to add arbitrary objects to a schema? Or perhaps is there another introspection utility (maybe off of
schema._def
) that could be built and would be better suited long term?