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/cue/build.go b/cue/build.go
index 013e029..c527847 100644
--- a/cue/build.go
+++ b/cue/build.go
@@ -37,7 +37,16 @@
 
 func init() {
 	internal.GetRuntime = func(instance interface{}) interface{} {
-		return &Runtime{idx: instance.(*Instance).index}
+		switch x := instance.(type) {
+		case Value:
+			return &Runtime{idx: x.idx}
+
+		case *Instance:
+			return &Runtime{idx: x.index}
+
+		default:
+			panic("argument must be Value or *Instance")
+		}
 	}
 
 	internal.CheckAndForkRuntime = func(runtime, value interface{}) interface{} {
diff --git a/cue/builtin_test.go b/cue/builtin_test.go
index 5b2da99..a2c9c8a 100644
--- a/cue/builtin_test.go
+++ b/cue/builtin_test.go
@@ -77,6 +77,18 @@
 		test("encoding/hex", `hex.Decode("foo")`),
 		`_|_(error in call to encoding/hex.Decode: encoding/hex: invalid byte: U+006F 'o')`,
 	}, {
+		test("encoding/json", `json.Validate("{\"a\":10}", {b:string})`),
+		`true`,
+	}, {
+		test("encoding/json", `json.Validate("{\"a\":10}", {a:<3})`),
+		`_|_(error in call to encoding/json.Validate: invalid value 10 (out of bound <3))`,
+	}, {
+		test("encoding/yaml", `yaml.Validate("a: 2\n---\na: 4", {a:<3})`),
+		`_|_(error in call to encoding/yaml.Validate: invalid value 4 (out of bound <3))`,
+	}, {
+		test("encoding/yaml", `yaml.Validate("a: 2\n---\na: 4", {a:<5})`),
+		`true`,
+	}, {
 		test("strconv", `strconv.FormatUint(64, 16)`),
 		`"40"`,
 	}, {
diff --git a/cue/builtins.go b/cue/builtins.go
index 83e0679..40d446f 100644
--- a/cue/builtins.go
+++ b/cue/builtins.go
@@ -32,6 +32,7 @@
 
 	"cuelang.org/go/cue/literal"
 	"cuelang.org/go/cue/parser"
+	"cuelang.org/go/internal"
 	"cuelang.org/go/internal/third_party/yaml"
 	"github.com/cockroachdb/apd/v2"
 	goyaml "github.com/ghodss/yaml"
@@ -522,6 +523,29 @@
 					return expr, nil
 				}()
 			},
+		}, {
+			Name:   "Validate",
+			Params: []kind{stringKind, topKind},
+			Result: boolKind,
+			Func: func(c *callCtxt) {
+				b, v := c.bytes(0), c.value(1)
+				c.ret, c.err = func() (interface{}, error) {
+					if !json.Valid(b) {
+						return false, fmt.Errorf("json: invalid JSON")
+					}
+					r := internal.GetRuntime(v).(*Runtime)
+					inst, err := r.Compile("json.Validate", b)
+					if err != nil {
+						return false, err
+					}
+
+					v = v.Unify(inst.Value())
+					if v.Err() != nil {
+						return false, v.Err()
+					}
+					return true, nil
+				}()
+			},
 		}},
 	},
 	"encoding/yaml": &builtinPkg{
@@ -572,6 +596,38 @@
 					return yaml.Unmarshal("", data)
 				}()
 			},
+		}, {
+			Name:   "Validate",
+			Params: []kind{stringKind, topKind},
+			Result: boolKind,
+			Func: func(c *callCtxt) {
+				b, v := c.bytes(0), c.value(1)
+				c.ret, c.err = func() (interface{}, error) {
+					d, err := yaml.NewDecoder("yaml.Validate", b)
+					if err != nil {
+						return false, err
+					}
+					r := internal.GetRuntime(v).(*Runtime)
+					for {
+						expr, err := d.Decode()
+						if err != nil {
+							if err == io.EOF {
+								return true, nil
+							}
+							return false, err
+						}
+
+						inst, err := r.CompileExpr(expr)
+						if err != nil {
+							return false, err
+						}
+
+						if x := v.Unify(inst.Value()); x.Err() != nil {
+							return false, x.Err()
+						}
+					}
+				}()
+			},
 		}},
 	},
 	"html": &builtinPkg{
diff --git a/encoding/json/json.go b/encoding/json/json.go
new file mode 100644
index 0000000..31335db
--- /dev/null
+++ b/encoding/json/json.go
@@ -0,0 +1,28 @@
+// Copyright 2019 CUE Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package yaml converts JSON to and from CUE.
+package json
+
+import (
+	"cuelang.org/go/cue"
+	"cuelang.org/go/pkg/encoding/json"
+)
+
+// Validate validates JSON and confirms it matches the constraints
+// specified by v.
+func Validate(b []byte, v cue.Value) error {
+	_, err := json.Validate(b, v)
+	return err
+}
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
+}
diff --git a/pkg/encoding/json/manual.go b/pkg/encoding/json/manual.go
index 4f1ac41..6ba9956 100644
--- a/pkg/encoding/json/manual.go
+++ b/pkg/encoding/json/manual.go
@@ -22,6 +22,7 @@
 	"cuelang.org/go/cue"
 	"cuelang.org/go/cue/ast"
 	"cuelang.org/go/cue/parser"
+	"cuelang.org/go/internal"
 )
 
 // Compact generates the JSON-encoded src with insignificant space characters
@@ -101,3 +102,22 @@
 	}
 	return expr, nil
 }
+
+// Validate validates JSON and confirms it matches the constraints
+// specified by v.
+func Validate(b []byte, v cue.Value) (bool, error) {
+	if !json.Valid(b) {
+		return false, fmt.Errorf("json: invalid JSON")
+	}
+	r := internal.GetRuntime(v).(*cue.Runtime)
+	inst, err := r.Compile("json.Validate", b)
+	if err != nil {
+		return false, err
+	}
+
+	v = v.Unify(inst.Value())
+	if v.Err() != nil {
+		return false, v.Err()
+	}
+	return true, nil
+}
diff --git a/pkg/encoding/yaml/manual.go b/pkg/encoding/yaml/manual.go
index c6d5188..686870c 100644
--- a/pkg/encoding/yaml/manual.go
+++ b/pkg/encoding/yaml/manual.go
@@ -16,9 +16,11 @@
 
 import (
 	"bytes"
+	"io"
 
 	"cuelang.org/go/cue"
 	"cuelang.org/go/cue/ast"
+	"cuelang.org/go/internal"
 	"cuelang.org/go/internal/third_party/yaml"
 	goyaml "github.com/ghodss/yaml"
 )
@@ -54,3 +56,31 @@
 func Unmarshal(data []byte) (ast.Expr, error) {
 	return yaml.Unmarshal("", data)
 }
+
+// Validate validates YAML and confirms it matches the constraints
+// specified by v. If the YAML source is a stream, every object must match v.
+func Validate(b []byte, v cue.Value) (bool, error) {
+	d, err := yaml.NewDecoder("yaml.Validate", b)
+	if err != nil {
+		return false, err
+	}
+	r := internal.GetRuntime(v).(*cue.Runtime)
+	for {
+		expr, err := d.Decode()
+		if err != nil {
+			if err == io.EOF {
+				return true, nil
+			}
+			return false, err
+		}
+
+		inst, err := r.CompileExpr(expr)
+		if err != nil {
+			return false, err
+		}
+
+		if x := v.Unify(inst.Value()); x.Err() != nil {
+			return false, x.Err()
+		}
+	}
+}