-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .meta()
to zod types
#1718
Comments
I was just checking out a lib called https://github.com/StefanTerdell/zod-to-json-schema and it seems they are converting json schemas's description with using This issue also seems to request this, but maybe opinions on this have shifted. This seems to work currently:
So I wonder if zod actually does anything internally with That being said, I think it would be awesome if we have some examples on how to extend the zod core to add our own version of for example |
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 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 |
It supports But you can add when are creating a schema too like that: const zodNumber = z.number({
description: 'My zod number',
}) For me I miss the ability to access if is required or not in the I think zod could fix that by creating a obj |
Yeah I understand the opinions in #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. |
I'm sure it could be used in pretty dumb ways, but potentially in pretty cool ways as well. As someone who's built and worked on zod-introspecting packages and who uses zod schemas constantly (for form validation, automatic form construction, and trpc input validation), I can at least attest that having it being natively supported anywhere the Personally, I'd prefer a way to have But even just some extra pre-defined meta fields like @samchungy is suggesting would be very useful on their own without opening the door to too much craziness. |
Interesting, so zod has a The problem is it loses information whenever the schema is wrapped ( |
@AndreiLucas123 each successive In any case it seems @colinhacks came down on one side under #273 (and I can't say I really disagree). Less is more and all that. FWIW, I don't think I'd ever see any other name brand "utility" packages (think of Although! There could be great value in documenting the available internals of zod so that other devs can easily introspect/extend the schemas. |
@maxArturo I think docs are a great idea here! I sure could've used them. I may write a short-but-potentially-useful guide up soon Anyways - closing this, thanks for the discussion. Doesn't seem super clear whether |
I wish to add some thoughts on this subject, should I do it in this issue, on #273 or create another one? 🤔 |
I was dreaming about it. Unfortunately `zod` does not provide any way to make a custom or branded or third-party schema that can be identified as one programmatically. Developer of `zod` also does not want to make any method to store metadata in schemas, instead, recommends to wrap schemas in some other structures, which is not suitable for the purposes of `express-zod-api`. There are many small inconvenient things in making custom schema classes, that I'd like to replace into native methods, and use `withMeta` wrapper for storing proprietary identifier, so the generators and walkers could still handle it. Related issues: ``` colinhacks/zod#1718 colinhacks/zod#2413 colinhacks/zod#273 colinhacks/zod#71 colinhacks/zod#37 ``` PR I've been waiting for months to merged (programmatically distinguishable branding): ``` colinhacks/zod#2860 ```
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:
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 notWould really love to add something like this, thoughts?
The text was updated successfully, but these errors were encountered: