Access nested metadata in zod4? #4912
Replies: 1 comment
-
|
Yes, you can retrieve an array of metadata for each top-level property by iterating over the object schema's shape and querying the registry for each property's schema instance. In your example, since both Here's how you can do it: const shape = schema.shape;
const metadataArray = Object.values(shape).map(fieldSchema =>
filterRegistry.get(fieldSchema)
);
// Result: [{type: "select"}, {type: "select"}]This works because the registry maps schema instances to their metadata, not property keys or paths. If you reuse the same schema instance, you'll get the same metadata object each time. If you want different metadata per field, you'll need to create separate schema instances and register each one with its own metadata. You can read more about how registries and metadata work in Zod 4 in the official docs and see discussion of this behavior in this issue. If this answers your question, feel free to close the issue! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following nested schema:
Ideally I'm looking for a way to retrieve an array of the top level nested metadata like:
Is that possible?
Beta Was this translation helpful? Give feedback.
All reactions