-
|
I have a schema that allows passing a string or a regex. Not a string that's validated by a regex, rather a regex that the user can actually pass into the schema. Something like this (using zod mini): However, this gives an error: I don't see anything in the docs about how to do this. Is there a way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hi @gitcrimes! I'm Dosu and I'm helping the Zod team. The issue is that To accept an actual import * as z from "zod/mini";
const schema = z.strictObject({
url: z.union([z.string(), z.instanceof(RegExp)]),
});
If you only want exactly one of the options to match (the url: z.xor([z.string(), z.instanceof(RegExp)])Both work fine with Feel free to close this discussion if that answers your question! To reply, just mention @dosu. Docs are dead. Just use Dosu. |
Beta Was this translation helpful? Give feedback.
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 theTypeError: option._zod.run is not a functionerror when passing it intoz.xor(), which expects actual schemas.To accept an actual
RegExpinstance, usez.instanceof(RegExp)[2]:z.instanceof()validates that the value is an instance of the given class [3], so it'll accept/foo/ornew RegExp("foo")but reject plain strings and other types.If you only want exactly one of the…