switz
switz3mo ago

Preventing action from throwing error due to zod schema?

I have trpc creating server actions, fronted by a zod form data schema.
const createDatasetSchema = zfd.formData({
name: zfd.text(
z
.string()
.min(1, 'Name is required')
.max(100, 'Name must be less than 100 characters')
.lowercase()
.regex(/[a-z0-9-_]/)
),
});
const createDatasetSchema = zfd.formData({
name: zfd.text(
z
.string()
.min(1, 'Name is required')
.max(100, 'Name must be less than 100 characters')
.lowercase()
.regex(/[a-z0-9-_]/)
),
});
I don't want this to throw an error, I want to return errors to the frontend form (useActionState) so it can render what the errors are. Is there a good way to do this?
1 Reply
switz
switzOP3mo ago
the reason to use server actions is the form can be progressively enhanced and work without javascript

Did you find this page helpful?