Leverage Zod's power
Astro ^4.0.0
Zod is a TypeScript-first schema validation with static type inference. It’s used in many parts of Astro and can help you achieve more advanced behavior.
A common place where Astronauts want more customization is in the Content Collections config. We’ll look at 3 powerful Zod features that cover a few basic use-cases.
preprocess
People often want a bit more customization when referencing images inside their frontmatter.
Say if you’re using Path Aliases to reference your assets, and would like to automatically prepend an aliased asset path to your images:
This can be easily be done using preprocess
!
transform
You may not have the data in the shape you want in the frontmatter but you don’t want (or can’t) change
all your entries. This is where transform
comes in! This transform will
allow us to you use those coordinates conveniently with library like mapbox-gl
:
refine
You may want to add extra validation to your frontmatter. For example, what if you want to make sure your
images are big enough? Let’s have a look at refine
. You can set a condition
that should be met, and a message that will be shown if not!
Conclusion
We’ve only scratched the surface! Have a look at the official Zod docs to discover more powerful APIs!