Next.js Discord

Discord Forum

`$ACTION_ID` added to FormData, zod validation

Unanswered
Nick posted this in #help-forum
Open in Discord
I'm passing a FormData object to a server action and trying to validate it with zod.

The form data has a username, password, and confirmPassword field.

The object is appearing like this:
{
  '$ACTION_ID_3d4b79c8f967c88b3fbcd65b3a5e75c2df291999': '',
  username: 'nick',
  password: 'password',
  confirmPassword: 'password',
}

I'm using strict() in the schema, so no additional fields can appear when passing the object. Not sure how to tackle this tbh?

export const SignUpSchema = z
  .object({
    username: z.string().min(2).max(50),
    password: z
      .string()
      .min(8, { message: "Password must be at least 8 characters long" }),
    confirmPassword: z
      .string()
      .min(8, { message: "Password must be at least 8 characters long" }),
  })
  .strict()
  .refine((data) => data.password === data.confirmPassword, {
    message: "Passwords do not match",
    path: ["confirmPassword"],
  });


export const signUp = async (formData: FormData) => {
    SignUpSchema.safeParse(Object.fromEntries(formData.entries()))
}

2 Replies