Skip to content

Commit f0fd77a

Browse files
Merge pull request #39 from dipdup-io/feature/parse-cfg-with-custom-validator
Feature: add custom config validator
2 parents 8b16198 + 9ba27b9 commit f0fd77a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

config/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,26 @@ func Parse(filename string, output Configurable) error {
127127
return validator.New().Struct(output)
128128
}
129129

130+
// ParseWithValidator - parse config with custom validator. If validator is nil validation will be skipped.
131+
func ParseWithValidator(filename string, val *validator.Validate, output Configurable) error {
132+
buf, err := readFile(filename)
133+
if err != nil {
134+
return err
135+
}
136+
137+
if err := yaml.NewDecoder(buf).Decode(output); err != nil {
138+
return err
139+
}
140+
141+
if err := output.Substitute(); err != nil {
142+
return err
143+
}
144+
if val != nil {
145+
return val.Struct(output)
146+
}
147+
return nil
148+
}
149+
130150
func readFile(filename string) (*bytes.Buffer, error) {
131151
if filename == "" {
132152
return nil, errors.Errorf("you have to provide configuration filename")

0 commit comments

Comments
 (0)