Skip to content

Commit

Permalink
feat: impl validate function error message output
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Oct 24, 2023
1 parent 43f9340 commit 7a50584
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/tools/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package validate

import (
"errors"

"kcl-lang.io/kcl-go/pkg/service"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)
Expand All @@ -28,5 +30,9 @@ func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error)
if err != nil {
return false, err
}
return resp.Success, nil
var e error = nil
if resp.ErrMessage != "" {
e = errors.New(resp.ErrMessage)
}
return resp.Success, e
}
19 changes: 19 additions & 0 deletions pkg/tools/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package validate

import (
"strings"
"testing"
)

Expand All @@ -24,3 +25,21 @@ schema Person:
t.Fatalf("expect: %q, got False", "True")
}
}

func TestValidateCodeFail(t *testing.T) {
data := `{"k": "value"}`
code := `
schema Person:
key: str
check:
"value" in key # 'key' is required and 'key' must contain "value"
`

_, err := ValidateCode(data, code, nil)
if err == nil {
t.Fatalf("expect validation error")
} else if !strings.Contains(err.Error(), "error") {
t.Fatalf("expect validation error, got %s", err.Error())
}
}

0 comments on commit 7a50584

Please sign in to comment.