withastro / docs

Astro documentation
https://docs.astro.build/
MIT License
1.32k stars 1.48k forks source link

Zod compatibility #7953

Closed pcolmer closed 5 months ago

pcolmer commented 6 months ago

๐Ÿ“ 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

Princesseuh commented 6 months ago

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

pcolmer commented 5 months ago

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 commented 5 months ago

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?