Browse Source
This commit introduces the Validator interface, which is intended to be implemented by any sub configs. It specifies a Validate() error method that should fail if a sub configuration contains any invalid or insane parameters. In addition, a package-level Validate method can be used to check a variadic number of sub configs implementing the Validator interface. This allows the primary config struct to be extended via targeted and/or specialized sub configs, and validate all of them in sequence without bloating the main package with the actual validation logic.master
Conner Fromknecht
5 years ago
2 changed files with 24 additions and 0 deletions
@ -0,0 +1,21 @@
|
||||
package lncfg |
||||
|
||||
// Validator is a generic interface for validating sub configurations.
|
||||
type Validator interface { |
||||
// Validate returns an error if a particular configuration is invalid or
|
||||
// insane.
|
||||
Validate() error |
||||
} |
||||
|
||||
// Validate accepts a variadic list of Validators and checks that each one
|
||||
// passes its Validate method. An error is returned from the first Validator
|
||||
// that fails.
|
||||
func Validate(validators ...Validator) error { |
||||
for _, validator := range validators { |
||||
if err := validator.Validate(); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
Loading…
Reference in new issue