We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
z.ZodType
merge
Hi all,
Following the advice for circular references, I've built this schema for a table-of-contents in the sphinx-external-toc Python package:
import { z } from 'zod'; const TOCTreeOptions = z .object({ caption: z.string(), hidden: z.boolean(), maxdepth: z.number(), numberted: z.boolean(), reversed: z.boolean(), titlesonly: z.boolean(), }) .partial(); const FileEntry = z.object({ file: z.string(), title: z.string().optional(), }); const URLEntry = z.object({ url: z.string(), title: z.string().optional(), }); const GlobEntry = z.object({ glob: z.string(), }); /** Basic TOC Trees **/ type NoFormatSubtreeType = z.infer<typeof TOCTreeOptions> & { entries: z.infer<typeof NoFormatEntry>[]; }; const NoFormatSubtree: z.ZodType<NoFormatSubtreeType> = TOCTreeOptions.extend({ entries: z.lazy(() => NoFormatEntry.array()), }); type NoFormatShorthandSubtreeType = { entries: z.infer<typeof NoFormatEntry>[]; options?: z.infer<typeof TOCTreeOptions>; } const NoFormatShorthandSubtree: z.ZodType<NoFormatShorthandSubtreeType> = z.object({ entries: z.lazy(() => NoFormatEntry.array()), options: TOCTreeOptions.optional(), }); const NoFormatHasSubtrees = z.object({ subtrees: NoFormatSubtree.array(), }); const NoFormatEntry = z.union([ FileEntry.merge(NoFormatShorthandSubtree), FileEntry.merge(NoFormatHasSubtrees), FileEntry, URLEntry, GlobEntry, ]); const NoFormatTOCBase = z.object({ root: z.string(), defaults: TOCTreeOptions.optional(), }); const NoFormatTOC = z.union([ NoFormatTOCBase.merge(NoFormatShorthandSubtree).strict(), NoFormatTOCBase.merge(NoFormatHasSubtrees).strict(), NoFormatTOCBase.strict(), ]);
This fails on the calls to merge that pass NoFormatShorthandSubtree as an argument.
NoFormatShorthandSubtree
I can understand why this fails (ZodType isn't ZodObject), but is there a way to solve this using merge?
ZodType
ZodObject
In my specific case, I can probably drop the strict requirement, and thus use and instead of merge, but it's not ideal!
strict
and
The text was updated successfully, but these errors were encountered:
myst init
Hi, I stumbled upon similar issue too and managed to resolve it using satisfies operator.
satisfies
import { z } from 'zod' type TypeA = { name: string } type TypeB = { surname: string } const objectWithName: z.ZodType<TypeA> = z.object({ name: z.string() }) const objectWithSurname: z.ZodType<TypeB> = z.object({ surname: z.string() }) const merged = objectWithName.merge(objectWithSurname)
The code above throws TS2339: Property merge does not exist on type ZodType<TypeA, ZodTypeDef, TypeA>, but this:
TS2339: Property merge does not exist on type ZodType<TypeA, ZodTypeDef, TypeA>
import { z } from 'zod' type TypeA = { name: string } type TypeB = { surname: string } const objectWithName = z.object({ name: z.string() }) satisfies z.ZodType<TypeA> const objectWithSurname = z.object({ surname: z.string() }) satisfies z.ZodType<TypeB> const merged = objectWithName.merge(objectWithSurname)
works fine. I hope it helps : )
Sorry, something went wrong.
No branches or pull requests
Hi all,
Following the advice for circular references, I've built this schema for a table-of-contents in the sphinx-external-toc Python package:
This fails on the calls to
merge
that passNoFormatShorthandSubtree
as an argument.I can understand why this fails (
ZodType
isn'tZodObject
), but is there a way to solve this usingmerge
?In my specific case, I can probably drop the
strict
requirement, and thus useand
instead ofmerge
, but it's not ideal!The text was updated successfully, but these errors were encountered: