Skip to content

Commit

Permalink
feat: add validate APIs (#189)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Nov 29, 2023
1 parent d0e6e93 commit 83897a5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
12 changes: 9 additions & 3 deletions kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,15 @@ func OverrideFile(file string, specs, importPaths []string) (bool, error) {
return override.OverrideFile(file, specs, importPaths)
}

// ValidateCode validate data match code
func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error) {
return validate.ValidateCode(data, code, opt)
// ValidateCode validate data string match code string
func ValidateCode(data, code string, opts *ValidateOptions) (ok bool, err error) {
return validate.ValidateCode(data, code, opts)
}

// Validate validates the given data file against the specified
// schema file with the provided options.
func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err error) {
return validate.Validate(dataFile, schemaFile, opts)
}

// Test calls the test tool to run uni tests in packages.
Expand Down
13 changes: 13 additions & 0 deletions pkg/tools/validate/test_data/data-failed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Alice",
"age": 18,
"message": "This is Alice",
"data": {
"id": 1,
"value": "value1"
},
"labels": {
"key": "value"
},
"hc": [1, 2, 3, ""]
}
13 changes: 13 additions & 0 deletions pkg/tools/validate/test_data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "Alice",
"age": 18,
"message": "This is Alice",
"data": {
"id": 1,
"value": "value1"
},
"labels": {
"key": "value"
},
"hc": [1, 2, 3]
}
14 changes: 14 additions & 0 deletions pkg/tools/validate/test_data/schema.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema User:
name: str
age: int
message?: str
data: Data
labels: {str:}
hc: [int]

check:
age > 10

schema Data:
id: int
value: str
38 changes: 34 additions & 4 deletions pkg/tools/validate/validate.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
// Copyright 2021 The KCL Authors. All rights reserved.
// Copyright The KCL Authors. All rights reserved.

package validate

import (
"errors"
"os"

"kcl-lang.io/kcl-go/pkg/service"
"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)

// ValidateOptions represents the options for the Validate function.
type ValidateOptions struct {
Schema string
AttributeName string
Format string
Schema string // The schema to validate against.
AttributeName string // The attribute name to validate.
Format string // The format of the data.
}

// Validate validates the given data file against the specified
// schema file with the provided options.
func Validate(dataFile, schemaFile string, opts *ValidateOptions) (ok bool, err error) {
data, err := os.ReadFile(dataFile)
if err != nil {
return false, err
}
if opts == nil {
opts = &ValidateOptions{}
}
client := service.NewKclvmServiceClient()
resp, err := client.ValidateCode(&gpyrpc.ValidateCode_Args{
File: schemaFile,
Data: string(data),
Schema: opts.Schema,
AttributeName: opts.AttributeName,
Format: opts.Format,
})
if err != nil {
return false, err
}
var e error = nil
if resp.ErrMessage != "" {
e = errors.New(resp.ErrMessage)
}
return resp.Success, e
}

func ValidateCode(data, code string, opt *ValidateOptions) (ok bool, err error) {
Expand Down
19 changes: 19 additions & 0 deletions pkg/tools/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ import (
"testing"
)

func TestValidate(t *testing.T) {
ok, err := Validate("./test_data/data.json", "./test_data/schema.k", nil)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatalf("expect: %q, got False", "True")
}
}

func TestValidateFailed(t *testing.T) {
ok, err := Validate("./test_data/data-failed.json", "./test_data/schema.k", nil)
if ok == false && err != nil && strings.Contains(err.Error(), "expected [int], got [int(1) | int(2) | int(3) | str()]") {
// Test Pass
} else {
t.Fatalf("expect: error, got (%v, %v)", ok, err)
}
}

func TestValidateCode(t *testing.T) {
data := `{"key": "value"}`
code := `
Expand Down

0 comments on commit 83897a5

Please sign in to comment.