Feedback: Variable frontmatter leads to error #742

Closed
opened 2026-10-05 03:03:09 +02:00 by adam · 1 comment
Owner

The error:

Image

My frontmatter for the cover image is defined via Astro's content.config.ts schema:

    cover: z
      .union([
        z.string(),
        z.object({
          src: z.string(),
          title: z.string().optional(),
        }),
      ])
      .optional(),

which means both of the following are valid:

cover: some-filename.jpg

cover:
  src: some-filename.jpg
  title: a title for the file

In my frontmatter.json, I see no way to configure an "either or" for the field type of the cover:

  "frontMatter.taxonomy.contentTypes": [
    {
      "name": "default",
      ...
      "fields": [
        ...
        {
          "title": "Content preview",
          "name": "cover",
          "type": "image",
          "isPreviewImage": true
        }
      ]
    }
  ]

With this frontmatter.json, the second cover format is invalid (which is okay by me) and throws an error because an expected string does not have a .startsWith method. The field needs to be typecast somehow before applying filters and transformations.

Is there a way to make Frontmatter understand two possible field formats, or have it ignore the field (not validate it)?

Even if not, it should not error loudly with a popup; instead, it should "simply" complain about incorrect frontmatter during the validation of the frontmatter when the file is opened/saved.

The error: ![Image](https://github.com/user-attachments/assets/fdf5af77-002b-403b-b406-fa563ac0570f) My frontmatter for the cover image is defined via Astro's content.config.ts schema: ```ts cover: z .union([ z.string(), z.object({ src: z.string(), title: z.string().optional(), }), ]) .optional(), ``` which means both of the following are valid: ```yaml cover: some-filename.jpg cover: src: some-filename.jpg title: a title for the file ``` In my frontmatter.json, I see no way to configure an "either or" for the field type of the cover: ``` "frontMatter.taxonomy.contentTypes": [ { "name": "default", ... "fields": [ ... { "title": "Content preview", "name": "cover", "type": "image", "isPreviewImage": true } ] } ] ``` With this frontmatter.json, the second `cover` format is invalid (which is okay by me) and throws an error because an expected string does not have a `.startsWith` method. The field needs to be typecast somehow before applying filters and transformations. Is there a way to make Frontmatter understand two possible field formats, or have it ignore the field (not validate it)? Even if not, it should not error loudly with a popup; instead, it should "simply" complain about incorrect frontmatter during the validation of the frontmatter when the file is opened/saved. <!-- github-sync-id:3154881949 -->
adam closed this issue 2026-10-05 03:03:09 +02:00
Author
Owner

@davidsneighbour it will be testable in the latest beta version.

@davidsneighbour it will be testable in the latest beta version. <!-- github-comment-id:4060230895 -->
Sign in to join this conversation.
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: adam/vscode-front-matter#742