Skip to content

Commit

Permalink
pkg/encoding/yaml: validate concreteness instead of instance of for V…
Browse files Browse the repository at this point in the history
…alidate

This makes the checks more permissive. Subsumption will
no longer be a part of the new evaluator, so we need to use
something different. Ultimately, the use of Subsumption here
is not ideal anyway, instead users should be able to specify
underspecified values in the type definitions itself.

Change-Id: I852bd4ebda805de3536ca33d4950dedcd379d835
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6462
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jul 3, 2020
1 parent 21ca371 commit 57f8242
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
4 changes: 1 addition & 3 deletions cmd/cue/cmd/testdata/script/vet_yaml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
cmp stderr expect-stderr

-- expect-stderr --
phrases: error in call to encoding/yaml.Validate: missing field "text":
phrases: error in call to encoding/yaml.Validate: phrases.quote1.text: incomplete value (!=""):
./yaml.cue:19:10
./yaml.cue:4:11
./yaml.cue:11:17
yaml.Validate:4:6
-- yaml.cue --
import "encoding/yaml"

Expand Down
4 changes: 2 additions & 2 deletions cue/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func TestBuiltins(t *testing.T) {
`_|_(error in call to encoding/json.Validate: a: 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))`,
`_|_(error in call to encoding/yaml.Validate: a: invalid value 4 (out of bound <3))`,
}, {
test("encoding/yaml", `yaml.Validate("a: 2\n---\na: 4", {a:<5})`),
`true`,
}, {
test("encoding/yaml", `yaml.Validate("a: 2\n", {a:<5, b:int})`),
`_|_(error in call to encoding/yaml.Validate: value not an instance)`,
`_|_(error in call to encoding/yaml.Validate: b: incomplete value (int))`,
}, {
test("encoding/yaml", `yaml.ValidatePartial("a: 2\n---\na: 4", {a:<3})`),
`_|_(error in call to encoding/yaml.ValidatePartial: a: invalid value 4 (out of bound <3))`,
Expand Down
7 changes: 6 additions & 1 deletion cue/builtins.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion pkg/encoding/yaml/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,23 @@ func Validate(b []byte, v cue.Value) (bool, error) {
return false, err
}

if err := v.Subsume(inst.Value(), cue.Final()); err != nil {
// TODO: consider using subsumption again here.
// Alternatives:
// - allow definition of non-concrete list,
// like list.Of(int), or []int.
// - Introduce ! in addition to ?, allowing:
// list!: [...]
// if err := v.Subsume(inst.Value(), cue.Final()); err != nil {
// return false, err
// }
x := v.Unify(inst.Value())
if err := x.Err(); err != nil {
return false, err
}
if err := x.Validate(cue.Concrete(true)); err != nil {
return false, err
}

}
}

Expand Down

0 comments on commit 57f8242

Please sign in to comment.