Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

How to validate data against schema in v0.4.0 in Go #1030

Answered by owulveryck
owulveryck asked this question in Q&A
Discussion options

You must be logged in to vote

Here is the solution:

package main

import (
	"log"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/cuecontext"
	"cuelang.org/go/cue/load"
)

func main() {
	ctx := cuecontext.New()

	binst := load.Instances([]string{"definitions.cue", "testdata/sample.cue"}, nil)
	insts, err := ctx.BuildInstances(binst)
	if err != nil {
		log.Fatal(err)
	}
	if len(insts) != 1 {
		log.Fatal("more than one instance")
	}
	v := insts[0]
	opts := []cue.Option{
		cue.Attributes(true),
		cue.Definitions(true),
		cue.Hidden(true),
	}
	err = v.Validate(opts...)
	if err != nil {
		log.Fatal(err)
	}
}

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by owulveryck
Comment options

You must be logged in to vote
4 replies
@owulveryck
Comment options

@owulveryck
Comment options

@myitcv
Comment options

@myitcv
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants