diff options
author | 2020-08-21 00:11:26 +0000 | |
---|---|---|
committer | 2020-08-21 00:11:26 +0000 | |
commit | 38ed3fbf85636ad5f73538c2de07cce72499f5bf (patch) | |
tree | 0b0df9525e67e0180c00abaa078d07fc9b5d9e08 /src/orchestrator/pkg/infra | |
parent | a5ffa8aadf49bb92371f06797b264e0bf4c264e2 (diff) | |
parent | 6fc3a329aba55dfa2d4867bde9d8a3126b45f59a (diff) |
Merge "Add Validation for API's"
Diffstat (limited to 'src/orchestrator/pkg/infra')
-rw-r--r-- | src/orchestrator/pkg/infra/validation/validation.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/orchestrator/pkg/infra/validation/validation.go b/src/orchestrator/pkg/infra/validation/validation.go index c43d29ec..b1b7f20f 100644 --- a/src/orchestrator/pkg/infra/validation/validation.go +++ b/src/orchestrator/pkg/infra/validation/validation.go @@ -19,14 +19,20 @@ package validation import ( "archive/tar" "compress/gzip" + "encoding/json" "fmt" "io" + "io/ioutil" "net" + "net/http" + "os" "regexp" "strconv" "strings" + log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils" pkgerrors "github.com/pkg/errors" + "github.com/xeipuuv/gojsonschema" "k8s.io/apimachinery/pkg/util/validation" ) @@ -321,3 +327,50 @@ func IsValidParameterPresent(vars map[string]string, sp []string) error { return nil } + +// ValidateJsonSchemaData function validates the document against the Json Schema +func ValidateJsonSchemaData(jsonSchemaFile string, jsonData interface{}) (error, int) { + + // Read the Json Schema File + if _, err := os.Stat(jsonSchemaFile); err != nil { + if os.IsNotExist(err) { + err = pkgerrors.New("JsonSchemaValidation: File " + jsonSchemaFile + " not found") + } else { + err = pkgerrors.Wrap(err, "JsonSchemaValidation: Stat file error") + } + return err, http.StatusInternalServerError + } + rawBytes, err := ioutil.ReadFile(jsonSchemaFile) + if err != nil { + return pkgerrors.Wrap(err, "JsonSchemaValidation: Read JSON file error"), http.StatusInternalServerError + } + + // Json encode the data + req, err := json.Marshal(jsonData) + if err != nil { + return pkgerrors.Wrap(err, "JsonSchemaValidation, Request Body error"), http.StatusBadRequest + } + + // Load schema and document + schemaLoader := gojsonschema.NewStringLoader(string(rawBytes)) + s, err := gojsonschema.NewSchema(schemaLoader) + if err != nil { + return pkgerrors.Wrap(err, "JsonSchemaValidation: Validation error"), http.StatusInternalServerError + } + documentLoader := gojsonschema.NewStringLoader(string(req)) + result, err := s.Validate(documentLoader) + if err != nil { + return pkgerrors.Wrap(err, "JsonSchemaValidation: Validation error"), http.StatusInternalServerError + } + // Validate document against Json Schema + if !result.Valid() { + for _, desc := range result.Errors() { + log.Error("The document is not valid", log.Fields{ + "Error": desc.Description(), + }) + } + return pkgerrors.New("JsonSchemaValidation: Document Validation failed"), http.StatusBadRequest + } + + return nil, 0 +} |