2
0

schema_validate.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }