-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Actions: add discriminated union support (#11939)
* feat: discriminated union for form validators * chore: changeset
- Loading branch information
1 parent
0d50d75
commit 7b09c62
Showing
4 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds support for Zod discriminated unions on Action form inputs. This allows forms with different inputs to be submitted to the same action, using a given input to decide which object should be used for validation. | ||
|
||
This example accepts either a `create` or `update` form submission, and uses the `type` field to determine which object to validate against. | ||
|
||
```ts | ||
import { defineAction } from 'astro:actions'; | ||
import { z } from 'astro:schema'; | ||
|
||
export const server = { | ||
changeUser: defineAction({ | ||
accept: 'form', | ||
input: z.discriminatedUnion('type', [ | ||
z.object({ | ||
type: z.literal('create'), | ||
name: z.string(), | ||
email: z.string().email(), | ||
}), | ||
z.object({ | ||
type: z.literal('update'), | ||
id: z.number(), | ||
name: z.string(), | ||
email: z.string().email(), | ||
}), | ||
]), | ||
async handler(input) { | ||
if (input.type === 'create') { | ||
// input is { type: 'create', name: string, email: string } | ||
} else { | ||
// input is { type: 'update', id: number, name: string, email: string } | ||
} | ||
}, | ||
}), | ||
} | ||
``` | ||
|
||
The corresponding `create` and `update` forms may look like this: | ||
|
||
```astro | ||
--- | ||
import { actions } from 'astro:actions'; | ||
--- | ||
<!--Create--> | ||
<form action={actions.changeUser} method="POST"> | ||
<input type="hidden" name="type" value="create" /> | ||
<input type="text" name="name" required /> | ||
<input type="email" name="email" required /> | ||
<button type="submit">Create User</button> | ||
</form> | ||
<!--Update--> | ||
<form action={actions.changeUser} method="POST"> | ||
<input type="hidden" name="type" value="update" /> | ||
<input type="hidden" name="id" value="user-123" /> | ||
<input type="text" name="name" required /> | ||
<input type="email" name="email" required /> | ||
<button type="submit">Update User</button> | ||
</form> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters