Skip to content

Commit

Permalink
Merge pull request #5786 from fgrzadkowski/better_errors
Browse files Browse the repository at this point in the history
Add validation error explanation for long annotations.
  • Loading branch information
saad-ali committed Mar 24, 2015
2 parents b01f1f9 + 6bc1e2e commit 282f605
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 12 deletions.
5 changes: 1 addition & 4 deletions pkg/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,10 @@ func ValidateAnnotations(annotations map[string]string, field string) errs.Valid
if !util.IsQualifiedName(strings.ToLower(k)) {
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, qualifiedNameErrorMsg))
}
if !util.IsValidAnnotationValue(v) {
allErrs = append(allErrs, errs.NewFieldInvalid(field, k, ""))
}
totalSize += (int64)(len(k)) + (int64)(len(v))
}
if totalSize > (int64)(totalAnnotationSizeLimitB) {
allErrs = append(allErrs, errs.NewFieldTooLong("annotations", ""))
allErrs = append(allErrs, errs.NewFieldTooLong(field, "", totalAnnotationSizeLimitB))
}
return allErrs
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/fielderrors/fielderrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var _ error = &ValidationError{}
func (v *ValidationError) Error() string {
var s string
switch v.Type {
case ValidationErrorTypeRequired:
case ValidationErrorTypeRequired, ValidationErrorTypeTooLong:
s = spew.Sprintf("%s: %s", v.Field, v.Type)
default:
s = spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue)
Expand Down Expand Up @@ -133,8 +133,8 @@ func NewFieldNotFound(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeNotFound, field, value, ""}
}

func NewFieldTooLong(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeTooLong, field, value, ""}
func NewFieldTooLong(field string, value interface{}, maxLength int) *ValidationError {
return &ValidationError{ValidationErrorTypeTooLong, field, value, fmt.Sprintf("must have at most %d characters", maxLength)}
}

type ValidationErrorList []error
Expand Down
5 changes: 0 additions & 5 deletions pkg/util/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func IsValidLabelValue(value string) bool {
return (len(value) <= LabelValueMaxLength && labelValueRegexp.MatchString(value))
}

// Annotation values are opaque.
func IsValidAnnotationValue(value string) bool {
return true
}

const QualifiedNameFmt string = "(" + qnameTokenFmt + "/)?" + qnameTokenFmt
const QualifiedNameMaxLength int = 253

Expand Down

0 comments on commit 282f605

Please sign in to comment.