Przeglądaj źródła

add validator for postgresql addon schema

Mohammed Nafees 3 lat temu
rodzic
commit
52ade81093

+ 11 - 0
internal/integrations/preview/driver_validators.go

@@ -123,6 +123,17 @@ func deployDriverValidator(resource *types.Resource) error {
 				}
 			}
 		}
+	} else if source.Repo == "https://chart-addons.getporter.dev" {
+		if len(resource.Config) > 0 {
+			if source.Name == "postgresql" {
+				err := validatePostgresChartValues(resource.Config)
+
+				if err != nil {
+					return fmt.Errorf("for resource '%s': error validating values for postgresql deployment: %w",
+						resource.Name, err)
+				}
+			}
+		}
 	}
 
 	return nil

+ 25 - 0
internal/integrations/preview/schema_validate.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 
 	"github.com/santhosh-tekuri/jsonschema/v5"
+	_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
 )
 
 func validateWebChartValues(values map[string]interface{}) error {
@@ -90,3 +91,27 @@ func validateJobChartValues(values map[string]interface{}) error {
 
 	return scm.Validate(v)
 }
+
+func validatePostgresChartValues(values map[string]interface{}) error {
+	compiler := jsonschema.NewCompiler()
+
+	scm, err := compiler.Compile("https://raw.githubusercontent.com/porter-dev/porter-charts/master/addons/postgresql/values.schema.json")
+
+	if err != nil {
+		return fmt.Errorf("error compiling postgres chart values schema: %w", err)
+	}
+
+	jsonBytes, err := json.Marshal(values)
+
+	if err != nil {
+		return fmt.Errorf("error marshalling values to JSON: %w", err)
+	}
+
+	var v interface{}
+
+	if err := json.Unmarshal(jsonBytes, &v); err != nil {
+		return fmt.Errorf("error unmarshalling values JSON to interface: %w", err)
+	}
+
+	return scm.Validate(v)
+}