Requires CUE v0.15.0 or later

CUE v0.15.0-alpha.1 introduced the “explicitopen” experiment, which greatly simplifies CUE’s concept of closedness.

The trailing ... operator

Using the trailing ... operator explicitly opens the reference to the closed struct that it follows:

file.cue
@experiment(explicitopen) // Enable the experiment.
package example

// The trailing ... operator prevents #Schema's
// closedness being "inherited" by X, and permits
// more fields to be added to X.
X: #Schema...
X: {
	A: "closed?"
	B: "no!"
}
schema.cue
package example

#Schema: {
	A: string
}
TERMINAL
$ cue export --out yaml
X:
  A: closed?
  B: no!

Closedness when embedding

A closed struct now closes the struct in which it’s embedded – unless it’s followed by the trailing ... operator. This means that a schema can now close the top-level of a package:

file.cue
@experiment(explicitopen) // Enable the experiment.
package example

// Only regular fields mentioned in #Schema are
// now permitted at the top-level of this package.
#Schema
A: "closed?"
B: "yes!"
schema.cue
package example

#Schema: {
	A: string
}
TERMINAL
$ cue vet -c
B: field not allowed:
    ./file.cue:8:1

As a result, enabling the experiment means that embedding a value is now exactly equivalent to unifying with that value – and therefore these two CUE snippets are now semantically identical:

embed.cue
@experiment(explicitopen)
A: {
	B
	c: "foo"
}
unify.cue
@experiment(explicitopen)
A: B & {
	c: "foo"
}

Conclusion

The “explicitopen” experiment simplifies how CUE handles closedness, allowing for more flexibility and control when closed structs are referenced. This flexibility now permits the top-level of packages to be closed, allowing them to be fully validated against schemas without permitting additional fields. Use cue fix --exp=explicitopen to update existing CUE automatically.

As with all CUE language experiments, one of this experiment’s aims is to gather feedback from users. Please do join the CUE community and tell us about your experience with the experiment!

  • Reference: cue help experiments – a list of the language experiments that can be enabled or disabled
  • Reference: cue help fix – update CUE code automatically, applying the latest fixes and language experiments
  • Reference: cue help mod edit – update the language version targeted by a module