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

safeParse not working server sider with z.union() : Error: Attempted to call indexOf() from the server but indexOf is on the client. #3570

Open
arnaudpfu opened this issue Jun 13, 2024 · 1 comment

Comments

@arnaudpfu
Copy link

I am working on a next js app with the directory page system.
I have a folder called [[...options]], which enable to get url segments in the url, more details about that here

I would like parse the string[] containing the segments with zod server side.

export const VIEW_OPTIONS = ['day', 'week', 'month', 'year'] as const;

type ViewOption = (typeof VIEW_OPTIONS)[number];
type Segments = ['booking', ViewOption, number, number, number] | [ViewOption, number, number, number];

const calendarOptionsSchema = z.union([
    z.tuple([z.literal('booking'), z.enum(VIEW_OPTIONS), z.number(), z.number(), z.number()]),
    z.tuple([z.enum(VIEW_OPTIONS), z.number(), z.number(), z.number()]),
    z.undefined(),
]);

const extractSegments = (options: Segments) => {
    if (!calendarOptionsSchema.safeParse(options).success) {
        // redirect to 404 page
        notFound();

    }
    // other stuff here
}

I get this issue :

Screenshot 2024-06-13 at 10 48 38

I looked on the internet, I have see nothing about this issue.
If you have an idea/an explanation, you are welcome. :)

@m10rten
Copy link
Contributor

m10rten commented Jun 28, 2024

Hi, do you still have this issue with the latest Next.js version?

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