Closed owulveryck closed 3 years ago
I digged a bit into the json.Validate method, and wrote a more straightforward version:
package main
import (
"io/ioutil"
"log"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/load"
)
func main() {
ctx := cuecontext.New()
buildInstances := load.Instances([]string{"definitions.cue"}, nil)
values, err := ctx.BuildInstances(buildInstances)
if err != nil {
log.Fatal(err)
}
if len(values) != 1 {
log.Fatal("more than one value")
}
v := values[0]
b, err := ioutil.ReadFile("testdata/bad.json")
if err != nil {
log.Fatal(err)
}
data := ctx.CompileBytes(b)
unified := v.Unify(data)
opts := []cue.Option{
cue.Attributes(true),
cue.Definitions(true),
cue.Hidden(true),
}
err = unified.Validate(opts...)
if err != nil {
log.Fatal(err)
}
}
I think this is the way to go (according to the discussion https://github.com/cuelang/cue/discussions/810). But it still does not work. I don't understand this comment
Value v and w must be obtained from the same build. TODO: remove this requirement.
Maybe it is the reason it does not work as expected.
This simply cannot work as my JSON "message" does not handle the type; therefore there is no way to validate this.
Looking for a proper way to add it on runtime, but so far, this issue is not relevant anymore
Please also see my response in https://github.com/cuelang/cue/discussions/1030#discussioncomment-813968
This issue has been migrated to https://github.com/cue-lang/cue/issues/1031.
For more details about CUE's migration to a new home, please see https://github.com/cue-lang/cue/issues/1078.
This is an issue following a comment orignially posted by @owulveryck in https://github.com/cuelang/cue/discussions/1030#discussioncomment-807569_
Consider the following data:
I can validate the file without explicitly loading the definitions:
I wonder now if there is a way to load the "definitions" in a context, a runtime, or whatever, so I can validate the data on the fly in a webservice for example?
edit
I tried with JSON which would do what I am trying to achieve:
and bad.json is:
I think that this should fail, but it does not :(