This Commented CUE demonstrates how to disallow specific data fields inside an otherwise open schema.
file.cue
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?: error("field name forbidden")data.yaml
name: Charlie Cartwright
address: Ripon, North Yorkshire
species: cat
age: 15.5TERMINAL
$ cue vet -c . data.yaml
age: field name forbidden:
./file.cue:10:7Another 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.
Related content
- The CUE Tour: Definitions
- Glossary: the
closebuilt-in function - Reference: The CUE Language Specification:
the built-in function
error