https://git.jolheiser.com/cuesonnet.git
CUE + Jsonnet
import "time"
#Schema: {
// Basic schema
firstName: string
lastName: string
age: int
birthday: string
// Refine as needed
#Title: =~"^[A-Z]" | error("must start with an uppercase letter")
firstName: #Title
lastName: #Title
age: >0
birthday: time.Format("01/02/2006")
// Defaults
gopher: bool | *true
}
// Apply the schema to root
#Schema
{
firstName: 'Jim',
lastName: 'Jimbly',
age: 45,
birthday: '01/02/2003',
}
Embedding the schema is the recommended pattern:
//go:embed schema.cue
var schema cuesonnet.Schema
var result MyStruct
if err := schema.Decode(r, &result); err != nil {
// handle validation errors
}
testdata/ contains a large list of schemas and Jsonnet fixtures covering CUE features (types, constraints, structs, lists, disjunctions, defaults, definitions, composition, stdlib) and Jsonnet features (functions, comprehensions, conditionals, inheritance, multiline strings, self-reference, stdlib).
See cuesonnet_test.go and cuesonnet_decode_test.go.