Skip to content
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

Zod compatibility #7953

Closed
pcolmer opened this issue Apr 19, 2024 · 3 comments
Closed

Zod compatibility #7953

pcolmer opened this issue Apr 19, 2024 · 3 comments
Labels
bug Something on the site isn't working

Comments

@pcolmer
Copy link

pcolmer commented Apr 19, 2024

📝 Issue Description

The documentation states:

To use Zod in Astro, import the z utility from "astro:content". This is a re-export of the Zod library, and it supports all of the features of Zod. See Zod’s README for complete documentation on how Zod works and what features are available.

Zod's README gives the following as an example:

z.string().date({ message: "Invalid date string!" });

However, in Astro, z.string().date() is not valid - it has to be z.date(). Furthermore, it is not possible to use message. It looks like I can use invalid_type_error instead, e.g.:

date_tested: z.date({ invalid_type_error: "Invalid date format. Must be YYYY-MM-DD"}).optional(),

but when I try to build a site that might have date_tested defined as, say

date_tested: 1/8/2024

I get the error date_tested: Expected type "date", received "string" instead of my custom error.

📋 On which page(s) it occurs

https://docs.astro.build/en/guides/content-collections/

🤔 Expected Behavior

The documentation needs to make clear that Zod's README is not authoritative and make it clear, for example, that z.string().date() is not supported and to use z.date() instead. I do realise that the existing documentation gives an example of z.date() (which is how I figured it out) but it needs to be clearer because of the "supports all of the features of Zod" statement.

Furthermore, the issue about the custom error message needs to be cleared up in the documentation if it isn't supported.

👀 Current Behavior

See issue description.

🖥️ Browser

Edge (Chromium version)

📄 Additional Information

No response

@pcolmer pcolmer added the bug Something on the site isn't working label Apr 19, 2024
@Princesseuh
Copy link
Member

z.string().date() was added in Zod 3.23, which is not released yet.

@pcolmer
Copy link
Author

pcolmer commented Apr 23, 2024

z.string().date() was added in Zod 3.23, which is not released yet.

OK - thank you.

In the absence of having Zod 3.23, is there a solution to getting a custom error message returned? I don't think users contributing to the data in my repo will find it easy to understand date_tested: Expected type "date", received "string" which is why I wanted to explain that the string needs to be in the specific format YYYY-MM-DD.

@sarah11918
Copy link
Member

Unfortunately, I don't think it's possible to change that error message because it informs that the value found is of the wrong type.

I believe YYYY-MM-DD is the YAML date format, not a string. It's not that a "string" is in the wrong format, but rather that a date format is expected.

So, I will close this issue as I don't think there is anything actionable for docs here, and maybe you can use your contributing guide to provide/highlight the YAML date spec to your contributors to help them with the date format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something on the site isn't working
Projects
None yet
Development

No branches or pull requests

3 participants