Skip to content

Commit 60a42d7

Browse files
author
OpenShift Bot
authored
Merge pull request #829 from csrwng/validate_imageref
Merged by openshift-bot
2 parents 2541220 + 4b813e7 commit 60a42d7

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

pkg/api/validation/validation.go

+30-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"strings"
66

7+
"github.com/docker/distribution/reference"
8+
79
"github.com/openshift/source-to-image/pkg/api"
810
)
911

@@ -31,6 +33,11 @@ func ValidateConfig(config *api.Config) []Error {
3133
}
3234
}
3335
}
36+
if config.Tag != "" {
37+
if err := validateDockerReference(config.Tag); err != nil {
38+
allErrs = append(allErrs, NewFieldInvalidValueWithReason("tag", err.Error()))
39+
}
40+
}
3441
return allErrs
3542
}
3643

@@ -50,14 +57,24 @@ func validateDockerNetworkMode(mode api.DockerNetworkMode) bool {
5057
return false
5158
}
5259

60+
func validateDockerReference(ref string) error {
61+
_, err := reference.Parse(ref)
62+
return err
63+
}
64+
5365
// NewFieldRequired returns a *ValidationError indicating "value required"
5466
func NewFieldRequired(field string) Error {
55-
return Error{ErrorTypeRequired, field}
67+
return Error{Type: ErrorTypeRequired, Field: field}
5668
}
5769

5870
// NewFieldInvalidValue returns a ValidationError indicating "invalid value"
5971
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}
6178
}
6279

6380
// ErrorType is a machine readable value providing more detail about why a field
@@ -77,17 +94,23 @@ const (
7794
// Error is an implementation of the 'error' interface, which represents an
7895
// error of validation.
7996
type Error struct {
80-
Type ErrorType
81-
Field string
97+
Type ErrorType
98+
Field string
99+
Reason string
82100
}
83101

84102
func (v Error) Error() string {
103+
var msg string
85104
switch v.Type {
86105
case ErrorInvalidValue:
87-
return fmt.Sprintf("Invalid value specified for %q", v.Field)
106+
msg = fmt.Sprintf("Invalid value specified for %q", v.Field)
88107
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)
90109
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)
92114
}
115+
return msg
93116
}

pkg/api/validation/validation_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestValidation(t *testing.T) {
3030
DockerNetworkMode: "foobar",
3131
BuilderPullPolicy: api.DefaultBuilderPullPolicy,
3232
},
33-
[]Error{{ErrorInvalidValue, "dockerNetworkMode"}},
33+
[]Error{{Type: ErrorInvalidValue, Field: "dockerNetworkMode"}},
3434
},
3535
{
3636
&api.Config{
@@ -90,7 +90,7 @@ func TestValidation(t *testing.T) {
9090
BuilderPullPolicy: api.DefaultBuilderPullPolicy,
9191
Labels: map[string]string{"some": "thing", "": "emptykey"},
9292
},
93-
[]Error{{ErrorInvalidValue, "labels"}},
93+
[]Error{{Type: ErrorInvalidValue, Field: "labels"}},
9494
},
9595
}
9696
for _, test := range testCases {

0 commit comments

Comments
 (0)