-
Notifications
You must be signed in to change notification settings - Fork 1
/
uncors_config.go
74 lines (58 loc) · 1.65 KB
/
uncors_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package validators
import (
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/config/validators/base"
"github.com/gobuffalo/validate"
"github.com/spf13/afero"
)
type UncorsConfigValidator struct {
config *config.UncorsConfig
fs afero.Fs
}
func (u *UncorsConfigValidator) IsValid(errors *validate.Errors) {
errors.Append(validate.Validate(
&base.PortValidator{Field: "http-port", Value: u.config.HTTPPort},
))
if len(u.config.Mappings) == 0 {
errors.Add("mappings", "mappings must not be empty")
return
}
for i, mapping := range u.config.Mappings {
errors.Append(validate.Validate(&MappingValidator{
Field: joinPath("mappings", index(i)),
Value: mapping,
Fs: u.fs,
}))
}
if u.config.HTTPSPort != 0 {
errors.Append(validate.Validate(
&base.PortValidator{Field: "https-port", Value: u.config.HTTPSPort},
))
if u.config.CertFile == "" {
errors.Add("cert-file", "cert-file must be specified")
return
}
if u.config.KeyFile == "" {
errors.Add("key-file", "key-file must be specified")
return
}
errors.Append(validate.Validate(
&base.FileValidator{Field: "cert-file", Value: u.config.CertFile, Fs: u.fs},
&base.FileValidator{Field: "key-file", Value: u.config.KeyFile, Fs: u.fs},
))
}
errors.Append(validate.Validate(
&ProxyValidator{Field: "proxy", Value: u.config.Proxy},
&CacheConfigValidator{Field: "cache-config", Value: u.config.CacheConfig},
))
}
func ValidateConfig(config *config.UncorsConfig, fs afero.Fs) error {
errors := validate.Validate(&UncorsConfigValidator{
config: config,
fs: fs,
})
if errors.HasAny() {
return errors
}
return nil
}