Skip to content

Commit

Permalink
feat: add support to verify schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashwat-K committed May 14, 2024
1 parent da643d6 commit e0925a4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
44 changes: 44 additions & 0 deletions common/general/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"strings"

"github.com/Masterminds/semver/v3"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"

sch "github.com/Sashwat-K/hpcr-contract-schema"
cert "github.com/Sashwat-K/hpcr-encryption-certificate"
)

Expand Down Expand Up @@ -137,6 +139,24 @@ func IsJSON(str string) bool {
return json.Unmarshal([]byte(str), &js) == nil
}

// YamlToJson - function to convert YAML to JSON
func YamlToJson(str string) (string, error) {
var obj interface{}

err := yaml.Unmarshal([]byte(str), &obj)
if err != nil {
return "", err
}

jsonData, err := json.Marshal(obj)
if err != nil {
return "", err
}

// Marshal the object to JSON
return string(jsonData), err
}

// EncodeToBase64 - function to encode string as base64
func EncodeToBase64(input string) string {
return base64.StdEncoding.EncodeToString([]byte(input))
Expand Down Expand Up @@ -319,3 +339,27 @@ func GenerateTgzBase64(folderFilesPath []string) (string, error) {

return EncodeToBase64(buf.String()), nil
}

// VerifyContractWithSchema - function to verify if contract matches schema
func VerifyContractWithSchema(contract string) (bool, error) {
jsonData, err := YamlToJson(contract)
if err != nil {
return false, fmt.Errorf("error converting YAML to JSON - %v", err)
}

schema, err := gojsonschema.NewSchema(gojsonschema.NewBytesLoader([]byte(sch.ContractSchema)))
if err != nil {
return false, fmt.Errorf("failed to parse schema - %v", err)
}

report, err := schema.Validate(gojsonschema.NewBytesLoader([]byte(jsonData)))
if err != nil {
return false, fmt.Errorf("failed to validate contract - %v", err)
}

for _, err := range report.Errors() {
fmt.Printf("- %s\n", err)
}

return report.Valid(), nil
}
14 changes: 14 additions & 0 deletions common/general/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,17 @@ func TestGenerateTgzBase64(t *testing.T) {

assert.NotEmpty(t, result)
}

func TestVerifyContractWithSchema(t *testing.T) {
contract, err := ReadDataFromFile(simpleContractPath)
if err != nil {
t.Errorf("failed to read contract - %v", err)
}

result, err := VerifyContractWithSchema(contract)
if err != nil {
t.Errorf("schema verification failed - %v", err)
}

fmt.Println(result)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ require (
)

require (
github.com/Sashwat-K/hpcr-contract-schema v0.0.0-20240514124741-7cf9bd31769a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Sashwat-K/hpcr-contract-schema v0.0.0-20240514124741-7cf9bd31769a h1:2J9iMh85iTXEhWTsMmqFQMmor0lKog9l5XiYEgICf2s=
github.com/Sashwat-K/hpcr-contract-schema v0.0.0-20240514124741-7cf9bd31769a/go.mod h1:tVKJn55K/mwcrIuWDD8aWY83ZqjuW5JVcAGmv1aXPOs=
github.com/Sashwat-K/hpcr-encryption-certificate v0.0.0-20240425124247-33ff4ba17874 h1:wppx9l7JPE8qeZo4SS8FsvaOH/W6m7tyTzmlj4i0uh0=
github.com/Sashwat-K/hpcr-encryption-certificate v0.0.0-20240425124247-33ff4ba17874/go.mod h1:RPviKVe8z+ICoF/OkdNksBbJhNfMHwqQS3QcI8sYH+M=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
7 changes: 6 additions & 1 deletion samples/simple_contract.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
env:
type: env
logging:
logDNA:
hostname: syslog-a.eu-de.logging.cloud.ibm.com
ingestionKey: ab00e3c09p1d4ff7fff9f04c12183413
workload:
type: workload
type: workload
compose:
archive: kdsbfoijdfojsnbo

0 comments on commit e0925a4

Please sign in to comment.