#Issue with Zod default
3 messages · Page 1 of 1 (latest)
The knowledge sources don’t explicitly describe this exact behavior with .default() on Zod booleans inside content collection schemas, so the following is based on how the Content Collections API and schemas are described, but there is no direct confirmation of your specific case in the docs. [Defining schema]
From the docs we know:
schemais a Zod object that defines and validates the shape ofdatafor each entry. [Content API]getCollection('tags')returns entries whosedataobject is the result of that schema. [getCollection; Querying]
However, the docs do not state whether Zod .default() values are always materialized into the resulting data object when the field is absent in the source frontmatter, or whether defaults are only applied at parse time in some situations.
Because of that, I can’t confidently say why your unlisted default is not appearing in tag.data for entries that omit it; the behavior you’re seeing (only explicitly-set values present) is not clearly documented one way or the other.
What is documented and reliable is:
- Every property you want to rely on should be defined in the schema, and you can treat
dataas matching that schema at the type level. [Defining schema] - If you need a guaranteed boolean at runtime, you can:
- Make the field required in the schema (
z.boolean()) and set it explicitly in all frontmatter, or - Treat it as optional and coerce in your own code (e.g.
const unlisted = tag.data.unlisted ?? false).
- Make the field required in the schema (
Since the docs don’t clarify default materialization, the safest approach within documented behavior is to either:
- Set
unlistedexplicitly in all tag frontmatter, or - Keep it optional in the schema and handle the default yourself when reading
tag.data.