Requires CUE v0.12.0 or later
CUE’s
file embedding
feature provides a flexible way to validate the contents of data files, and to
record the precise constraints that should be used to validate each file in the
future.
File embedding is an alternative to using
the --path
/-l
flag
to specify data files’ locations using command parameters.
Here’s how to use it to validate data files:
// Enable file embedding.
@extern(embed)
package validate
// Embed the contents of some data files and
// constrain their contents using two schemas.
alpha: #A @embed(file=data/alpha.json)
beta: #B @embed(file=data/beta.yaml)
package validate
// #A permits two fields with integer values.
#A: {X: int, O: int}
// #B extends #A by requiring each field's
// value to be greater than 100.
#B: #A & {[_]: >100}
{
"X": 1,
"O": 2.2
}
X: 11
O: 222
TERMINAL
$ cue vet -c .:validate
alpha.O: conflicting values 2.2 and int (mismatched types float and int):
./constraints.cue:4:17
data/alpha.json:3:10
beta.X: invalid value 11 (out of bound >100):
./constraints.cue:8:16
data/beta.yaml:1:4
The file embedding in this example validates a pair of individually named files, but can also be used to reference multiple files using filesystem globs. See Embedding files in a CUE evaluation for more detail.
File embedding can only be used inside a module, and works with CUE v0.12.0 or later.
Related content
- Reference: cue help embed
- How-to Guide: Embedding files in a CUE evaluation
- Concept Guide: Modules