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 aboolean
or anobject
with specific properties, you can use theunion
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 tounion
, which allows the field to accept multiple types.
Thetypes
array includes two types:
- A
boolean
type.- An
object
type with the fieldsminHeadingLevel
andmaxHeadingLevel
.This setup ensures that the
tableOfContents
field can either be aboolean
or anobject
with the specified properties.