Skip to content

Commit 9e4aa4e

Browse files
committed
feat: add validate APIs
Signed-off-by: peefy <[email protected]>
1 parent d0e6e93 commit 9e4aa4e

File tree

6 files changed

+102
-7
lines changed

6 files changed

+102
-7
lines changed

kclvm.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,15 @@ func OverrideFile(file string, specs, importPaths []string) (bool, error) {
178178
return override.OverrideFile(file, specs, importPaths)
179179
}
180180

181-
// ValidateCode validate data match code
182-
func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error) {
183-
return validate.ValidateCode(data, code, opt)
181+
// ValidateCode validate data string match code string
182+
func ValidateCode(data, code string, opts *ValidateOptions) (ok bool, err error) {
183+
return validate.ValidateCode(data, code, opts)
184+
}
185+
186+
// Validate validates the given data file against the specified
187+
// schema file with the provided options.
188+
func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err error) {
189+
return validate.Validate(dataFile, schemaFile, opts)
184190
}
185191

186192
// Test calls the test tool to run uni tests in packages.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Alice",
3+
"age": 18,
4+
"message": "This is Alice",
5+
"data": {
6+
"id": 1,
7+
"value": "value1"
8+
},
9+
"labels": {
10+
"key": "value"
11+
},
12+
"hc": [1, 2, 3, ""]
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Alice",
3+
"age": 18,
4+
"message": "This is Alice",
5+
"data": {
6+
"id": 1,
7+
"value": "value1"
8+
},
9+
"labels": {
10+
"key": "value"
11+
},
12+
"hc": [1, 2, 3]
13+
}

pkg/tools/validate/test_data/schema.k

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
schema User:
2+
name: str
3+
age: int
4+
message?: str
5+
data: Data
6+
labels: {str:}
7+
hc: [int]
8+
9+
check:
10+
age > 10
11+
12+
schema Data:
13+
id: int
14+
value: str

pkg/tools/validate/validate.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,48 @@
1-
// Copyright 2021 The KCL Authors. All rights reserved.
1+
// Copyright The KCL Authors. All rights reserved.
22

33
package validate
44

55
import (
66
"errors"
7+
"os"
78

89
"kcl-lang.io/kcl-go/pkg/service"
910
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
1011
)
1112

13+
// ValidateOptions represents the options for the Validate function.
1214
type ValidateOptions struct {
13-
Schema string
14-
AttributeName string
15-
Format string
15+
Schema string // The schema to validate against.
16+
AttributeName string // The attribute name to validate.
17+
Format string // The format of the data.
18+
}
19+
20+
// Validate validates the given data file against the specified
21+
// schema file with the provided options.
22+
func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err error) {
23+
data, err := os.ReadFile(dataFile)
24+
if err != nil {
25+
return false, err
26+
}
27+
if opts == nil {
28+
opts = &ValidateOptions{}
29+
}
30+
client := service.NewKclvmServiceClient()
31+
resp, err := client.ValidateCode(&gpyrpc.ValidateCode_Args{
32+
File: schemaFile,
33+
Data: string(data),
34+
Schema: opts.Schema,
35+
AttributeName: opts.AttributeName,
36+
Format: opts.Format,
37+
})
38+
if err != nil {
39+
return false, err
40+
}
41+
var e error = nil
42+
if resp.ErrMessage != "" {
43+
e = errors.New(resp.ErrMessage)
44+
}
45+
return resp.Success, e
1646
}
1747

1848
func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error) {

pkg/tools/validate/validate_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@ import (
77
"testing"
88
)
99

10+
func TestValidate(t *testing.T) {
11+
ok, err := Validate("./test_data/data.json", "./test_data/schema.k", nil)
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
if !ok {
16+
t.Fatalf("expect: %q, got False", "True")
17+
}
18+
}
19+
20+
func TestValidateFailed(t *testing.T) {
21+
ok, err := Validate("./test_data/data-failed.json", "./test_data/schema.k", nil)
22+
if ok == false && err != nil && strings.Contains(err.Error(), "expected [int], got [int(1) | int(2) | int(3) | str()]") {
23+
// Test Pass
24+
} else {
25+
t.Fatalf("expect: error, got (%v, %v)", ok, err)
26+
}
27+
}
28+
1029
func TestValidateCode(t *testing.T) {
1130
data := `{"key": "value"}`
1231
code := `

0 commit comments

Comments
 (0)