Make CIdentifier return error strings

pull/6/head
Tim Hockin 2015-12-19 22:52:48 -08:00
parent d07328dc4a
commit 189d4a5159
5 changed files with 18 additions and 11 deletions

View File

@ -51,7 +51,6 @@ var RepairMalformedUpdates bool = true
const isNegativeErrorMsg string = `must be greater than or equal to 0` const isNegativeErrorMsg string = `must be greater than or equal to 0`
const isInvalidQuotaResource string = `must be a standard resource for quota` const isInvalidQuotaResource string = `must be a standard resource for quota`
const fieldImmutableErrorMsg string = `field is immutable` const fieldImmutableErrorMsg string = `field is immutable`
const cIdentifierErrorMsg string = `must be a C identifier (matching regex ` + validation.CIdentifierFmt + `): e.g. "my_name" or "MyName"`
const isNotIntegerErrorMsg string = `must be an integer` const isNotIntegerErrorMsg string = `must be an integer`
func InclusiveRangeErrorMsg(lo, hi int) string { func InclusiveRangeErrorMsg(lo, hi int) string {
@ -1087,8 +1086,10 @@ func validateEnv(vars []api.EnvVar, fldPath *field.Path) field.ErrorList {
idxPath := fldPath.Index(i) idxPath := fldPath.Index(i)
if len(ev.Name) == 0 { if len(ev.Name) == 0 {
allErrs = append(allErrs, field.Required(idxPath.Child("name"), "")) allErrs = append(allErrs, field.Required(idxPath.Child("name"), ""))
} else if !validation.IsCIdentifier(ev.Name) { } else {
allErrs = append(allErrs, field.Invalid(idxPath.Child("name"), ev.Name, cIdentifierErrorMsg)) for _, msg := range validation.IsCIdentifier(ev.Name) {
allErrs = append(allErrs, field.Invalid(idxPath.Child("name"), ev.Name, msg))
}
} }
allErrs = append(allErrs, validateEnvVarValueFrom(ev, idxPath.Child("valueFrom"))...) allErrs = append(allErrs, validateEnvVarValueFrom(ev, idxPath.Child("valueFrom"))...)
} }

View File

@ -1155,7 +1155,7 @@ func TestValidateEnv(t *testing.T) {
{ {
name: "name not a C identifier", name: "name not a C identifier",
envs: []api.EnvVar{{Name: "a.b.c"}}, envs: []api.EnvVar{{Name: "a.b.c"}},
expectedError: `[0].name: Invalid value: "a.b.c": must be a C identifier (matching regex [A-Za-z_][A-Za-z0-9_]*): e.g. "my_name" or "MyName"`, expectedError: `[0].name: Invalid value: "a.b.c": must match the regex`,
}, },
{ {
name: "value and valueFrom specified", name: "value and valueFrom specified",

View File

@ -835,7 +835,10 @@ func parseEnvs(envArray []string) ([]api.EnvVar, error) {
} }
name := env[:pos] name := env[:pos]
value := env[pos+1:] value := env[pos+1:]
if len(name) == 0 || !validation.IsCIdentifier(name) || len(value) == 0 { if len(name) == 0 || len(value) == 0 {
return nil, fmt.Errorf("invalid env: %v", env)
}
if len(validation.IsCIdentifier(name)) != 0 {
return nil, fmt.Errorf("invalid env: %v", env) return nil, fmt.Errorf("invalid env: %v", env)
} }
envVar := api.EnvVar{Name: name, Value: value} envVar := api.EnvVar{Name: name, Value: value}
@ -853,7 +856,7 @@ func parseV1Envs(envArray []string) ([]v1.EnvVar, error) {
} }
name := env[:pos] name := env[:pos]
value := env[pos+1:] value := env[pos+1:]
if len(name) == 0 || !validation.IsCIdentifier(name) || len(value) == 0 { if len(name) == 0 || len(validation.IsCIdentifier(name)) != 0 || len(value) == 0 {
return nil, fmt.Errorf("invalid env: %v", env) return nil, fmt.Errorf("invalid env: %v", env)
} }
envVar := v1.EnvVar{Name: name, Value: value} envVar := v1.EnvVar{Name: name, Value: value}

View File

@ -145,8 +145,11 @@ var cIdentifierRegexp = regexp.MustCompile("^" + CIdentifierFmt + "$")
// IsCIdentifier tests for a string that conforms the definition of an identifier // IsCIdentifier tests for a string that conforms the definition of an identifier
// in C. This checks the format, but not the length. // in C. This checks the format, but not the length.
func IsCIdentifier(value string) bool { func IsCIdentifier(value string) []string {
return cIdentifierRegexp.MatchString(value) if !cIdentifierRegexp.MatchString(value) {
return []string{RegexError(CIdentifierFmt, "my_name", "MY_NAME", "MyName")}
}
return nil
} }
// IsValidPortNum tests that the argument is a valid, non-zero port number. // IsValidPortNum tests that the argument is a valid, non-zero port number.

View File

@ -119,8 +119,8 @@ func TestIsCIdentifier(t *testing.T) {
"A", "AB", "AbC", "A1", "_A", "A_", "A_B", "A_1", "A__1__2__B", "__123_ABC", "A", "AB", "AbC", "A1", "_A", "A_", "A_B", "A_1", "A__1__2__B", "__123_ABC",
} }
for _, val := range goodValues { for _, val := range goodValues {
if !IsCIdentifier(val) { if msgs := IsCIdentifier(val); len(msgs) != 0 {
t.Errorf("expected true for '%s'", val) t.Errorf("expected true for '%s': %v", val, msgs)
} }
} }
@ -132,7 +132,7 @@ func TestIsCIdentifier(t *testing.T) {
"#a#", "#a#",
} }
for _, val := range badValues { for _, val := range badValues {
if IsCIdentifier(val) { if msgs := IsCIdentifier(val); len(msgs) == 0 {
t.Errorf("expected false for '%s'", val) t.Errorf("expected false for '%s'", val)
} }
} }