Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit e6df0b1

Browse files
committed
Convert validation to use FieldPath
Before this change we have a mish-mash of ways to pass field names around for error generation. Sometimes string fieldnames, sometimes .Prefix(), sometimes neither, often wrong names or not indexed when it should be. Instead of that mess, this is part one of a couple of commits that will make it more strongly typed and hopefully encourage correct behavior. At least you will have to think about field names, which is better than nothing. It turned out to be really hard to do this incrementally.
1 parent 102eced commit e6df0b1

File tree

19 files changed

+986
-807
lines changed

19 files changed

+986
-807
lines changed

examples/examples_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func validateObject(obj runtime.Object) (errors utilvalidation.ErrorList) {
123123
}
124124
errors = expvalidation.ValidateDaemonSet(t)
125125
default:
126-
return utilvalidation.ErrorList{utilvalidation.NewInternalError("", fmt.Errorf("no validation defined for %#v", obj))}
126+
return utilvalidation.ErrorList{utilvalidation.NewInternalError(utilvalidation.NewFieldPath(""), fmt.Errorf("no validation defined for %#v", obj))}
127127
}
128128
return errors
129129
}

pkg/api/errors/errors_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func TestNewInvalid(t *testing.T) {
9292
Details *unversioned.StatusDetails
9393
}{
9494
{
95-
validation.NewDuplicateError("field[0].name", "bar"),
95+
validation.NewDuplicateError(validation.NewFieldPath("field[0].name"), "bar"),
9696
&unversioned.StatusDetails{
9797
Kind: "kind",
9898
Name: "name",
@@ -103,7 +103,7 @@ func TestNewInvalid(t *testing.T) {
103103
},
104104
},
105105
{
106-
validation.NewInvalidError("field[0].name", "bar", "detail"),
106+
validation.NewInvalidError(validation.NewFieldPath("field[0].name"), "bar", "detail"),
107107
&unversioned.StatusDetails{
108108
Kind: "kind",
109109
Name: "name",
@@ -114,7 +114,7 @@ func TestNewInvalid(t *testing.T) {
114114
},
115115
},
116116
{
117-
validation.NewNotFoundError("field[0].name", "bar"),
117+
validation.NewNotFoundError(validation.NewFieldPath("field[0].name"), "bar"),
118118
&unversioned.StatusDetails{
119119
Kind: "kind",
120120
Name: "name",
@@ -125,7 +125,7 @@ func TestNewInvalid(t *testing.T) {
125125
},
126126
},
127127
{
128-
validation.NewNotSupportedError("field[0].name", "bar", nil),
128+
validation.NewNotSupportedError(validation.NewFieldPath("field[0].name"), "bar", nil),
129129
&unversioned.StatusDetails{
130130
Kind: "kind",
131131
Name: "name",
@@ -136,7 +136,7 @@ func TestNewInvalid(t *testing.T) {
136136
},
137137
},
138138
{
139-
validation.NewRequiredError("field[0].name"),
139+
validation.NewRequiredError(validation.NewFieldPath("field[0].name")),
140140
&unversioned.StatusDetails{
141141
Kind: "kind",
142142
Name: "name",

pkg/api/rest/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx api.Context, obj runtime.Obje
7777
// Custom validation (including name validation) passed
7878
// Now run common validation on object meta
7979
// Do this *after* custom validation so that specific error messages are shown whenever possible
80-
if errs := validation.ValidateObjectMeta(objectMeta, strategy.NamespaceScoped(), validation.ValidatePathSegmentName); len(errs) > 0 {
80+
if errs := validation.ValidateObjectMeta(objectMeta, strategy.NamespaceScoped(), validation.ValidatePathSegmentName, utilvalidation.NewFieldPath("metadata")); len(errs) > 0 {
8181
return errors.NewInvalid(kind, objectMeta.Name, errs)
8282
}
8383

pkg/api/rest/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func validateCommonFields(obj, old runtime.Object) utilvalidation.ErrorList {
5757
allErrs := utilvalidation.ErrorList{}
5858
objectMeta, err := api.ObjectMetaFor(obj)
5959
if err != nil {
60-
return append(allErrs, utilvalidation.NewInternalError("metadata", err))
60+
return append(allErrs, utilvalidation.NewInternalError(utilvalidation.NewFieldPath("metadata"), err))
6161
}
6262
oldObjectMeta, err := api.ObjectMetaFor(old)
6363
if err != nil {
64-
return append(allErrs, utilvalidation.NewInternalError("metadata", err))
64+
return append(allErrs, utilvalidation.NewInternalError(utilvalidation.NewFieldPath("metadata"), err))
6565
}
66-
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(objectMeta, oldObjectMeta)...)
66+
allErrs = append(allErrs, validation.ValidateObjectMetaUpdate(objectMeta, oldObjectMeta, utilvalidation.NewFieldPath("metadata"))...)
6767

6868
return allErrs
6969
}

pkg/api/v1/backward_compatibility_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func TestCompatibility_v1_PodSecurityContext(t *testing.T) {
218218
}
219219

220220
validator := func(obj runtime.Object) utilvalidation.ErrorList {
221-
return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec))
221+
return validation.ValidatePodSpec(&(obj.(*api.Pod).Spec), utilvalidation.NewFieldPath("spec"))
222222
}
223223

224224
for _, tc := range cases {

pkg/api/validation/events.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ func ValidateEvent(event *api.Event) validation.ErrorList {
2727
// There is no namespace required for node.
2828
if event.InvolvedObject.Kind == "Node" &&
2929
event.Namespace != "" {
30-
allErrs = append(allErrs, validation.NewInvalidError("involvedObject.namespace", event.InvolvedObject.Namespace, "namespace is not required for node"))
30+
allErrs = append(allErrs, validation.NewInvalidError(validation.NewFieldPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "namespace is not required for node"))
3131
}
3232
if event.InvolvedObject.Kind != "Node" &&
3333
event.Namespace != event.InvolvedObject.Namespace {
34-
allErrs = append(allErrs, validation.NewInvalidError("involvedObject.namespace", event.InvolvedObject.Namespace, "namespace does not match involvedObject"))
34+
allErrs = append(allErrs, validation.NewInvalidError(validation.NewFieldPath("involvedObject", "namespace"), event.InvolvedObject.Namespace, "does not match involvedObject"))
3535
}
3636
if !validation.IsDNS1123Subdomain(event.Namespace) {
37-
allErrs = append(allErrs, validation.NewInvalidError("namespace", event.Namespace, ""))
37+
allErrs = append(allErrs, validation.NewInvalidError(validation.NewFieldPath("namespace"), event.Namespace, ""))
3838
}
3939
return allErrs
4040
}

0 commit comments

Comments
 (0)