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

Unable to pass z.ZodType as argument to merge #3510

Open
agoose77 opened this issue May 22, 2024 · 1 comment
Open

Unable to pass z.ZodType as argument to merge #3510

agoose77 opened this issue May 22, 2024 · 1 comment

Comments

@agoose77
Copy link

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.

I can understand why this fails (ZodType isn't ZodObject), but is there a way to solve this using merge?

In my specific case, I can probably drop the strict requirement, and thus use and instead of merge, but it's not ideal!

@danielBorowik
Copy link

Hi, I stumbled upon similar issue too and managed to resolve it using satisfies operator.

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:

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 : )

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

2 participants