pkg/encoding: add {yaml.json}.Validate

This allows creating types of embedded
YAML and JSON text that most conform
to the given CUE constraints.

Change-Id: I74e07f2f110251bd9c48feafd82b0a969a8ee2c1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2876
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/yaml/yaml.go b/encoding/yaml/yaml.go
index 24f0f18..4339e7d 100644
--- a/encoding/yaml/yaml.go
+++ b/encoding/yaml/yaml.go
@@ -23,6 +23,7 @@
 	"cuelang.org/go/cue"
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/internal/third_party/yaml"
+	pkgyaml "cuelang.org/go/pkg/encoding/yaml"
 	goyaml "github.com/ghodss/yaml"
 )
 
@@ -98,3 +99,10 @@
 	}
 	return buf.Bytes(), nil
 }
+
+// Validate validates the YAML and confirms it matches the constraints
+// specified by v. For YAML streams, all values must match v.
+func Validate(b []byte, v cue.Value) error {
+	_, err := pkgyaml.Validate(b, v)
+	return err
+}