Skip to content

Commit

Permalink
[config] Unmarshall YAML before validating it
Browse files Browse the repository at this point in the history
This way consumers can use the validation function without needing to use Cosmo to directly read the file.
  • Loading branch information
clayne11 committed Sep 19, 2024
1 parent 378ccc6 commit 5854d9e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
16 changes: 9 additions & 7 deletions router/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ type Config struct {
PersistedOperationsConfig PersistedOperationsConfig `yaml:"persisted_operations"`
}

func (c *Config) Validate() error {
return validateConfig(c, JSONSchema)
}

type LoadResult struct {
Config Config
DefaultLoaded bool
Expand Down Expand Up @@ -685,16 +689,14 @@ func LoadConfig(configFilePath string, envOverride string) (*LoadResult, error)

configFileBytes = []byte(configYamlData)

err = ValidateConfig(configFileBytes, JSONSchema)
if err != nil {
return nil, fmt.Errorf("router config validation error: %w", err)
}

// Unmarshal the final config

if err := yaml.Unmarshal(configFileBytes, &cfg.Config); err != nil {
return nil, err
}

err = cfg.Config.Validate()
if err != nil {
return nil, fmt.Errorf("router config validation error: %w", err)
}
}

// Post-process the config
Expand Down
12 changes: 3 additions & 9 deletions router/pkg/config/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/goccy/go-json"
"github.com/goccy/go-yaml"
"github.com/santhosh-tekuri/jsonschema/v6"
"golang.org/x/text/message"
"io/fs"
Expand Down Expand Up @@ -82,7 +81,7 @@ func goDurationVocab() *jsonschema.Vocabulary {
"properties": {
"minimum": {
"type": "string"
},
},
"maximum": {
"type": "string"
}
Expand Down Expand Up @@ -217,7 +216,7 @@ func humanBytesVocab() *jsonschema.Vocabulary {
"properties": {
"minimum": {
"type": "string"
},
},
"minimum": {
"type": "string"
}
Expand Down Expand Up @@ -286,18 +285,13 @@ var (
hostnameRegexRFC1123 = regexp.MustCompile(hostnameRegexStringRFC1123)
)

func ValidateConfig(yamlData []byte, schema []byte) error {
func validateConfig(v *Config, schema []byte) error {
var s any
err := json.Unmarshal(schema, &s)
if err != nil {
return err
}

var v any
if err := yaml.Unmarshal(yamlData, &v); err != nil {
log.Fatal(err)
}

c := jsonschema.NewCompiler()
c.AssertFormat()
c.AssertVocabs()
Expand Down

0 comments on commit 5854d9e

Please sign in to comment.