OneOf Option for Single Entry Field

4 replies
Last updated: Jul 26, 2022
Hi all. I was just thinking of something: while an
array
field has an option
of
to list all possible types to insert (either embed or reference), AFAIK there's no equivalent for a single entry field. For example, type
object
with
oneOf
. Something like this would be very useful to have IMHO:
{
  type: 'object',
  name: 'thing',
  title: 'Thing',
  oneOf: [{ type: 'widget'}, { type: 'tool' }]
}
Jul 21, 2022, 8:05 PM
Like a union type. I would love this.
Jul 21, 2022, 8:27 PM
Maybe ideas-and-feedback? ๐Ÿ™‚
Jul 22, 2022, 7:20 AM
Will crosspost there ๐Ÿ™‚
Jul 22, 2022, 7:22 AM
You can use a selection of things though with an array and a validation of
maxLength
instead:
{
  type: 'array',
  name: 'thing',
  title: 'Thing',
  of: [{ type: 'widget'}, { type: 'tool' }],
  validation: Rule => Rule.max(1)
}
You can also use
options to do some awesome stuff btw.
Jul 26, 2022, 2:30 PM

Sanity โ€“ Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?