Skip to content

Enhancement: Union field type for more complex schemas #807

Open
@theinfinit

Description

@theinfinit

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions