Add name field to ZodTypeDef
See original GitHub issueI’ve run into a situation where adding a name
field to ZodTypeDef
would be very helpful. I have a library that builds OpenAPI specs based on schemas and am having to deal with stack overflows when supporting lazy schemas. I have mostly got around this by tracking visited lazy schemas and creating References on the second time around, the key parts look like this
const visitedLazies = new Set<string>();
function createSchema(
obj: ReferenceType | Reference<any>,
components: OpenAPIV3.ComponentsObject,
keyName?: string,
): OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined {
let schemaObject: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject | undefined;
const def = obj._def;
if ('typeName' in def) {
switch (def.typeName) {
...
case z.ZodFirstPartyTypeKind.ZodLazy:
if (!keyName) { // handle top level lazy schema ie not within an object. should perhaps throw here instead
schemaObject = createSchema(def.getter(), components);
} else if (visitedLazies.has(keyName)) {
schemaObject = { $ref: `#/components/schemas/${keyName}` };
} else {
visitedLazies.add(keyName);
schemaObject = createSchema(def.getter(), components);
if (schemaObject) {
components.schemas![keyName] = schemaObject;
}
}
break;
...
}
return schemaObject;
The problem here is that keyName
is the name of the key in an object that contains the lazy. If the schema is z.object({ hello: SomeLazySchema })
then keyName = hello
. This is a poor choice because a) the key name is not always the friendliest name to give an OpenAPI component and b) it forces you to make all keys with lazy schemas unique, or you will overwrite them.
I could write a custom zod type that has a name field on it and force lazy schemas to use it. But I’m wondering if this is would be a welcome addition to Zod itself? It’s somewhat similar to adding the description to the schema like https://github.com/colinhacks/zod/commit/e2389b0312fb1b280a90d4bd1f10d099178aec9a.
I’m happy to do the work but wanted to make sure I’m not wasting my time before I do.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top GitHub Comments
@brendo-m unrelated to the issue itself, but in order to solve your main problem of tracking zod schemas, did you try just using the
def
object itself as a key in a(Weak)Map
or as a value in a(Weak)Set
? In theory, the object reference itself shouldn’t change, so you can uniquely identify which schema you already processed. Perhaps you don’t need this name proposal due to this?That being said, I think this issue is still a nice to have feature. But I’d prefer if it was a bit more generic. Why not add a
.meta()
function that can add any arbitrary metadata to any schema? That sounds like it could be useful for a lot of zod-related third-party libraries that may need to add their own info on the schemas. Similar to what @colinhacks described indescription
as aJSON
string, just less awkwardThis issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.