Scuemata: Checking json validity by enabling skipped tests (#34385)
* Make sure we don't skip any tests - refactoring * Remove commented lines * Move test folder
This commit is contained in:
@@ -8,11 +8,28 @@ import (
|
||||
"strings"
|
||||
|
||||
"cuelang.org/go/cue"
|
||||
errs "cuelang.org/go/cue/errors"
|
||||
cuejson "cuelang.org/go/pkg/encoding/json"
|
||||
)
|
||||
|
||||
var rt = &cue.Runtime{}
|
||||
|
||||
// CueError wraps Errors caused by malformed cue files.
|
||||
type CueError struct {
|
||||
ErrorMap map[int]string
|
||||
}
|
||||
|
||||
// Error func needed to implement standard golang error
|
||||
func (cErr *CueError) Error() string {
|
||||
var errorString string
|
||||
if cErr.ErrorMap != nil {
|
||||
for k, v := range cErr.ErrorMap {
|
||||
errorString = errorString + fmt.Sprintf("line: %d, %s \n", k, v)
|
||||
}
|
||||
}
|
||||
return errorString
|
||||
}
|
||||
|
||||
// CueSchema represents a single, complete CUE-based schema that can perform
|
||||
// operations on Resources.
|
||||
//
|
||||
@@ -93,6 +110,10 @@ func SearchAndValidate(s VersionedCueSchema, v interface{}) (VersionedCueSchema,
|
||||
// collates all the individual errors, relates them to the schema that
|
||||
// produced them, and ideally deduplicates repeated errors across each
|
||||
// schema.
|
||||
cueErrors := WrapCUEError(err)
|
||||
if err != nil {
|
||||
return nil, cueErrors
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -402,4 +423,24 @@ type Resource struct {
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// WrapCUEError is a wrapper for cueErrors that occur and are not self explanatory.
|
||||
// If an error is of type cueErr, then iterate through the error array, export line number
|
||||
// and filename, otherwise return usual error.
|
||||
func WrapCUEError(err error) error {
|
||||
var cErr errs.Error
|
||||
m := make(map[int]string)
|
||||
if ok := errors.As(err, &cErr); ok {
|
||||
for _, e := range errs.Errors(err) {
|
||||
if e.Position().File() != nil {
|
||||
line := e.Position().Line()
|
||||
m[line] = fmt.Sprintf("%q: in file %s", err, e.Position().File().Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(m) != 0 {
|
||||
return &CueError{m}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO add migrator with SearchOption for stopping criteria
|
||||
|
||||
Reference in New Issue
Block a user