diff options
author | Matthew Holt <[email protected]> | 2019-04-03 11:41:36 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-04-03 11:41:36 -0600 |
commit | 3eae6d43b62db96c1d69e82c8d2997b6bf41beda (patch) | |
tree | f71a20214df572dec9fa2e481ddc4ee44b1381ca /modules.go | |
parent | 59a5d0db28e36b9732bc9ad2b73bcde9188e207c (diff) | |
download | caddy-3eae6d43b62db96c1d69e82c8d2997b6bf41beda.tar.gz caddy-3eae6d43b62db96c1d69e82c8d2997b6bf41beda.zip |
Add Validator interface
Modules can now verify their own configurations
Diffstat (limited to 'modules.go')
-rw-r--r-- | modules.go | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/modules.go b/modules.go index efd5d4ab8..46dfbe827 100644 --- a/modules.go +++ b/modules.go @@ -138,6 +138,13 @@ func LoadModule(name string, rawMsg json.RawMessage) (interface{}, error) { return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err) } + if validator, ok := val.(Validator); ok { + err := validator.Validate() + if err != nil { + return nil, fmt.Errorf("%s: invalid configuration: %v", mod.Name, err) + } + } + return val, nil } @@ -170,6 +177,15 @@ func LoadModuleInlineName(moduleScope string, raw json.RawMessage) (interface{}, return val, nil } +// Validator is implemented by modules which can verify that their +// configurations are valid. This method will be called after New() +// instantiations of modules (if implemented). Validation should +// always be fast (imperceptible running time) and an error should +// be returned only if the value's configuration is invalid. +type Validator interface { + Validate() error +} + var ( modules = make(map[string]Module) modulesMu sync.Mutex |