Skip to content

Commit a8db15b

Browse files
sgallagherpweil-
authored andcommitted
Add details for field.Required
Some fields are only required if other fields have certain values. This patch adds some additional error messaging to inform the user why these fields were necessary. Fixes issue #7118 Signed-off-by: Stephen Gallagher <[email protected]>
1 parent 8974c3c commit a8db15b

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

pkg/authorization/api/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func validateRoleBindingSubject(subject kapi.ObjectReference, isNamespaced bool,
297297
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), subject.Name, strings.Join(reasons, ", ")))
298298
}
299299
if !isNamespaced && len(subject.Namespace) == 0 {
300-
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), ""))
300+
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), "Service account subjects for ClusterRoleBindings must have a namespace"))
301301
}
302302

303303
case authorizationapi.UserKind:

pkg/cmd/server/api/validation/etcd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ValidateEtcdConnectionInfo(config api.EtcdConnectionInfo, server *api.EtcdC
4242
// Require a client cert to connect to an etcd that requires client certs
4343
if len(server.ServingInfo.ClientCA) > 0 {
4444
if len(config.ClientCert.CertFile) == 0 {
45-
allErrs = append(allErrs, field.Required(fldPath.Child("certFile"), ""))
45+
allErrs = append(allErrs, field.Required(fldPath.Child("certFile"), "A client certificate must be provided for this etcd server"))
4646
}
4747
}
4848

pkg/cmd/server/api/validation/oauth.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,11 @@ func ValidateRequestHeaderIdentityProvider(provider *api.RequestHeaderIdentityPr
243243
validationResults.AddErrors(field.Required(fieldPath.Child("provider", "headers"), ""))
244244
}
245245
if identityProvider.UseAsChallenger && len(provider.ChallengeURL) == 0 {
246-
err := field.Required(fieldPath.Child("provider", "challengeURL"), "")
247-
err.Detail = "challengeURL is required if challenge=true"
246+
err := field.Required(fieldPath.Child("provider", "challengeURL"), "challengeURL is required if challenge is true")
248247
validationResults.AddErrors(err)
249248
}
250249
if identityProvider.UseAsLogin && len(provider.LoginURL) == 0 {
251-
err := field.Required(fieldPath.Child("provider", "loginURL"), "")
252-
err.Detail = "loginURL is required if login=true"
250+
err := field.Required(fieldPath.Child("provider", "loginURL"), "loginURL is required if login=true")
253251
validationResults.AddErrors(err)
254252
}
255253

pkg/cmd/server/api/validation/validation.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,20 @@ func ValidateHostPort(value string, fldPath *field.Path) field.ErrorList {
7676
func ValidateCertInfo(certInfo api.CertInfo, required bool, fldPath *field.Path) field.ErrorList {
7777
allErrs := field.ErrorList{}
7878

79-
if required || len(certInfo.CertFile) > 0 || len(certInfo.KeyFile) > 0 {
79+
if required {
8080
if len(certInfo.CertFile) == 0 {
81-
allErrs = append(allErrs, field.Required(fldPath.Child("certFile"), ""))
81+
allErrs = append(allErrs, field.Required(fldPath.Child("certFile"), "The certificate file must be provided"))
8282
}
8383
if len(certInfo.KeyFile) == 0 {
84-
allErrs = append(allErrs, field.Required(fldPath.Child("keyFile"), ""))
84+
allErrs = append(allErrs, field.Required(fldPath.Child("keyFile"), "The certificate key must be provided"))
8585
}
8686
}
8787

88+
if (len(certInfo.CertFile) == 0) != (len(certInfo.KeyFile) == 0) {
89+
allErrs = append(allErrs, field.Required(fldPath.Child("certFile"), "Both the certificate file and the certificate key must be provided together or not at all"))
90+
allErrs = append(allErrs, field.Required(fldPath.Child("keyFile"), "Both the certificate file and the certificate key must be provided together or not at all"))
91+
}
92+
8893
if len(certInfo.CertFile) > 0 {
8994
allErrs = append(allErrs, ValidateFile(certInfo.CertFile, fldPath.Child("certFile"))...)
9095
}

pkg/image/api/validation/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func ValidateImageStreamTagReference(tagRef api.TagReference, fldPath *field.Pat
192192
var errs field.ErrorList
193193
if tagRef.From != nil {
194194
if len(tagRef.From.Name) == 0 {
195-
errs = append(errs, field.Required(fldPath.Child("from", "name"), "name is required"))
195+
errs = append(errs, field.Required(fldPath.Child("from", "name"), ""))
196196
}
197197
switch tagRef.From.Kind {
198198
case "DockerImage":
@@ -324,7 +324,7 @@ func ValidateImageStreamImport(isi *api.ImageStreamImport) field.ErrorList {
324324
switch from.Kind {
325325
case "DockerImage":
326326
if len(spec.From.Name) == 0 {
327-
errs = append(errs, field.Required(repoPath.Child("from", "name"), ""))
327+
errs = append(errs, field.Required(repoPath.Child("from", "name"), "Docker image references require a name"))
328328
} else {
329329
if ref, err := api.ParseDockerImageReference(from.Name); err != nil {
330330
errs = append(errs, field.Invalid(repoPath.Child("from", "name"), from.Name, err.Error()))

pkg/user/api/validation/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ func ValidateIdentity(identity *api.Identity) field.ErrorList {
155155
allErrs = append(allErrs, field.Invalid(userPath.Child("name"), identity.User.Name, strings.Join(reasons, ", ")))
156156
}
157157
if len(identity.User.Name) == 0 && len(identity.User.UID) != 0 {
158-
allErrs = append(allErrs, field.Invalid(userPath.Child("uid"), identity.User.UID, "may not be set if user.name is empty"))
158+
allErrs = append(allErrs, field.Invalid(userPath.Child("username"), "username is required when uid is provided"))
159159
}
160160
if len(identity.User.Name) != 0 && len(identity.User.UID) == 0 {
161-
allErrs = append(allErrs, field.Required(userPath.Child("uid"), ""))
161+
allErrs = append(allErrs, field.Required(userPath.Child("uid"), "uid is required when username is provided"))
162162
}
163163
return allErrs
164164
}

0 commit comments

Comments
 (0)