schema_validate.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package preview
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/santhosh-tekuri/jsonschema/v5"
  6. _ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
  7. )
  8. func validateWebChartValues(values map[string]interface{}) error {
  9. compiler := jsonschema.NewCompiler()
  10. scm, err := compiler.Compile("https://raw.githubusercontent.com/porter-dev/porter-charts/master/applications/web/validate.json")
  11. if err != nil {
  12. return fmt.Errorf("error compiling job chart values schema: %w", err)
  13. }
  14. jsonBytes, err := json.Marshal(values)
  15. if err != nil {
  16. return fmt.Errorf("error marshalling values to JSON: %w", err)
  17. }
  18. var v interface{}
  19. if err := json.Unmarshal(jsonBytes, &v); err != nil {
  20. return fmt.Errorf("error unmarshalling values JSON to interface: %w", err)
  21. }
  22. return scm.Validate(v)
  23. }
  24. func validateWorkerChartValues(values map[string]interface{}) error {
  25. compiler := jsonschema.NewCompiler()
  26. scm, err := compiler.Compile("https://raw.githubusercontent.com/porter-dev/porter-charts/master/applications/worker/validate.json")
  27. if err != nil {
  28. return fmt.Errorf("error compiling job chart values schema: %w", err)
  29. }
  30. jsonBytes, err := json.Marshal(values)
  31. if err != nil {
  32. return fmt.Errorf("error marshalling values to JSON: %w", err)
  33. }
  34. var v interface{}
  35. if err := json.Unmarshal(jsonBytes, &v); err != nil {
  36. return fmt.Errorf("error unmarshalling values JSON to interface: %w", err)
  37. }
  38. return scm.Validate(v)
  39. }
  40. func validateJobChartValues(values map[string]interface{}) error {
  41. compiler := jsonschema.NewCompiler()
  42. scm, err := compiler.Compile("https://raw.githubusercontent.com/porter-dev/porter-charts/master/applications/job/validate.json")
  43. if err != nil {
  44. return fmt.Errorf("error compiling job chart values schema: %w", err)
  45. }
  46. jsonBytes, err := json.Marshal(values)
  47. if err != nil {
  48. return fmt.Errorf("error marshalling values to JSON: %w", err)
  49. }
  50. var v interface{}
  51. if err := json.Unmarshal(jsonBytes, &v); err != nil {
  52. return fmt.Errorf("error unmarshalling values JSON to interface: %w", err)
  53. }
  54. return scm.Validate(v)
  55. }
  56. func validatePostgresChartValues(values map[string]interface{}) error {
  57. compiler := jsonschema.NewCompiler()
  58. scm, err := compiler.Compile("https://raw.githubusercontent.com/porter-dev/porter-charts/master/addons/postgresql/values.schema.json")
  59. if err != nil {
  60. return fmt.Errorf("error compiling postgres chart values schema: %w", err)
  61. }
  62. jsonBytes, err := json.Marshal(values)
  63. if err != nil {
  64. return fmt.Errorf("error marshalling values to JSON: %w", err)
  65. }
  66. var v interface{}
  67. if err := json.Unmarshal(jsonBytes, &v); err != nil {
  68. return fmt.Errorf("error unmarshalling values JSON to interface: %w", err)
  69. }
  70. return scm.Validate(v)
  71. }