Skip to content

Commit

Permalink
Refactor status code validations
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Marrero committed Aug 2, 2019
1 parent 51ca059 commit 0c3c469
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
25 changes: 12 additions & 13 deletions pkg/apis/configuration/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,23 @@ func validateStatusMatch(s string, fieldPath *field.Path) field.ErrorList {
allErrs = append(allErrs, field.Invalid(fieldPath, s, "`!` can only appear once at the beginning"))
}
} else if strings.Contains(value, "-") {
statusRange := strings.Split(value, "-")

if len(statusRange) != 2 {
allErrs = append(allErrs, field.Invalid(fieldPath, s, "ranges must only have 2 numbers"))
} else {
if msg := validateStatusCodeRange(statusRange); msg != "" {
allErrs = append(allErrs, field.Invalid(fieldPath, s, msg))
}
if msg := validateStatusCodeRange(value); msg != "" {
allErrs = append(allErrs, field.Invalid(fieldPath, s, msg))
}
} else if msg := validateStatusCodeInRange(value); msg != "" {
} else if msg := validateStatusCode(value); msg != "" {
allErrs = append(allErrs, field.Invalid(fieldPath, s, msg))
}
}

return allErrs
}

func validateStatusCodeRange(statusRange []string) string {
func validateStatusCodeRange(statusRangeStr string) string {
statusRange := strings.Split(statusRangeStr, "-")
if len(statusRange) != 2 {
return "ranges must only have 2 numbers"
}

min, msg := validateIntFromString(statusRange[0])
if msg != "" {
return msg
Expand All @@ -200,7 +199,7 @@ func validateStatusCodeRange(statusRange []string) string {
}

for _, code := range statusRange {
if msg := validateStatusCodeInRange(code); msg != "" {
if msg := validateStatusCode(code); msg != "" {
return msg
}
}
Expand All @@ -221,7 +220,7 @@ func validateIntFromString(number string) (int, string) {
return int(numberInt), ""
}

func validateStatusCodeInRange(status string) string {
func validateStatusCode(status string) string {
code, errMsg := validateIntFromString(status)
if errMsg != "" {
return errMsg
Expand Down Expand Up @@ -252,7 +251,7 @@ func validateHeader(h v1alpha1.Header, fieldPath *field.Path) field.ErrorList {
return allErrs
}

const headerValueFmt = `([^"\\]|\\.)*`
const headerValueFmt = `([^"$\\]|\\.)*`
const headerValueFmtErrMsg string = `a valid header must have all '"' escaped and must not end with an unescaped '\'`

var headerValueFmtRegexp = regexp.MustCompile("^" + headerValueFmt + "$")
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/configuration/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,13 @@ func TestValidateHeaderFails(t *testing.T) {
},
msg: "Invalid value with ending '\\'",
},
{
header: v1alpha1.Header{
Name: "Host",
Value: "$my.service",
},
msg: "Invalid value with '$' character",
},
}
for _, test := range tests {
allErrs := validateHeader(test.header, field.NewPath("headers"))
Expand Down

0 comments on commit 0c3c469

Please sign in to comment.