encoding/gocodec: Validate helper function

Change-Id: I2365180fbe4d7742a2bd392e5b03d06e11218cb7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6342
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/gocode/gocodec/codec.go b/encoding/gocode/gocodec/codec.go
index 0037d2a..bd4b083 100644
--- a/encoding/gocode/gocodec/codec.go
+++ b/encoding/gocode/gocodec/codec.go
@@ -93,6 +93,30 @@
 	return v.Decode(x)
 }
 
+var defaultCodec = New(&cue.Runtime{}, nil)
+
+// Validate calls Validate on a default Codec for the type of x.
+func Validate(x interface{}) error {
+	c := defaultCodec
+	c.mutex.RLock()
+	defer c.mutex.RUnlock()
+
+	r := defaultCodec.runtime
+	v, err := fromGoType(r, x)
+	if err != nil {
+		return err
+	}
+	w, err := fromGoValue(r, x, false)
+	if err != nil {
+		return err
+	}
+	v = v.Unify(w)
+	if err := v.Validate(); err != nil {
+		return err
+	}
+	return nil
+}
+
 // Validate checks whether x satisfies the constraints defined by v.
 //
 // The given value must be created using the same Runtime with which c was