Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Union field type for more complex schemas #807

Open
theinfinit opened this issue May 17, 2024 · 0 comments
Open

Enhancement: Union field type for more complex schemas #807

theinfinit opened this issue May 17, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@theinfinit
Copy link

theinfinit commented May 17, 2024

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.

@theinfinit theinfinit added the enhancement New feature or request label May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant