This guide demonstrates how to use the
cuego
API to validate Go values against CUE constraints expressed in
Go struct tags.
Create a Go program using cuego
1
Initialize a Go module, or use an existing one if that’s more suitable for your situation:
TERMINAL
$ go mod init go.example
...
2
Create a main program that uses the cuego
API to perform the validation you require.
You can use this example code as a starting point:
main.go
package main
import (
"fmt"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cuego"
)
// Policy contains fields with struct tags in the "cue" namespace.
// These tags are read by the cuego API, and define field-level CUE constraints.
type Policy struct {
Level int `cue:">10"`
Action string `cue:" \"Allow\" | \"Deny\" "`
}
func main() {
// check uses the cuego API to validate an instance of a Policy against the
// CUE constraints embedded in the type definition. It returns either a
// success message, or one or more errors formatted as a string.
check := func(p Policy) string {
if err := cuego.Validate(p); err != nil {
return errors.Details(err, nil)
}
return "✅ ok"
}
// good is an instance of a Policy that adheres to the type's CUE constraints.
good := Policy{
Level: 100,
Action: "Allow",
}
// bad is an instance of a Policy that violates the type's CUE constraints.
bad := Policy{
Level: 5,
Action: "Bypass",
}
// Display the validation result for each Policy instance.
fmt.Printf("good: %v\n", check(good))
fmt.Printf("bad: %v\n", check(bad))
}
This example code uses cuego
to check two instances of a simple struct type (good
/ bad
)
against CUE constraints embedded in the type definition (Policy
),
and prints the validation result for each instance.
3
Add a dependency on cuelang.org/go
and ensure the Go module is tidy:
TERMINAL
$ go get cuelang.org/go@v0.11.0
...
$ go mod tidy
...
4
Run the program:
TERMINAL
$ go run .
good: ✅ ok
bad: Action: 2 errors in empty disjunction:
Action: conflicting values "Allow" and "Bypass":
<field:>:1:2
Action: conflicting values "Deny" and "Bypass":
<field:>:1:12
Level: invalid value 5 (out of bound >10):
<field:>:1:1
Related content
- Go API:
cuego
– package documentation - Go API:
cue/errors
– package documentation - Tag: go api – pages explaining and exploring CUE’s Go API