Problems migrating to v1 #515
-
I am working through the migration to v1 and I'm stuck on migrating async validation. My old code looked like this: const submission = await parse(formData, {
schema: (intent) =>
schema.transform(async (data, ctx) => {
if (intent !== "submit") return { ...data, user: null };
const user = await verifyLogin(data.email, data.password);
if (!user) {
ctx.addIssue({
path: ["password"],
code: z.ZodIssueCode.custom,
message: "Invalid username or password",
});
return z.NEVER;
}
return { ...data, user };
}),
async: true,
}); My new code looks like this: const submission = await parseWithZod(formData, {
schema: (intent) =>
schema.transform(async (data, ctx) => {
console.log("intent", intent);
if (intent?.type !== "validate") return { ...data, user: null };
const user = await verifyLogin(data.email, data.password);
console.log(user);
if (!user) {
ctx.addIssue({
path: ["password"],
code: z.ZodIssueCode.custom,
message: "Invalid username or password",
});
return z.NEVER;
}
return { ...data, user };
}),
async: true,
}); I'm logging the intent but it's always null and I can't find anything relating to that in the docs. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Sorry for the confusion. The So I would imagine the equivalent implementation on v1 would be: const submission = await parse(formData, {
schema: (intent) =>
schema.transform(async (data, ctx) => {
if (intent !== null) return { ...data, user: null };
const user = await verifyLogin(data.email, data.password);
if (!user) {
ctx.addIssue({
path: ["password"],
code: z.ZodIssueCode.custom,
message: "Invalid username or password",
});
return z.NEVER;
}
return { ...data, user };
}),
async: true,
}); |
Beta Was this translation helpful? Give feedback.
Sorry for the confusion. The
intent
is considerednull
now on submit as you are likely submitting through a button with no explicit name / value pair describing the intent.So I would imagine the equivalent implementation on v1 would be: