4
4
"fmt"
5
5
"strings"
6
6
7
+ "github.com/docker/distribution/reference"
8
+
7
9
"github.com/openshift/source-to-image/pkg/api"
8
10
)
9
11
@@ -31,6 +33,11 @@ func ValidateConfig(config *api.Config) []Error {
31
33
}
32
34
}
33
35
}
36
+ if config .Tag != "" {
37
+ if err := validateDockerReference (config .Tag ); err != nil {
38
+ allErrs = append (allErrs , NewFieldInvalidValueWithReason ("tag" , err .Error ()))
39
+ }
40
+ }
34
41
return allErrs
35
42
}
36
43
@@ -50,14 +57,24 @@ func validateDockerNetworkMode(mode api.DockerNetworkMode) bool {
50
57
return false
51
58
}
52
59
60
+ func validateDockerReference (ref string ) error {
61
+ _ , err := reference .Parse (ref )
62
+ return err
63
+ }
64
+
53
65
// NewFieldRequired returns a *ValidationError indicating "value required"
54
66
func NewFieldRequired (field string ) Error {
55
- return Error {ErrorTypeRequired , field }
67
+ return Error {Type : ErrorTypeRequired , Field : field }
56
68
}
57
69
58
70
// NewFieldInvalidValue returns a ValidationError indicating "invalid value"
59
71
func NewFieldInvalidValue (field string ) Error {
60
- return Error {ErrorInvalidValue , field }
72
+ return Error {Type : ErrorInvalidValue , Field : field }
73
+ }
74
+
75
+ // NewFieldInvalidValueWithReason returns a ValidationError indicating "invalid value" and a reason for the error
76
+ func NewFieldInvalidValueWithReason (field , reason string ) Error {
77
+ return Error {Type : ErrorInvalidValue , Field : field , Reason : reason }
61
78
}
62
79
63
80
// ErrorType is a machine readable value providing more detail about why a field
@@ -77,17 +94,23 @@ const (
77
94
// Error is an implementation of the 'error' interface, which represents an
78
95
// error of validation.
79
96
type Error struct {
80
- Type ErrorType
81
- Field string
97
+ Type ErrorType
98
+ Field string
99
+ Reason string
82
100
}
83
101
84
102
func (v Error ) Error () string {
103
+ var msg string
85
104
switch v .Type {
86
105
case ErrorInvalidValue :
87
- return fmt .Sprintf ("Invalid value specified for %q" , v .Field )
106
+ msg = fmt .Sprintf ("Invalid value specified for %q" , v .Field )
88
107
case ErrorTypeRequired :
89
- return fmt .Sprintf ("Required value not specified for %q" , v .Field )
108
+ msg = fmt .Sprintf ("Required value not specified for %q" , v .Field )
90
109
default :
91
- return fmt .Sprintf ("%s: %s" , v .Type , v .Field )
110
+ msg = fmt .Sprintf ("%s: %s" , v .Type , v .Field )
111
+ }
112
+ if len (v .Reason ) > 0 {
113
+ msg = fmt .Sprintf ("%s: %s" , msg , v .Reason )
92
114
}
115
+ return msg
93
116
}
0 commit comments