Skip to content

Commit

Permalink
Merge pull request #39 from dipdup-io/feature/parse-cfg-with-custom-v…
Browse files Browse the repository at this point in the history
…alidator

Feature: add custom config validator
  • Loading branch information
aopoltorzhicky authored Oct 3, 2024
2 parents 8b16198 + 9ba27b9 commit f0fd77a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ func Parse(filename string, output Configurable) error {
return validator.New().Struct(output)
}

// ParseWithValidator - parse config with custom validator. If validator is nil validation will be skipped.
func ParseWithValidator(filename string, val *validator.Validate, output Configurable) error {
buf, err := readFile(filename)
if err != nil {
return err
}

if err := yaml.NewDecoder(buf).Decode(output); err != nil {
return err
}

if err := output.Substitute(); err != nil {
return err
}
if val != nil {
return val.Struct(output)
}
return nil
}

func readFile(filename string) (*bytes.Buffer, error) {
if filename == "" {
return nil, errors.Errorf("you have to provide configuration filename")
Expand Down

0 comments on commit f0fd77a

Please sign in to comment.