generated from layer5io/layer5-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #523 from MUzairS15/MUzairS15/schema-validation
Support for validating resources.
- Loading branch information
Showing
6 changed files
with
208 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package validator | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"sync" | ||
|
||
"cuelang.org/go/cue" | ||
cueerrors "cuelang.org/go/cue/errors" | ||
"github.com/layer5io/meshkit/errors" | ||
"github.com/layer5io/meshkit/utils" | ||
"github.com/meshery/schemas" | ||
) | ||
|
||
var ( | ||
ErrValidateCode = "" | ||
schemaPath = "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/openapi.yml") | ||
if err != nil { | ||
return utils.ErrReadFile(err, "schemas/openapi.yml") | ||
} | ||
|
||
cueschema, err = utils.ConvertoCue(file) | ||
if err == nil { | ||
isSchemaLoaded = true | ||
} | ||
return err | ||
} | ||
|
||
func GetSchemaFor(resourceName string) (cue.Value, error) { | ||
var schema cue.Value | ||
schemaPathForResource := fmt.Sprintf("%s.%s", schemaPath, resourceName) | ||
|
||
err := loadSchema() | ||
if err != nil { | ||
return schema, err | ||
} | ||
|
||
schema, err = utils.Lookup(cueschema, schemaPathForResource) | ||
if err != nil { | ||
return schema, err | ||
} | ||
|
||
byt, err := schema.MarshalJSON() | ||
if err != nil { | ||
return schema, utils.ErrMarshal(err) | ||
} | ||
|
||
schema, err = utils.JsonSchemaToCue(string(byt)) | ||
if err != nil { | ||
return schema, err | ||
} | ||
|
||
return schema, nil | ||
} | ||
|
||
func Validate(schema cue.Value, resourceValue interface{}) error { | ||
|
||
byt, err := json.Marshal(resourceValue) | ||
if err != nil { | ||
return utils.ErrMarshal(err) | ||
} | ||
|
||
cv, err := utils.JsonToCue(byt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
valid, errs := utils.Validate(schema, cv) | ||
if !valid { | ||
return errors.New(ErrValidateCode, | ||
errors.Alert, | ||
[]string{"validation for the resource failed"}, | ||
convertCueErrorsToStrings(errs), | ||
[]string{}, []string{}, | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
func convertCueErrorsToStrings(errs []cueerrors.Error) []string { | ||
var res []string | ||
for _, err := range errs { | ||
_ = cueerrors.Sanitize(err) | ||
} | ||
for _, err2 := range errs { | ||
res = append(res, err2.Error()) | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/layer5io/meshkit/models/catalog/v1alpha1" | ||
"github.com/layer5io/meshkit/models/meshmodel/core/v1beta1" | ||
"github.com/meshery/schemas/models/patterns" | ||
) | ||
|
||
type ValidationCases struct { | ||
Path string | ||
Resource interface{} | ||
|
||
ShouldPass bool | ||
} | ||
|
||
func TestValidator(t *testing.T) { | ||
tests := []ValidationCases{ | ||
{ | ||
Path: "design", | ||
Resource: patterns.PatternFile{ | ||
Name: "test-design", | ||
Services: make(map[string]*patterns.Service), | ||
}, | ||
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"), | ||
}, | ||
ShouldPass: false, | ||
}, | ||
{ | ||
Path: "models", | ||
Resource: v1beta1.Model{ | ||
VersionMeta: v1beta1.VersionMeta{ | ||
SchemaVersion: "v1beta1", | ||
Version: "1.0.0", | ||
}, | ||
Category: v1beta1.Category{ | ||
Name: "test", | ||
}, | ||
Model: v1beta1.ModelEntity{ | ||
Version: "1.0.0", | ||
}, | ||
Status: "", | ||
DisplayName: "", | ||
Description: "", | ||
}, | ||
ShouldPass: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run("validaion", func(_t *testing.T) { | ||
schema, err := GetSchemaFor(test.Path) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
|
||
} | ||
|
||
err = Validate(schema, test.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) | ||
|
||
} else if !test.ShouldPass && err == nil { | ||
t.Errorf("test failed for %s, got %s, want %t error: %v", test.Path, "true", !test.ShouldPass, err) | ||
} | ||
|
||
}) | ||
} | ||
} |