Skip to content
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 Request] - Schema ID anchor property #4024

Open
thequinndev opened this issue Mar 8, 2025 · 0 comments
Open

[Feature Request] - Schema ID anchor property #4024

thequinndev opened this issue Mar 8, 2025 · 0 comments

Comments

@thequinndev
Copy link

To clarify, I'm not referring to a new primitive schema like the string().uuid().

I agree that previous requests for things like .meta() or .examples() are out of scope for something like zod. However, I've found very useful satellite libraries that have gotten around this limitation by extending the schema (or leveraging .describe()?). Their existence appears to show there is a need for some way to internally anchor, link and document schemas, and they have found their own workarounds. Without this I will probably also have to leverage .describe(), which I'm not a fan of. You can solve the problem of extensibility by abstracting zod into a higher level function, as suggested here: #273, however this won't provide a persistent anchor point physically inside the schema.

What I would like to request is a minimal field similar to .describe() that provides a unique string identifier to the schema. As an example something like .identifiedBy(...), this will be present in the _def field like the description (the name isn't important if the concept isn't good):

const exampleAnchorField = z.string().identifiedBy('ExampleField')
const exampleNormalField = z.string()

const example = z.object({
     // I will be able to introspect the schema and find its unique field in the _def
     exampleAnchorField: exampleAnchorField,

     // This will just become just another string field and I lose scope for documenting and lifecycle purposes
     exampleNormalField: exampleNormalField
}).identifiedBy('Example')

Having this internal anchor point for a schema would be very useful for documenting schemas down their lifecycle if I use them in multiple places or derive from them. I have no issue with separating my metadata and examples into abstracted functions from zod, but it would be a more streamlined approach if I could know who was who at the schema level.

I'm hopeful something like this is more agreeable than a blanket metadata field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant