Skip to content

Commit

Permalink
Add more context about image tag invalid error (#35)
Browse files Browse the repository at this point in the history
Signed-off-by: rick <[email protected]>

Co-authored-by: rick <[email protected]>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Jun 10, 2021
1 parent 14ac69a commit 67fc970
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ func ValidateConfig(config *api.Config) []Error {
switch config.BuilderPullPolicy {
case api.PullNever, api.PullAlways, api.PullIfNotPresent:
default:
allErrs = append(allErrs, NewFieldInvalidValue("builderPullPolicy"))
allErrs = append(allErrs, NewFieldInvalidValueObj("builderPullPolicy", config.BuilderPullPolicy))
}
if config.DockerConfig == nil || len(config.DockerConfig.Endpoint) == 0 {
allErrs = append(allErrs, NewFieldRequired("dockerConfig.endpoint"))
}
if config.DockerNetworkMode != "" && !validateDockerNetworkMode(config.DockerNetworkMode) {
allErrs = append(allErrs, NewFieldInvalidValue("dockerNetworkMode"))
allErrs = append(allErrs, NewFieldInvalidValueObj("dockerNetworkMode", config.DockerNetworkMode))
}
if config.Labels != nil {
for k := range config.Labels {
if len(k) == 0 {
allErrs = append(allErrs, NewFieldInvalidValue("labels"))
allErrs = append(allErrs, NewFieldInvalidValueWithReason("labels", "contains empty label"))
}
}
}
if config.Tag != "" {
if err := validateDockerReference(config.Tag); err != nil {
allErrs = append(allErrs, NewFieldInvalidValueWithReason("tag", err.Error()))
allErrs = append(allErrs, NewFieldInvalidValueWithReasonAndValue("tag", err.Error(), config.Tag))
}
}
return allErrs
Expand Down Expand Up @@ -72,11 +72,21 @@ func NewFieldInvalidValue(field string) Error {
return Error{Type: ErrorInvalidValue, Field: field}
}

// NewFieldInvalidValueObj returns a ValidationError indicating "invalid value"
func NewFieldInvalidValueObj(field string, value interface{}) Error {
return Error{Type: ErrorInvalidValue, Field: field, Value: value}
}

// NewFieldInvalidValueWithReason returns a ValidationError indicating "invalid value" and a reason for the error
func NewFieldInvalidValueWithReason(field, reason string) Error {
return Error{Type: ErrorInvalidValue, Field: field, Reason: reason}
}

// NewFieldInvalidValueWithReasonAndValue returns a ValidationError indicating the value and reason
func NewFieldInvalidValueWithReasonAndValue(field, reason string, value interface{}) Error {
return Error{Type: ErrorInvalidValue, Field: field, Value: value, Reason: reason}
}

// ErrorType is a machine readable value providing more detail about why a field
// is invalid.
type ErrorType string
Expand All @@ -96,14 +106,15 @@ const (
type Error struct {
Type ErrorType
Field string
Value interface{}
Reason string
}

func (v Error) Error() string {
var msg string
switch v.Type {
case ErrorInvalidValue:
msg = fmt.Sprintf("Invalid value specified for %q", v.Field)
msg = fmt.Sprintf("Invalid value specified for %q, value: %v", v.Field, v.Value)
case ErrorTypeRequired:
msg = fmt.Sprintf("Required value not specified for %q", v.Field)
default:
Expand Down

0 comments on commit 67fc970

Please sign in to comment.