-
Notifications
You must be signed in to change notification settings - Fork 162
/
validator.go
64 lines (50 loc) · 1.67 KB
/
validator.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
package manifest
import (
"regexp"
"strings"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Validator interface {
Validate(Manifest) error
}
type validator struct {
logger boshlog.Logger
}
func NewValidator(logger boshlog.Logger) Validator {
return &validator{logger: logger}
}
func (v *validator) Validate(manifest Manifest) error {
errs := []error{}
releaseNames := map[string]struct{}{}
if len(manifest.Releases) < 1 {
errs = append(errs, bosherr.Errorf("releases must contain at least 1 release"))
}
protocolRegex := regexp.MustCompile("^(file|http|https)://")
for releaseIdx, release := range manifest.Releases {
if v.isBlank(release.Name) {
errs = append(errs, bosherr.Errorf("releases[%d].name must be provided", releaseIdx))
}
if _, found := releaseNames[release.Name]; found {
errs = append(errs, bosherr.Errorf("releases[%d].name '%s' must be unique", releaseIdx, release.Name))
}
releaseNames[release.Name] = struct{}{}
if v.isBlank(release.URL) {
errs = append(errs, bosherr.Errorf("releases[%d].url must be provided", releaseIdx))
}
matched := protocolRegex.MatchString(release.URL)
if !matched {
errs = append(errs, bosherr.Errorf("releases[%d].url must be a valid URL (file:// or http(s)://)", releaseIdx))
}
if strings.HasPrefix(release.URL, "http") && v.isBlank(release.SHA1) {
errs = append(errs, bosherr.Errorf("releases[%d].sha1 must be provided for http URL", releaseIdx))
}
}
if len(errs) > 0 {
return bosherr.NewMultiError(errs...)
}
return nil
}
func (v *validator) isBlank(str string) bool {
return str == "" || strings.TrimSpace(str) == ""
}