Skip to content

Zod schema param type is too constrained #2

@glebec

Description

@glebec

Problem

In the Zod docs for Library Authors, it's shown that you must not import from zod, but instead only use one or more of the "blessed" paths like zod/v4/core. See:

The react-markdown-with-mdx project currently imports from Zod (code permalink):

// Bad
import type { ZodType } from "zod";

This overly constrains the validator param (permalink) which makes certain valid user-defined Zod schemas (e.g. those pulled in from zod/v4) throw a type error in practice. Here's an example in the TypeScript playground (note: you'll have to wait a minute for TS to give up on the infinite type loop this causes).

Solution 1: Zod Import Fix

The first solution available is to follow the docs and make sure that the ZodType is only imported from the allowed path for library authors, to guarantee compatibility with user code.

Solution 2: Standard Schema

In addition, there's an argument to be made that instead of requiring Zod specifically, this project should be widened to accept any validator that conforms to the Standard Schema spec. This is explicitly mentioned in the Zod docs here:

If you're building a library that accepts user-defined schemas to perform black-box validation, you may not need to integrate with Zod specifically. Instead look into Standard Schema. It's a shared interface implemented by most popular validation libraries in the TypeScript ecosystem (see the full list), including Zod.

This spec works great if you accept user-defined schemas and treat them like "black box" validators. Given any compliant library, you can extract inferred input/output types, validate inputs, and get back a standardized error.

Since all that react-markedown-with-mdx does with the validator is call parse (permalink), this could be changed to call the standard schema validate method, and then throw a validation error on failure. Then you'd be able to drop the Zod peer deps entirely.

Conclusion / Proposal

I think this project should:

  1. Be updated to use the correct Zod paths for library authors, to ensure compatibility with user-defined Zod schemas
  2. Optionally, consider also updating the validator param to be any Standard Schema validator, to remove the Zod peer dep entirely

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions