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

Error when using .optional() with .refine() with Zod #406

Open
1 task done
Ennoriel opened this issue Apr 10, 2024 · 3 comments
Open
1 task done

Error when using .optional() with .refine() with Zod #406

Ennoriel opened this issue Apr 10, 2024 · 3 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@Ennoriel
Copy link

  • Before posting an issue, read the FAQ and search the previous issues.

Description
On a zod schema, when using .optional() with .refine(), the method superValidate(zod(schema) fails with error:

Async refinement encountered during synchronous parse operation. Use .parseAsync instead.

If applicable, a MRE

// file: +page.server.ts
import { superValidate } from "sveltekit-superforms/server";
import { zod } from "sveltekit-superforms/adapters";
import { z } from "zod";

const schema = z.object({
  name: z.string().min(1),
  email: z
    .string()
    .optional()
    .refine(async () => Promise.resolve(true)),
});

export const load = async () => {
  return { form: await superValidate(zod(schema)) };
};
// file: +page.svelte
// empty, not even implemented
@Ennoriel Ennoriel added the bug Something isn't working label Apr 10, 2024
@ciscoheat
Copy link
Owner

That is strange, since safeParseAsync is used in the adapter. Does it work when you remove optional?

@ciscoheat ciscoheat added the question Further information is requested label Apr 12, 2024
@Ennoriel
Copy link
Author

Ennoriel commented Apr 15, 2024

Thank you for your message :)

Yes, it works when removing .optional()

@ciscoheat
Copy link
Owner

It looks like a problem with Zod and the JSON Schema generator: colinhacks/zod#1460

@ciscoheat ciscoheat added documentation Improvements or additions to documentation and removed question Further information is requested labels May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants