From 9ba27b9a6e18ee21e9167b40bca1cdd255dbb381 Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 3 Oct 2024 15:29:13 +0300 Subject: [PATCH] Feature: add custom config validator --- config/config.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/config/config.go b/config/config.go index 46b9818..cd49b43 100644 --- a/config/config.go +++ b/config/config.go @@ -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")