Skip to content

Commit

Permalink
Merge pull request #208 from meshery/nithish/refactor/lazy_validation
Browse files Browse the repository at this point in the history
[Refactor] Make Validation Lazy and Add Tests
  • Loading branch information
humblenginr committed Aug 20, 2022
2 parents ca04e54 + 791e09b commit 61a4cbb
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
41 changes: 33 additions & 8 deletions utils/cue.go
@@ -1,24 +1,49 @@
package utils

import (
"fmt"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/format"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/jsonschema"
"cuelang.org/go/encoding/yaml"
)

func Validate(schema cue.Value, value cue.Value) (bool, error) {
uval := schema.Unify(value)
if uval.Err() != nil {
return false, uval.Err()
}
_, err := uval.MarshalJSON()
func Validate(schema cue.Value, value cue.Value) (bool, []errors.Error) {
var errs []errors.Error
uval := value.Unify(schema)
err := uval.Validate()
if err != nil {
return false, err
cueErr := errors.Errors(err)
errs = append(errs, cueErr...)
}
// check for required fields
schema.Walk(func(v cue.Value) bool {
val := value.LookupPath(v.Path())
if !(val.Err() == nil && val.IsConcrete()) {
cueErr := errors.Errors(errors.New(fmt.Sprintf("%v is a required field", v.Path().String())))
errs = append(errs, cueErr...)
}
return true
}, nil)
if len(errs) != 0 {
return false, errs
}
return true, nil
return true, make([]errors.Error, 0)
}

func GetNonConcreteFields(val cue.Value) []string {
res := make([]string, 0)
val.Walk(func(v cue.Value) bool {
if !v.IsConcrete() {
res = append(res, v.Path().String())
}
return true
}, nil)
return res
}

func JsonToCue(value []byte) (cue.Value, error) {
Expand Down
72 changes: 72 additions & 0 deletions utils/cue_test.go
@@ -0,0 +1,72 @@
package utils

import (
"fmt"
"reflect"
"testing"

// "cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
// "cuelang.org/go/cue/errors"
)

var testSchema1 = `
name: string
task: string
properties?: {
item: "array"
}
`
var testValue1 = `
name: 43
properties: item : "asf"
`

var testValue2 = `
name: string
google: int
prop: item: string
`

func TestValidate(t *testing.T) {
var tests = []struct {
schema string
value string
want bool
}{
{testSchema1, testValue1, false},
}

for _, tt := range tests {
t.Run("validate", func(t *testing.T) {
ctx := cuecontext.New()
schema := ctx.CompileString(tt.schema)
value := ctx.CompileString(tt.value)
ans, errs := Validate(schema, value)
fmt.Println("errs: ", errs)
if ans != tt.want {
t.Errorf("got %v, want %v", ans, tt.want)
}
})
}
}

func TestGetNonConcreteFields(t *testing.T) {
var tests = []struct {
value string
want []string
}{
{testValue2, []string{"name", "google", "prop.item"}},
}

for _, tt := range tests {
t.Run("validate", func(t *testing.T) {
ctx := cuecontext.New()
value := ctx.CompileString(tt.value)
ans := GetNonConcreteFields(value)
if !reflect.DeepEqual(ans, tt.want) {
t.Errorf("got %v, want %v", ans, tt.want)
}
})
}
}

0 comments on commit 61a4cbb

Please sign in to comment.