Z
Zod4w ago
Torbjørn

Torbjørn - Hi!We use zod to validate data com...

Hi! We use Zod to validate data coming from sanity CMS. Sanity has this fancy preview mode that uses @vercel/stega a package that encodes invisible strings into its output. This breaks all our z.enums when we enable the preview mode from sanity since they contain much more than just the string.
Solution:
if your goal is to keep the types as if the stega matched ones aren't there you can do something like this ```ts const MyStringSchema = z.union([...
Jump to solution
4 Replies
Torbjørn
TorbjørnOP4w ago
{
"received": "overlay​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌​‌‍​‌‌‍‍‌‍‍‌‌‌​‌‍‌​‍‍‌‍‍‌‌‍​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‍‌‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‍‌‍‌‌‍‌‍‌​‌‍‌‌​‌‌​​‌​‍‌‍‌‌‌​‌‍‌‌‌‍‍‌‌​‌‍​‌‌‌​‌‍‍‌‌‍‌‍‍​‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‌‍​‍‌‍​‌‌​‌‍‌‌‌‌‌‌‌​‍‌‍​​‌​‍‌‌​​‍‌​‌‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‍‌‍‌‍‍‌‌​‌​‌​‌​‍‌‍​‌‌‍‌‍‌‌​​‌​‍​‍‌‌",
"code": "invalid_enum_value",
"options": [
"overlay",
"splitview"
],
"path": [
"hero",
"style"
],
"message": "Invalid enum value. Expected 'overlay' | 'splitview', received 'overlay​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌​‌‍​‌‌‍‍‌‍‍‌‌‌​‌‍‌​‍‍‌‍‍‌‌‍​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‍‌‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‍‌‍‌‌‍‌‍‌​‌‍‌‌​‌‌​​‌​‍‌‍‌‌‌​‌‍‌‌‌‍‍‌‌​‌‍​‌‌‌​‌‍‍‌‌‍‌‍‍​‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‌‍​‍‌‍​‌‌​‌‍‌‌‌‌‌‌‌​‍‌‍​​‌​‍‌‌​​‍‌​‌‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‍‌‍‌‍‍‌‌​‌​‌​‌​‍‌‍​‌‌‍‌‍‌‌​​‌​‍​‍‌‌'"
}
{
"received": "overlay​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌​‌‍​‌‌‍‍‌‍‍‌‌‌​‌‍‌​‍‍‌‍‍‌‌‍​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‍‌‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‍‌‍‌‌‍‌‍‌​‌‍‌‌​‌‌​​‌​‍‌‍‌‌‌​‌‍‌‌‌‍‍‌‌​‌‍​‌‌‌​‌‍‍‌‌‍‌‍‍​‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‌‍​‍‌‍​‌‌​‌‍‌‌‌‌‌‌‌​‍‌‍​​‌​‍‌‌​​‍‌​‌‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‍‌‍‌‍‍‌‌​‌​‌​‌​‍‌‍​‌‌‍‌‍‌‌​​‌​‍​‍‌‌",
"code": "invalid_enum_value",
"options": [
"overlay",
"splitview"
],
"path": [
"hero",
"style"
],
"message": "Invalid enum value. Expected 'overlay' | 'splitview', received 'overlay​​​​‌‍​‍​‍‌‍‌​‍‌‍‍‌‌‍‌‌‍‍‌‌‍‍​‍​‍​‍‍​‍​‍‌​‌‍​‌‌‍‍‌‍‍‌‌‌​‌‍‌​‍‍‌‍‍‌‌‍​‍​‍​‍​​‍​‍‌‍‍​‌​‍‌‍‌‌‌‍‌‍​‍​‍​‍‍​‍​‍​‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‍‌‌‍‍‌‌​‌‍‌‌‌‍‍‌‌​​‍‌‍‌‌‌‍‌​‌‍‍‌‌‌​​‍‌‍‌‌‍‌‍‌​‌‍‌‌​‌‌​​‌​‍‌‍‌‌‌​‌‍‌‌‌‍‍‌‌​‌‍​‌‌‌​‌‍‍‌‌‍‌‍‍​‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‌‍​‍‌‍​‌‌​‌‍‌‌‌‌‌‌‌​‍‌‍​​‌​‍‌‌​​‍‌​‌‍‌​‌‌​‌‌‌‌‍‌​‌‍‍‌‌‍​‍‌‍‌‍‍‌‌‍‌​​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌‌​‌‍‌‌​​‌‍‌‌​‌‌‍‌‍‌​‍‌‍‌‍‍‌‌​‌​​‌‍​‌‌‍‌‌‍‌‌​‍‌‍‌​​‌‍​‌‌‌​‌‍‍​​‌‌‍‍​‌‍‌‌‌​‍‌‍​‍‍‌​‌‌​‌‍‌‌‍​‌‍‌‌​‍‌‍‌‍‍‌‌​‌​‌​‌​‍‌‍​‌‌‍‌‍‌‌​​‌​‍​‍‌‌'"
}
Does anyone have any ideas of how we can keep our string enums and still let the stega strings through? I have tried using unions, but they just give us the string type (Not what we want, but its the easiest)
import { z } from 'zod';
import { VERCEL_STEGA_REGEX } from '@vercel/stega';

const MyStringSchema = z.union([z.literal('hello'), z.literal('world'), z.string().regex(VERCEL_STEGA_REGEX)]);

type TestType1 = z.infer<typeof MyStringSchema>; // string 🥲
import { z } from 'zod';
import { VERCEL_STEGA_REGEX } from '@vercel/stega';

const MyStringSchema = z.union([z.literal('hello'), z.literal('world'), z.string().regex(VERCEL_STEGA_REGEX)]);

type TestType1 = z.infer<typeof MyStringSchema>; // string 🥲
Solution
janglad
janglad4w ago
if your goal is to keep the types as if the stega matched ones aren't there you can do something like this
const MyStringSchema = z.union([
z.literal('hello'),
z.literal('world'),
z.string().regex(/^[a-z]+$/),
]);

type One = z.output<typeof MyStringSchema>;
// ^?

const MyStringSchema2 = z.union([
z.literal('hello'),
z.literal('world'),
z.string().regex(/^[a-z]+$/) as unknown as z.ZodType<never>,
]);

type Two = z.output<typeof MyStringSchema2>;
// ^?
const MyStringSchema = z.union([
z.literal('hello'),
z.literal('world'),
z.string().regex(/^[a-z]+$/),
]);

type One = z.output<typeof MyStringSchema>;
// ^?

const MyStringSchema2 = z.union([
z.literal('hello'),
z.literal('world'),
z.string().regex(/^[a-z]+$/) as unknown as z.ZodType<never>,
]);

type Two = z.output<typeof MyStringSchema2>;
// ^?
`
janglad
janglad4w ago
No description
Torbjørn
TorbjørnOP4w ago
Thank you so much! This is exactly what I was looking for!
Want results from more Discord servers?
Add your server