-
Notifications
You must be signed in to change notification settings - Fork 160
fix(validator): resolve unknown domain for reference #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
Comment on lines
+17
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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) | ||
} | ||
|
||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There appears to be a logic error in how the schema is being processed. The
byt
variable contains the content of a JSON Schema file. To correctly convert a JSON Schema into a CUE value that can be used for validation, you should useutils.JsonSchemaToCue
. The current implementation usesutils.JsonToCue
, which simply converts the JSON Schema document itself into a CUE value, rather than interpreting it as a set of validation rules. This will cause validations to not work as intended.