-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Description
Astro Info
Astro v5.13.5
Node v20.19.5
System Linux (x64)
Package Manager pnpm
Output static
Adapter @astrojs/node
Integrations astro-page-insight
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When using superRefine or discriminatedUnion for complex validation on nested objects in Astro Actions, form values cannot be properly received from the client side, resulting in invalid_type errors.
While superRefine works correctly at the top level, applying it to nested objects causes form data to not be parsed correctly, even when sent in the proper format (e.g., bc.b).
What's the expected result?
Steps to Reproduce
- Create an Astro Action with a nested object schema using
superRefine:
import { z } from 'zod';
const schema = z.object({
input: z.object({
a: z.string().trim().min(1, 'A is required'),
bc: z.object({
b: z.enum(['hoge', 'huga']),
c: z.string().optional(),
})
.superRefine((data, ctx) => {
if (data.b === 'huga' && (!data.c || data.c.trim() === '')) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ['bc', 'c'],
message: 'C is required when B is "huga"',
});
}
}),
}),
});- Create a form that submits data to this action with fields named
bc.bandbc.c - Submit the form
Expected Behavior
- Form values should be correctly received as nested object properties
- The
superRefinevalidation should execute properly - Custom validation errors should be displayed when conditions are met
Actual Behavior
- Form values result in
invalid_typeerrors - The
superRefinevalidation is not executed - Values are not properly parsed into the nested object structure
Additional Context
This issue appears to be related to how Astro Actions processes form data when dealing with Zod's advanced validation features on nested objects. The problem does not occur when using superRefine at the top level of the schema.
A potential workaround is to restructure the schema to use top-level validation, but this limits the ability to create clean, nested data structures with complex validation rules.
Possible Related Issues
This might be related to issue #13562 regarding astro:actions transform, though the specific case of nested superRefine validation appears to be a distinct problem.
Link to Minimal Reproducible Example
https://stackblitz.com/edit/github-xx95mxcc?file=src%2Fpages%2Findex.astro
Participation
- I am willing to submit a pull request for this issue.