-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add section on Zod 4 to v6 migration guide #12848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v6
Are you sure you want to change the base?
Conversation
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
✅ Deploy Preview for astro-docs-2 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Thanks, @matthewp ! Also checking that none of these examples here where we describe using Zod for content collections look outdated to you? (This whole section on "Defining the collection schema" including the subsections for defining data types and collection references): https://deploy-preview-12322--astro-docs-2.netlify.app/en/guides/content-collections/#defining-datatypes-with-zod We also do mention and show examples of Zod schemas on the actions page, but it's probably the two sections here about validation where we have text/examples that we should double check are still accurate for Zod 4: https://deploy-preview-12322--astro-docs-2.netlify.app/en/guides/actions/#using-validators-with-form-inputs Lastly, even though Zod's migration guide shows everything, I would suspect there would be some changes that are common that most people would have to make to content collections schemas that are worth showing here, a way of focusing their attention on things that they'd very likely have to deal with. Can you suggest what some of those might be? If for example there were one or two things ONLY thing that will affect the majority of people, but maybe that's all they need to do, it would be much better for us to show those here. If it would be possible to help a number of people avoid needing to go to Zod's guide at all, then we really should do that. |
|
In addition to what Sarah just said, I noticed another change in the implementation regarding the use of the -heroImage: image().optional(),
+heroImage: z.optional(image()),Does this mean that |
Yes, I wasn't sure if updating other docs was in scope for these PRs or not, since in the last one it was not. Happy to update other docs though, just tell me.
Not really, no. I can only say what was common in updating Astro itself, but I wouldn't presume that holds over to CC users. For example |
It should still be chainable, I'll check to confirm. |
|
Just noting that we're holding on this one as some aspects of the implementation PR might determine what kind of guidance is needed here! |
|
@sarah11918 Which aspects? So I can unblock if possible. |
|
@matthewp We were waiting on whether or not |
|
Seeing #12866 and that some of our Action code samples had to update makes me think that mentioning some of Zod's changes ourself is probably helpful. Expecially if people have followed our code examples, and we are updating examples in docs, that's probably a good indication of what's common enough to include as some additional helpful content. So, in the Actions guide, we saw our own examples update from
Presumably the |
|
I think what you mentioned could be the most commonly used. Except that, maybe customizing errors? For people using Zod on the client (e.g. a form) and having implemented i18n, they probably customized the error messages because every errors were in English in Zod 3. The And, yes, we should mention Actions schema in addition to content collections! |
|
Do I need to update the examples/docs in this PR to their proper v4 usage? |
|
Hi @matthewp , sorry there hasn't been a lot of direct actionable instructions on this PR as Armand and I have kind of been discussing what might need to be said! I have taken a quick look through all the content collections material we show and I think I only saw one stray So here's what you can do:
I think that's all we'll do for now. As we notice people trying the beta and maybe struggling with the upgrade on certain things, we might later decide to add more specific guidance here. |
|
|
||
| #### What should I do? | ||
|
|
||
| If you have custom Zod schemas in your `content.config.ts` or other configuration files, you'll need to update them for Zod 4. Refer to the [Zod migration guide](https://zod.dev/v4/changelog) for detailed changes in the Zod API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| If you have custom Zod schemas in your `content.config.ts` or other configuration files, you'll need to update them for Zod 4. Refer to the [Zod migration guide](https://zod.dev/v4/changelog) for detailed changes in the Zod API. | |
| If you have custom Zod schemas in your `content.config.ts` or other configuration files, you'll need to update them for Zod 4. Refer to the [Zod migration guide](https://zod.dev/v4/changelog) for detailed changes in the Zod API. | |
| Notably, many `string()` formats have been deprecated (e.g. `z.string().email()`, `z.string.url()`), and their APIs have been moved to the top-level `z` namespace. You may need to update how you validate form input for your Astro Actions: | |
| ```ts title="src/actions/index.ts" ins={8} del={7} | |
| import { defineAction } from 'astro:actions'; | |
| import { z } from 'astro/zod'; | |
| export const server = { | |
| newsletter: defineAction({ | |
| accept: 'form', | |
| input: z.object({ | |
| email: z.string().email(), | |
| email: z.email(), | |
| terms: z.boolean(), | |
| }), | |
| handler: async ({ email, terms }) => { /* ... */ }, | |
| }) | |
| } | |
| ``` |
|
|
||
| If you have custom Zod schemas in your `content.config.ts` or other configuration files, you'll need to update them for Zod 4. Refer to the [Zod migration guide](https://zod.dev/v4/changelog) for detailed changes in the Zod API. | ||
|
|
||
| You can import Zod from `astro/zod` to ensure you're using the same version of Zod that Astro uses internally: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| You can import Zod from `astro/zod` to ensure you're using the same version of Zod that Astro uses internally: | |
| You can ensure you're the same version of Zod that Astro uses internally by [importing Zod from `astro/zod`](#deprecated-astroschema-and-z-from-astrocontent). |
This doesn't have quite the same nuance you were getting at, but I think for our intended audience it's clear guidance that will they can follow for a successful upgrade and it also links to the other entry about Zod imports where we've deprecated importing from other places. So I think it's good "upgrade material."
| import { z } from 'astro/zod'; | ||
| ``` | ||
|
|
||
| <ReadMore>See more about [defining collection schemas with Zod](/en/guides/content-collections/#defining-datatypes-with-zod).</ReadMore> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| <ReadMore>See more about [defining collection schemas with Zod](/en/guides/content-collections/#defining-datatypes-with-zod).</ReadMore> | |
| <ReadMore>See more about [the `astro/zod` module](/en/reference/modules/astro-zod/).</ReadMore> |
Thanks to Armand, we have a much better link now! 🎉
Description (required)
Adds Zod 4 under the "Dependency Upgrades" section.
Mostly just refers to the Zod 4's own migration guide.
Related issues & labels (optional)
For Astro version:
6.x. See astro PR #14956.