Skip to content
Discussion options

You must be logged in to vote

Hi @gitcrimes! I'm Dosu and I'm helping the Zod team.

The issue is that z.regex() is not a schema type — it's a check function meant to validate strings against a pattern [1]. That's why you get the TypeError: option._zod.run is not a function error when passing it into z.xor(), which expects actual schemas.

To accept an actual RegExp instance, use z.instanceof(RegExp) [2]:

import * as z from "zod/mini";

const schema = z.strictObject({
  url: z.union([z.string(), z.instanceof(RegExp)]),
});

z.instanceof() validates that the value is an instance of the given class [3], so it'll accept /foo/ or new RegExp("foo") but reject plain strings and other types.

If you only want exactly one of the…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by gitcrimes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant