This Commented CUE demonstrates how to disallow specific data fields inside an otherwise open schema.
package example
// This schema is open because we do not know
// which additional fields might be present.
name!: string
address!: string
// We must not allow our data model to contain
// people's ages, so we disallow the age field.
age?: _|_
name: Charlie Cartwright
address: Ripon, North Yorkshire
species: cat
age: 15.5
$ cue vet . data.yaml
explicit error (_|_ literal) in source:
./file.cue:10:7
Another common way to prevent the existence of unwanted data fields is to rely
on the closedness property that comes from the use of
definitions or the
close
built-in function. However, in situations such as the example above, closedness
cannot be used to disallow specific fields because the schema author cannot
specify each and every acceptable field - and therefore the schema must be left
open.
The technique demonstrated here may be superseded by the error()
builtin
function proposed in issue #943.
This function is not yet available, but would allow for custom error
messages instead of theexplicit error (_|_ literal) in source
shown above.
Related content
- The CUE Tour: Definitions
- Glossary: the
close
built-in function - Issue #943 contains details of the proposed
error()
builtin function