Skip to content

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. #14387

@k-wakamatsu-tms

Description

@k-wakamatsu-tms

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

  1. 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"',
        });
      }
    }),
  }),
});
  1. Create a form that submits data to this action with fields named bc.b and bc.c
  2. Submit the form

Expected Behavior

  • Form values should be correctly received as nested object properties
  • The superRefine validation should execute properly
  • Custom validation errors should be displayed when conditions are met

Actual Behavior

  • Form values result in invalid_type errors
  • The superRefine validation 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundAn edge case that only affects very specific usage, but has a trivial workaround (priority)feat: actionsRelated to Astro actions (scope)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions