Repository navigation
Set meta.description for a specific usage of a schema in an object
#6240
|
In this scenario: const fooUnion = z.union([
barSchema,
zedSchema,
]);
const coolObj = z.object({
aThing: fooUnion.meta({description: 'Usage in the context of coolObj'})
});
// more schema, in which `coolObj` is included
const output = myfinalSchema.toJSONSchema()The json schema shown for but the actual The resulting json schema output then does not contain the description on the The only way to get For instance, I have a Is there anyway to force the description to stick to the property when rendered? It's easy enough to check child properties of |
Replies: 2 comments 3 replies
|
Hi @FoxxMD! I'm Dosu and I'm helping the Zod team. Calling Workaround: use the const output = z.toJSONSchema(myFinalSchema, {
override: ({ jsonSchema, path }) => {
// path is an array like ["coolObj", "aThing"]
const key = path.join(".");
if (key === "coolObj.aThing") {
jsonSchema.description = "Usage in the context of coolObj";
}
// add more path-specific descriptions as needed
},
});The This is currently the most reliable way to attach context-specific descriptions to shared schemas. I'm not 100% sure whether the deduplication stripping the description is considered intentional behavior or a gap — @colinhacks would know best if you'd like a definitive answer on whether Feel free to close this discussion if that answers your question! To reply, just mention @dosu. Docs are dead. Just use Dosu. |
|
I can't reproduce this on zod 4.4.3 — usage-site descriptions survive in every arrangement I tried, including the ones suggested above as the cause. Rather than guess at what's different, here's the harness so you can point at the case that breaks. import { z } from 'zod'
const mkUnion = () => z.union([
z.object({ kind: z.literal('bar'), n: z.number() }),
z.object({ kind: z.literal('zed'), s: z.string() }),
])
const u = mkUnion()
const coolObj = z.object({
aThing: u.meta({ description: 'for a' }),
bThing: u.meta({ description: 'for b' }),
})
console.log(JSON.stringify(z.toJSONSchema(coolObj), null, 2))Results across the variations, checking whether Two things worth drawing out of that. The And So the difference is somewhere in your actual schema rather than in the union-plus-
If you drop your real schema shape into that harness and it reproduces, that's a clean bug report. And I agree with you on the |
I can't reproduce this on zod 4.4.3 — usage-site descriptions survive in every arrangement I tried, including the ones suggested above as the cause. Rather than guess at what's different, here's the harness so you can point at the case that breaks.
Results across the variations, checking whether
aThing/bThingkeep their own