estruyf / vscode-front-matter

Front Matter is a CMS running straight in Visual Studio Code. Can be used with static site generators like Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...

Home Page:https://frontmatter.codes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhancement: `Union` field type for more complex schemas

theinfinit opened this issue · comments

Is your feature request related to a problem? Please describe.

Hi Elio,
I searched through issues, but haven't found anything relevant. I'm testing your extension with Astro (Starlight). Does the Front Matter CMS allows to describe the union type?

In Zod, it's described like this:

tableOfContents: z.union([
    z.strictObject({
        minHeadingLevel: z.number().int().min(1).max(6).optional().default(2),
        maxHeadingLevel: z.number().int().min(1).max(6).optional().default(3),
    }),
    z.boolean().transform((enabled) => (enabled ? defaults : false)),
])

Describe the solution you'd like

The ability to describe unions would be great.

Describe alternatives you've considered

I tried to use the fieldGroups, but without success:

Video.mp4

Additional context

I tried the AI assistant on Front Matter's website and it hallucinated a solution 😆

To create a Front Matter field that allows the tableOfContents to be either a boolean or an object with specific properties, you can use the union field type. This type allows you to define multiple types for a single field. Below is an example of how you can define such a field in your Front Matter configuration:

{
  "name": "tableOfContents",
  "title": "Table of content settings",
  "type": "union",
  "types": [
    {
      "type": "boolean"
    },
    {
      "type": "object",
      "fields": [
        {
          "name": "minHeadingLevel",
          "title": "Minimum heading level",
          "type": "number",
          "numberOptions": {
            "min": 2,
            "max": 6
          }
        },
        {
          "name": "maxHeadingLevel",
          "title": "Maximum heading level",
          "type": "number",
          "numberOptions": {
            "min": 2,
            "max": 6
          }
        }
      ]
    }
  ]
}

In this configuration:

The type is set to union, which allows the field to accept multiple types.
The types array includes two types:

  • A boolean type.
  • An object type with the fields minHeadingLevel and maxHeadingLevel.

This setup ensures that the tableOfContents field can either be a boolean or an object with the specified properties.