diff --git a/utils/cue.go b/utils/cue.go index 229edfb7..42985477 100644 --- a/utils/cue.go +++ b/utils/cue.go @@ -3,7 +3,7 @@ package utils import ( "fmt" "io" - + "runtime" "cuelang.org/go/cue" "cuelang.org/go/cue/cuecontext" "cuelang.org/go/cue/errors" @@ -76,35 +76,91 @@ func YamlToCue(value string) (cue.Value, error) { return out, nil } +// func JsonSchemaToCue(value string) (cue.Value, error) { +// var out cue.Value +// jsonSchema, err := json.Extract("", []byte(value)) +// if err != nil { +// return out, ErrJsonSchemaToCue(err) +// } +// cueCtx := cuecontext.New() +// cueJsonSchemaExpr := cueCtx.BuildExpr(jsonSchema) +// if err = cueJsonSchemaExpr.Err(); err != nil { +// return out, ErrJsonSchemaToCue(err) +// } +// extractedSchema, err := jsonschema.Extract(cueJsonSchemaExpr, &jsonschema.Config{ +// PkgName: "jsonschemeconv", +// }) + +// if err != nil { +// return out, ErrJsonSchemaToCue(err) +// } +// src, err := format.Node(extractedSchema) +// if err != nil { +// return out, ErrJsonSchemaToCue(err) +// } +// out = cueCtx.CompileString(string(src)) +// if out.Err() != nil { +// return out, ErrJsonSchemaToCue(out.Err()) +// } +// return out, nil +// } +func printStackTrace() { + buf := make([]byte, 1024) + runtime.Stack(buf, false) + fmt.Printf("%s\n", buf) +} + func JsonSchemaToCue(value string) (cue.Value, error) { var out cue.Value + + // Step 1: Extract JSON schema jsonSchema, err := json.Extract("", []byte(value)) + fmt.Println("jsonSchema = ", jsonSchema) if err != nil { + fmt.Printf("Error during json.Extract: %v\n", err) return out, ErrJsonSchemaToCue(err) } + + // Step 2: Build CUE expression cueCtx := cuecontext.New() cueJsonSchemaExpr := cueCtx.BuildExpr(jsonSchema) + fmt.Println("cueJsonSchemaExp = ", cueJsonSchemaExpr) if err = cueJsonSchemaExpr.Err(); err != nil { + fmt.Printf("Error during cueCtx.BuildExpr: %v\n", err) return out, ErrJsonSchemaToCue(err) } + + // Step 3: Extract schema to CUE extractedSchema, err := jsonschema.Extract(cueJsonSchemaExpr, &jsonschema.Config{ PkgName: "jsonschemeconv", }) - + fmt.Println("extractedS = ", extractedSchema) if err != nil { + fmt.Printf("Error during jsonschema.Extract: %v\n", err) + printStackTrace() return out, ErrJsonSchemaToCue(err) } + + // Step 4: Format and compile schema src, err := format.Node(extractedSchema) + fmt.Println("src = ", src) if err != nil { + fmt.Printf("Error during format.Node: %v\n", err) return out, ErrJsonSchemaToCue(err) } + out = cueCtx.CompileString(string(src)) + fmt.Println("out = ", out) if out.Err() != nil { + fmt.Printf("Error during cueCtx.CompileString: %v\n", out.Err()) return out, ErrJsonSchemaToCue(out.Err()) } + + fmt.Println("Successfully converted JSON schema to CUE.") return out, nil } + func Lookup(rootVal cue.Value, path string) (cue.Value, error) { res := rootVal.LookupPath(cue.ParsePath(path)) if res.Err() != nil { diff --git a/validator/validate.go b/validator/validate.go index dc043d52..a55e8bd4 100644 --- a/validator/validate.go +++ b/validator/validate.go @@ -3,7 +3,7 @@ package validator import ( "encoding/json" "sync" - + "fmt" "cuelang.org/go/cue" cueerrors "cuelang.org/go/cue/errors" "github.com/layer5io/meshkit/errors" @@ -48,30 +48,91 @@ func GetSchemaFor(resourceName string) (cue.Value, error) { err := loadSchema() if err != nil { + fmt.Println("error in loadSchema", err) return schema, err } - + + fmt.Println("cueschema = ", cueschema, "schemapathresource = ", schemaPathForResource) schema, err = utils.Lookup(cueschema, schemaPathForResource) + fmt.Println("schema = ", schema) if err != nil { return schema, err } byt, err := schema.MarshalJSON() + fmt.Println("marshal = ", byt, err) if err != nil { + fmt.Println("return", schema, "and", utils.ErrMarshal(err)) return schema, utils.ErrMarshal(err) } + fmt.Printf("JSON Data: %s\n", string(byt)) schema, err = utils.JsonSchemaToCue(string(byt)) - + fmt.Println("inside fun schema = ", schema, err) log, _ := logger.New("test-validate", logger.Options{}) log.Error(err) if err != nil { return schema, err } - + fmt.Println("done") return schema, nil } +// func GetSchemaFor(resourceName string) (cue.Value, error) { +// var schema cue.Value +// var schemaPathForResource string + +// // Set the schema path based on the resourceName +// switch resourceName { +// case "design": +// schemaPathForResource = "path.to.design.schema" +// case "catalog_data": +// schemaPathForResource = "path.to.catalog.schema" +// case "models": +// schemaPathForResource = "path.to.models.schema" +// default: +// fmt.Printf("Unknown resource name: %s\n", resourceName) +// return schema, fmt.Errorf("unknown resource name: %s", resourceName) +// } + +// fmt.Printf("Schema path for resource %s: %s\n", resourceName, schemaPathForResource) + +// // Load the schema +// err := loadSchema() +// if err != nil { +// fmt.Printf("Failed to load schema: %v\n", err) +// return schema, fmt.Errorf("failed to load schema: %v", err) +// } + +// // Look up the schema using the resource path +// schema, err = utils.Lookup(cueschema, schemaPathForResource) +// if err != nil { +// fmt.Printf("Failed to look up schema for resource %s: %v\n", resourceName, err) +// return schema, fmt.Errorf("failed to look up schema for resource %s: %v", resourceName, err) +// } + +// // Marshal the schema to JSON +// byt, err := schema.MarshalJSON() +// if err != nil { +// fmt.Printf("Failed to marshal schema for resource %s: %v\n", resourceName, err) +// return schema, fmt.Errorf("failed to marshal schema for resource %s: %v", resourceName, err) +// } + +// // Convert JSON schema to CUE schema +// schema, err = utils.JsonSchemaToCue(string(byt)) +// if err != nil { +// fmt.Printf("Failed to convert JSON schema to CUE for resource %s: %v\n", resourceName, err) +// log, _ := logger.New("test-validate", logger.Options{}) +// log.Error(err) +// return schema, fmt.Errorf("failed to convert JSON schema to CUE for resource %s: %v", resourceName, err) +// } + +// fmt.Println("Schema successfully loaded and converted.") + +// return schema, nil +// } + + func Validate(schema cue.Value, resourceValue interface{}) error { byt, err := json.Marshal(resourceValue) diff --git a/validator/validate_test.go b/validator/validate_test.go index 48303d22..33ea76da 100644 --- a/validator/validate_test.go +++ b/validator/validate_test.go @@ -66,18 +66,32 @@ func TestValidator(t *testing.T) { for _, test := range tests { t.Run("validaion", func(_t *testing.T) { schema, err := GetSchemaFor(test.Path) - if err != nil { - t.Errorf("%v", err) + fmt.Println("schema = ", schema) + fmt.Println("err = ", err) + // if err != nil { + // t.Errorf("%v", err) + // } + if err != nil { + t.Errorf("Error getting schema for path %s: %v", test.Path, err) + return } err = Validate(schema, test.Resource) + if err != nil { + t.Errorf("Validation failed for path %s with resource %v: %v", test.Path, test.Resource, err) + } fmt.Println(err) - if test.ShouldPass && err != nil { - t.Errorf("test failed for %s, got %s, want %t, error: %v", test.Path, "false", test.ShouldPass, err) + // if test.ShouldPass && err != nil { + // t.Errorf("test failed for %s, got %s, want %t, error: %v", test.Path, "false", test.ShouldPass, err) + // } else if !test.ShouldPass && err == nil { + // t.Errorf("test failed for %s, got %s, want %t error: %v", test.Path, "true", !test.ShouldPass, err) + // } + if test.ShouldPass && err != nil { + t.Errorf("Expected success but got failure for path %s: %v", test.Path, err) } else if !test.ShouldPass && err == nil { - t.Errorf("test failed for %s, got %s, want %t error: %v", test.Path, "true", !test.ShouldPass, err) + t.Errorf("Expected failure but got success for path %s", test.Path) } })