-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Feature Proposal: Functionalities for adding label and example(s) #2548
Comments
@StefanTerdell I am looking forward to your feedback! |
By and large it LGTM. Some thoughts:
|
@StefanTerdell Thank you for your review
Yeah but with what verb? Titleize? I'm really not sure how to follow naming conventions on this one.
Yeah, couldn't come up with another verb except for
My current PR does not implement it, but I think it's a good point of discussion.
It would be typed against
I do agree. Footnotes |
Since the input and output matters here, how just calling it |
Would be great if #2387 was included in this as well, if it isn't already. Especially having the Currently in our solution we've had to work around this by authoring all error messages in a way that they can always be appended to a label, and then it's stuck together by an error message component instead. But that's not very flexible, and the error messages sometimes become kind of awkward since it doesn't always make sense to have the label at the start. 😕 |
Great proposal @StephanMeijer ! I think something we haven't discussed yet is whether this metadata should have a well-known structure vs. just allowing arbitrary My hesitation to support having a well-known structure is that then Zod becomes the intermediary between libraries who want to attach metadata, the end users, and between different libraries who might have different goals. Definitely not a hard blocker, since I know other libraries have chosen the well-known structure route, but I think it would be useful to at least discuss the pros and cons from the perspective of Zod itself, library authors, and end users. |
True 🤔 Same issue with
I like this syntax FWIW. Typing it to unknown seems a bit odd in this case and would invariably create stale examples.
I think that idea has been considered and rejected already in #1767
Great point. As far as sample data goes I think it would be useful to have a typed interface based on input- and/or output types, since this could serve as inline documentation in the Zod schema code itself. It's a little harder to see the use-case for label/title, especially with the error message case being rejected. Serving the schema to a custom error message parser could be a future case for this but yeah. However, one thing need not exclude the other. Collecting some well known keys such as the ones described here and having them typed but still allowing for additional keys containing whatever you want and collecting them into a type ZodMeta<Input, Output> = {
title?: string;
description?: string;
inputExamples?: Input[];
outputExamples?: Output[];
[key: string]: unknown;
}; The current Just a thought ofc |
The idea considered and rejected in #1767 was the |
@Svish That would be indeed a proper usecase, Shall I add it to the proposal? |
Please do 😊👍 |
@StefanTerdell I'd rather see an implementation where |
Would be nice to have both if we can't have |
I was looking for something like this a while ago, my goal was to wire up a Zod schema as the source of truth for the validation and labels of a react hook form. Unfortunately there was no way to access the data I needed for this. Some form of metadata would be very useful for advanced use-cases like this. I'm also a fan of codegen, and while I prefer to generate from a static schema (such as OpenAPI) generating TypeScript code is a bit of a minefield so with Zod in a project, it's nice to go the other way around (Zod -> code) and having additional metadata could help here. To play devil's advocate however, this could be considered unnecessary bloat. There's already a way to get the name of a field, so why does a field need a second name defined? ( |
+1. It'd be great to be able to, for example, on the server send through the Schema('s .shape) and then on the UI automatically build a form from that schema. Suggested keys and then custom keys sounds good. Also, how would multiple meta be handled, For example, wouldlet zSecret = z.string().regex(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i)
.meta({ placeholder: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", sensitive: true })
let Foo = z.object({
secret: zSecret.meta({ label: "Secret", description: "Enter your secret", icon: "password" })
}) be automatically merged by Zod, in something like: { foo: { meta: {
placeholder: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", sensitive: true,
label: "Secret", description: "Enter your secret", icon: "password"
} } or kept seperate, in something like: { foo: {
meta: {
label: "Secret", description: "Enter your secret", icon: "password"
},
inner: { meta: {
placeholder: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", sensitive: true,
}
} |
For this use-case, you can set a custom error message as the last argument to most of the schema parts, so in this case you'd set |
Using custom error messages is not really feasible. For consistent error messages, it's much better to use a global error map. The challenge is that error messages, to be accessible, should be referencing the name of the field it belongs to, which is not possible with zod because there's no label and even if it was, it's not available in the error map when generating the error message. In our app we've currently worked around it by writing all error messages in a way that it can be appended to the field label, and then in the error component it will display This kind of works, but isn't very flexible. Would be much better if there was a way to label schemas and a way to access that label in the error map when generating an error for that field. |
It would be great to have some something like |
Having to write messages for every error and rule is not a solution, the solution was to just switch back to Joi untile this is solved properly |
Is there any news about this being implemented yet? |
This would be an fantastic addition! This has my upvote! |
Any update? |
Feature Proposal: Functionalities for adding label and example(s)
Introduction
This proposal aims to introduce new functionalities to Zod, drawing inspiration from the capabilities of Joi. The proposed enhancements will allow users to assign labels as well as ability to set or add input examples. The primary objective of these enhancements is to augment Zod's capabilities, enabling the extraction of vital data necessary for tasks such as constructing OpenAPI specifications.
Detailed Description
1. Setting name 12
The proposed feature will allow users to set names for their schemas. This will provide more context about the data and make it easier for developers to understand the purpose of each schema.
In style of .describe I propose the following:
.name
Use property
name
via the constructor to add aname
property to the resulting schema.This can be useful for documenting a field, for example in a JSON Schema using a library like zod-to-json-schema).
This property is available in the error map when generating error messages.
2. Setting and Adding Example(s) 34
In addition to setting descriptions and labels, the proposed feature will also allow users to set or add examples of input. This will provide a practical illustration of how the schema should be used, making it easier for developers to understand and implement.
In style of .describe I propose the following:
.exemplify
Use
.exemplify()
to add add anexamples
property to the resulting schema.This can be useful for documenting a field, for example in a JSON Schema using a library like zod-to-json-schema).
Benefits
The foremost benefit of this proposal is the enhanced ability to extract data from Zod. This is crucial for tasks such as building OpenAPI specifications. By providing more context about the data (through descriptions and labels) and practical examples of use, developers will find it easier to understand and use the schemas. This, in turn, will lead to more efficient development processes and higher-quality output.
Conclusion
The proposed enhancements to Zod, inspired by Joi, will significantly improve the platform's functionality and usability. By allowing users to set labels and examples of input, we can make Zod more intuitive and effective for developers. This will ultimately lead to better data extraction capabilities, which is crucial for tasks such as building OpenAPI specifications.
Earlier proposals
.describe()
#1443Pull Requests made
exemplify()
and.examples
#2549Points of discussion
notes
?5tags
?6unit
?7Footnotes
For
name
we need other words, as the function cannot be the same as the property being set. ↩Inspired by https://joi.dev/api/?v=17.9.1#anylabelname ↩
I am open for using another term. ↩
Inspired by https://joi.dev/api/?v=17.9.1#anyexampleexample-options ↩
Inspired by https://joi.dev/api/?v=17.9.1#anynotenotes ↩
Inspired by https://joi.dev/api/?v=17.9.1#anytagtags ↩
Inspired by https://joi.dev/api/?v=17.9.1#anyunitname ↩
The text was updated successfully, but these errors were encountered: