Skip to content

Commit 810facb

Browse files
committed
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 7d6a536 commit 810facb

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
@@ -296,7 +296,7 @@ func validateRoleBindingSubject(subject kapi.ObjectReference, isNamespaced bool,
296296
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), subject.Name, reason))
297297
}
298298
if !isNamespaced && len(subject.Namespace) == 0 {
299-
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), ""))
299+
allErrs = append(allErrs, field.Required(fldPath.Child("namespace"), "Service account subjects for ClusterRoleBindings must have a namespace"))
300300
}
301301

302302
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
@@ -161,7 +161,7 @@ func ValidateImageStreamTagReference(tagRef api.TagReference, fldPath *field.Pat
161161
var errs field.ErrorList
162162
if tagRef.From != nil {
163163
if len(tagRef.From.Name) == 0 {
164-
errs = append(errs, field.Required(fldPath.Child("from", "name"), "name is required"))
164+
errs = append(errs, field.Required(fldPath.Child("from", "name"), ""))
165165
}
166166
switch tagRef.From.Kind {
167167
case "DockerImage":
@@ -293,7 +293,7 @@ func ValidateImageStreamImport(isi *api.ImageStreamImport) field.ErrorList {
293293
switch from.Kind {
294294
case "DockerImage":
295295
if len(spec.From.Name) == 0 {
296-
errs = append(errs, field.Required(repoPath.Child("from", "name"), ""))
296+
errs = append(errs, field.Required(repoPath.Child("from", "name"), "Docker image references require a name"))
297297
} else {
298298
if ref, err := api.ParseDockerImageReference(from.Name); err != nil {
299299
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, msg))
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("uid"), identity.User.UID, "UID is required when username 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"), "username is required when UID is provided"))
162162
}
163163
return allErrs
164164
}

0 commit comments

Comments
 (0)