diff --git a/validator/validate.go b/validator/validate.go index a60ee084..8c4f96cf 100644 --- a/validator/validate.go +++ b/validator/validate.go @@ -1,99 +1,47 @@ package validator import ( - "cuelang.org/go/cue" - cueerrors "cuelang.org/go/cue/errors" "encoding/json" "fmt" + "io" + + "cuelang.org/go/cue" + cueerrors "cuelang.org/go/cue/errors" "github.com/meshery/meshkit/errors" "github.com/meshery/meshkit/utils" - // "github.com/meshery/schemas" - // "sync" + "github.com/meshery/schemas" ) var ( ErrValidateCode = "" - _ = "components.schemas" - // cueschema cue.Value - // mx sync.Mutex - // isSchemaLoaded bool ) -// func loadSchema() error { -// mx.Lock() -// defer func() { -// mx.Unlock() -// }() - -// if isSchemaLoaded { -// return nil -// } - -// file, err := schemas.Schemas.Open("schemas/constructs/v1beta1/design/design.json") -// if err != nil { -// return utils.ErrReadFile(err, "schemas/constructs/v1beta1/design/design.json") -// } - -// cueschema, err = utils.ConvertoCue(file) -// if err == nil { -// isSchemaLoaded = true -// } -// return err -// } - func GetSchemaFor(resourceName string) (cue.Value, error) { var schema cue.Value - var schemaJSON string - - // Use simplified schemas for testing without external references + var sch string switch resourceName { case "design": - schemaJSON = `{ - "type": "object", - "properties": { - "name": {"type": "string"}, - "services": {"type": "object"}, - "schemaVersion": {"type": "string"} - }, - "required": ["name"] - }` + sch = "constructs/v1beta1/design/design.json" case "catalog_data": - schemaJSON = `{ - "type": "object", - "properties": { - "publishedVersion": {"type": "string", "pattern": "^v[0-9]+\\.[0-9]+\\.[0-9]+$"}, - "contentClass": {"type": "string"}, - "compatibility": {"type": "array"}, - "patternCaveats": {"type": "string"}, - "patternInfo": {"type": "string"}, - "type": {"type": "string", "enum": ["Deployment", "Service", "ConfigMap"]} - }, - "required": ["publishedVersion", "contentClass", "compatibility", "patternCaveats", "patternInfo", "type"] - }` + sch = "constructs/v1alpha1/catalog_data.json" case "models": - schemaJSON = `{ - "type": "object", - "properties": { - "schemaVersion": {"type": "string"}, - "version": {"type": "string"}, - "category": {"type": "object"}, - "model": {"type": "object"}, - "status": {"type": "string"}, - "displayName": {"type": "string"}, - "description": {"type": "string"} - }, - "required": ["schemaVersion", "version", "displayName", "description"] - }` + sch = "constructs/v1beta1/model/model.json" default: return schema, fmt.Errorf("no schema defined for resource: %s", resourceName) } - - schema, err := utils.JsonSchemaToCue(schemaJSON) + file, err := schemas.Schemas.Open(fmt.Sprintf("schemas/%s", sch)) if err != nil { return schema, err } - - return schema, nil + byt, err := io.ReadAll(file) + if err != nil { + return schema, err + } + val, err := utils.JsonToCue(byt) + if err != nil { + return schema, err + } + return val, nil } func Validate(schema cue.Value, resourceValue interface{}) error { diff --git a/validator/validate_test.go b/validator/validate_test.go index 8f8631cd..2eb425c8 100644 --- a/validator/validate_test.go +++ b/validator/validate_test.go @@ -4,80 +4,67 @@ import ( "fmt" "testing" - "github.com/meshery/meshkit/models/catalog/v1alpha1" - "github.com/meshery/schemas/models/v1alpha2" - "github.com/meshery/schemas/models/v1beta1" - "github.com/meshery/schemas/models/v1beta1/category" - "github.com/meshery/schemas/models/v1beta1/model" + "cuelang.org/go/cue" ) type ValidationCases struct { Path string - Resource interface{} + Resource any ShouldPass bool } +func generateValidResource(t *testing.T, schema cue.Value) any { + var resource map[string]any + err := schema.Decode(&resource) + if err != nil { + t.Fatal(err) + } + return resource +} + func TestValidator(t *testing.T) { tests := []ValidationCases{ { - Path: "design", - Resource: v1alpha2.PatternFile{ - Name: "test-design", - Services: make(map[string]*v1alpha2.Service), - }, + Path: "design", ShouldPass: true, }, { Path: "catalog_data", - Resource: v1alpha1.CatalogData{ - PublishedVersion: "v.10.9", - ContentClass: "sdsds", - Compatibility: []v1alpha1.CatalogDataCompatibility{ - "kubernetes", - }, - PatternCaveats: "NA", - PatternInfo: "NA", - Type: v1alpha1.CatalogDataType("Dployment"), + Resource: map[string]any{ + "pattern_caveats": "NA", + "pattern_info": "NA", + "type": "Deployment", }, ShouldPass: false, }, { - Path: "models", - Resource: model.ModelDefinition{ - - SchemaVersion: v1beta1.ModelSchemaVersion, - Version: "1.0.0", - - Category: category.CategoryDefinition{ - Name: "test", - }, - Model: model.Model{ - Version: "1.0.0", - }, - Status: "", - DisplayName: "", - Description: "", - }, - ShouldPass: false, + Path: "models", + ShouldPass: true, }, } for _, test := range tests { - t.Run("validaion", func(_t *testing.T) { + t.Run(test.Path, func(_t *testing.T) { schema, err := GetSchemaFor(test.Path) if err != nil { t.Errorf("%v", err) } + var resource any + if test.Resource != nil { + resource = test.Resource + } else { + resource = generateValidResource(t, schema) + } - err = Validate(schema, test.Resource) + err = Validate(schema, resource) 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) + t.Errorf("test failed for %s, got %t, 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) + t.Errorf("test failed for %s, got %t, want %t error: %v", test.Path, true, !test.ShouldPass, err) } })