Field Patterns
There are a number of patterns which allow us to add complexity to field constraints and validation.
OneOf
The OneOf pattern is used to enforce one and only one of a set of fields. We use disjunctions and an embedding to create this behavior.
You can also have more options and an empty option if you want to allow none of them to be valid as well.
However, you cannot have options which are subsets of another option.
The following will error when only a
is provided:
CUE cannot distinguish between the choices.
AnyOf
AnyOf is a pattern where we want at least one of a set of fields to be present.
AnyOf requires a validator field to check for the existence of optional fields. The pattern here makes use of
- a set of optional fields that we want 1 or more of.
- the alias
this=
so we can reference the local fields. You can use a different name thanthis
. - a list comprehension over
this
, which useslist.Contains()
for the fields to check. - a definition with
true & list.MinItems
to check that there is at least one of the labels.
Note that we have two variations,
local like ABC
and DE
, and
definition based like XYZ
.
You can also change the minimum required fields if you so desire
by adjusting the second argument to list.MinItems
.
It is also possible to have overlapping sets.
Note, this is a workaround until the required fields and builtins proposals provided a more native method. See cueology/futurology for more details on the proposals.