summaryrefslogtreecommitdiffstats
path: root/src/orchestrator/pkg/infra/validation
diff options
context:
space:
mode:
authorRitu Sood <ritu.sood@intel.com>2020-08-04 14:46:34 -0700
committerRitu Sood <ritu.sood@intel.com>2020-08-20 11:42:12 -0700
commit6fc3a329aba55dfa2d4867bde9d8a3126b45f59a (patch)
tree21e39a03255198a8f0781a78ad9e2ade1f3c28ed /src/orchestrator/pkg/infra/validation
parent6e5ca4741dab0de3b4d89f410f0ff9d0313d6aee (diff)
Add Validation for API's
Adding input validations for API based on Json schemas Issue-ID: MULTICLOUD-1096 Signed-off-by: Ritu Sood <ritu.sood@intel.com> Change-Id: If6591bdef6305f87cbce7ef56d894376f687c6c1
Diffstat (limited to 'src/orchestrator/pkg/infra/validation')
-rw-r--r--src/orchestrator/pkg/infra/validation/validation.go53
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
+}