-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
validate.go
54 lines (41 loc) · 1.07 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package structural
import (
"fmt"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"github.com/hofstadter-io/cuetils/cmd/cuetils/flags"
)
func ValidateGlobs(schema string, globs []string, opts *flags.RootPflagpole) ([]GlobResult, error) {
ctx := cuecontext.New()
operator, err := ReadArg(schema, ctx, nil)
if err != nil {
return nil, err
}
inputs, err := LoadGlobs(globs)
if len(inputs) == 0 {
return nil, fmt.Errorf("no inputs found")
}
results := make([]GlobResult, 0)
for _, input := range inputs {
iv := ctx.CompileBytes(input.Content, cue.Filename(input.Filename))
if iv.Err() != nil {
return nil, iv.Err()
}
result := iv.Unify(operator.Value)
err := result.Validate()
if err != nil {
out := FormatCueError(err)
results = append(results, GlobResult{
Filename: input.Filename,
Content: out,
})
continue
}
}
return results, nil
}
func ValidateValue(schema, val cue.Value, opts *flags.RootPflagpole) (bool, error) {
// probably need to deal with some flags here...
e := val.Unify(schema).Err()
return e == nil, e
}