question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `.meta()` to zod types

See original GitHub issue

Inspecting 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:closed
  • Created 9 months ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
samchungycommented, Dec 21, 2022

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).

1reaction
maxArturocommented, Dec 19, 2022

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Meta data/labels · Issue #273 · colinhacks/zod
We want to create something that parses our Hapi route objects, sees the Zod object we have passed to the validator, and generates...
Read more >
Schema validation in TypeScript with Zod
In this article, you will learn about schema design and validation in Zod and how to run it in a TypeScript codebase at...
Read more >
Zod: create a schema using an existing type
I would suggest doing the following: import { z } from 'zod'; import type { Method } from 'axios'; const methods: z. ZodType<Method>...
Read more >
Zod | Documentation
With Zod, you declare a validator once and Zod will automatically infer the static TypeScript type. It's easy to compose simpler types into...
Read more >
asteasolutions/zod-to-openapi
Unsupported types. In case you try to create an OpenAPI schema from a zod schema that is not one of the aforementioned types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found