/cuelang.org/go/yaml.gofunc Validate(b []byte, v cue.Value) error { _, err := pkgyaml.Validate(b, v) return err}沒有任何示例代碼告訴我如何使用此 API,我需要一些示例來了解如何使用它。
1 回答

哈士奇WWW
TA貢獻1799條經(jīng)驗 獲得超6個贊
我想到了。首先我們需要一個提示文件:
// demo.cue
min: number
max: number & >min
接著:
// valid_test.go
package demo
import (
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/encoding/yaml"
"fmt"
"io/ioutil"
"strings"
"testing"
)
const Yaml = `
min: 10
max: 5
`
func TestValidate(t *testing.T) {
r := strings.NewReader(Yaml)
b, _ := ioutil.ReadAll(r)
cue, _ := ioutil.ReadFile("demo.cue")
// Cue API for Go
c := cuecontext.New()
v := c.CompileBytes(cue)
err := yaml.Validate(b, v)
fmt.Println(err) // max: invalid value 5 (out of bound >10)
}
- 1 回答
- 0 關(guān)注
- 118 瀏覽
添加回答
舉報
0/150
提交
取消