forked from openshift/origin
/
validation.go
73 lines (66 loc) · 2.1 KB
/
validation.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
package validation
import (
"regexp"
"strings"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
routeapi "github.com/openshift/origin/pkg/route/api"
routevalidation "github.com/openshift/origin/pkg/route/api/validation"
"github.com/openshift/origin/pkg/template/api"
)
var parameterNameExp = regexp.MustCompile(`^[a-zA-Z0-9\_]+$`)
// ValidateParameter tests if required fields in the Parameter are set.
func ValidateParameter(param *api.Parameter) (errs errors.ErrorList) {
if len(param.Name) == 0 {
errs = append(errs, errors.NewFieldRequired("name", ""))
return
}
if !parameterNameExp.MatchString(param.Name) {
errs = append(errs, errors.NewFieldInvalid("name", param.Name))
}
return
}
// ValidateTemplate tests if required fields in the Template are set.
func ValidateTemplate(template *api.Template) (errs errors.ErrorList) {
if len(template.ID) == 0 {
errs = append(errs, errors.NewFieldRequired("id", template.ID))
}
for i, item := range template.Items {
err := errors.ErrorList{}
switch obj := item.Object.(type) {
case *kapi.ReplicationController:
err = validation.ValidateReplicationController(obj)
case *kapi.Pod:
err = validation.ValidatePod(obj)
case *kapi.Service:
err = validation.ValidateService(obj)
case *routeapi.Route:
err = routevalidation.ValidateRoute(obj)
default:
// Pass-through unknown types.
}
// ignore namespace validation errors in templates
err = filter(err, "namespace")
errs = append(errs, err.PrefixIndex(i).Prefix("items")...)
}
for i := range template.Parameters {
paramErr := ValidateParameter(&template.Parameters[i])
errs = append(errs, paramErr.PrefixIndex(i).Prefix("parameters")...)
}
return
}
func filter(errs errors.ErrorList, prefix string) errors.ErrorList {
if errs == nil {
return errs
}
next := errors.ErrorList{}
for _, err := range errs {
ve, ok := err.(errors.ValidationError)
if ok && strings.HasPrefix(ve.Field, prefix) {
continue
}
next = append(next, err)
}
return next
}