diff --git a/common/general/general.go b/common/general/general.go index 2ff9ccb..acb7ed8 100644 --- a/common/general/general.go +++ b/common/general/general.go @@ -341,26 +341,26 @@ func GenerateTgzBase64(folderFilesPath []string) (string, error) { } // VerifyContractWithSchema - function to verify if contract matches schema -func VerifyContractWithSchema(contract string) (bool, error) { +func VerifyContractWithSchema(contract string) error { jsonData, err := YamlToJson(contract) if err != nil { - return false, fmt.Errorf("error converting YAML to JSON - %v", err) + return 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) + return 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) + return fmt.Errorf("failed to validate contract - %v", err) } result := report.Valid() if result { - return true, nil + return nil } else { var consolidatedErrors strings.Builder for i, err := range report.Errors() { @@ -370,6 +370,6 @@ func VerifyContractWithSchema(contract string) (bool, error) { consolidatedErrors.WriteString(err.String()) } - return report.Valid(), fmt.Errorf("validation failed - %s", consolidatedErrors.String()) + return fmt.Errorf("validation failed - %s", consolidatedErrors.String()) } } diff --git a/common/general/general_test.go b/common/general/general_test.go index 73d6315..27f1074 100644 --- a/common/general/general_test.go +++ b/common/general/general_test.go @@ -264,10 +264,8 @@ func TestVerifyContractWithSchema(t *testing.T) { t.Errorf("failed to read contract - %v", err) } - result, err := VerifyContractWithSchema(contract) + err = VerifyContractWithSchema(contract) if err != nil { t.Errorf("schema verification failed - %v", err) } - - assert.True(t, result) } diff --git a/contract/contract.go b/contract/contract.go index 28f3f85..f9719da 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -86,6 +86,11 @@ func HpcrTgzEncrypted(folderPath, encryptionCertificate string) (string, string, // HpcrContractSignedEncrypted - function to generate Signed and Encrypted contract func HpcrContractSignedEncrypted(contract, encryptionCertificate, privateKey string) (string, error) { + err := gen.VerifyContractWithSchema(contract) + if err != nil { + return "", fmt.Errorf("schema verification failed - %v", err) + } + if gen.CheckIfEmpty(contract, privateKey) { return "", fmt.Errorf(emptyParameterErrStatement) } @@ -107,6 +112,11 @@ func HpcrContractSignedEncrypted(contract, encryptionCertificate, privateKey str // HpcrContractSignedEncryptedContractExpiry - function to generate sign with contract expiry enabled and encrypt contract (with CSR parameters and CSR file) func HpcrContractSignedEncryptedContractExpiry(contract, encryptionCertificate, privateKey, cacert, caKey, csrDataStr, csrPemData string, expiryDays int) (string, error) { + err := gen.VerifyContractWithSchema(contract) + if err != nil { + return "", fmt.Errorf("schema verification failed - %v", err) + } + if gen.CheckIfEmpty(contract, privateKey, cacert, caKey) { return "", fmt.Errorf(emptyParameterErrStatement) }