Hi!
I see many times passing form's inputs as FormData to the server action and perfomr a server-side validation with Zod. I understand the importance of validating the values both client and server side, but why not passing the values as z.infer<typeof whateverSchema>,?
Like in this case ( I use React Hook Form for client validation);
client
const onSubmit = async (values: z.infer<typeof updateUserSchema>) => {
const { success, error } = await updateUserAction(updatedValues); // PASSING IT AS VALID ZOD SCHEMA
}
server:
export const updateUserAction = async (
formData: z.infer<typeof updateUserSchema>,
// TODO: return the updatedProfile if possible
// ): Promise<ServerActionResponse<Tables<"users">>> => {
): Promise<ServerActionResponse> => {
// TODO: add server-side validation
const { success, error, data } = updateUserSchema.safeParse(formData);
if (error) {
return {
success,
error: error.message,
};
}
// Continue logic if successful...
}
What am I missing? Should i use this signature instead?
export const updateUserAction = async (
formData: FormData,
// TODO: return the updatedProfile if possible
// ): Promise<ServerActionResponse<Tables<"users">>> => {
): Promise<ServerActionResponse> => {
// TODO: add server-side validation
const { success, error, data } = updateUserSchema.safeParse(formData);
if (error) {
return {
success,
error: error.message,
};
}
// Continue logic if successful...
}