forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add inner validation support with one level nesting for field v…
…alidators to validation-gen
- Loading branch information
1 parent
f6f3a16
commit 2e4d2d2
Showing
6 changed files
with
439 additions
and
20 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/inner/metadataname/doc.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// +k8s:validation-gen=* | ||
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme | ||
|
||
// Package inner contains test types for testing inner field validation tags. | ||
package inner | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/code-generator/cmd/validation-gen/testscheme" | ||
) | ||
|
||
var localSchemeBuilder = testscheme.New() | ||
|
||
type T1 struct { | ||
// +k8s:inner(Name)=+k8s:validateFalse="inner T1.ObjectMeta.Name" | ||
metav1.ObjectMeta | ||
} |
64 changes: 64 additions & 0 deletions
64
...-generator/cmd/validation-gen/output_tests/inner/metadataname/zz_generated.validations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/inner/validatefalse/doc.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// +k8s:validation-gen=TypeMeta | ||
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme | ||
|
||
// Package inner contains test types for testing inner field validation tags. | ||
package inner | ||
|
||
import "k8s.io/code-generator/cmd/validation-gen/testscheme" | ||
|
||
var localSchemeBuilder = testscheme.New() | ||
|
||
// M1 represents the formerly anonymous struct used in T1.Simple. | ||
type M1 struct { | ||
StringField string `json:"stringField"` | ||
SliceField []string `json:"sliceField"` | ||
PointerField *string `json:"pointerField"` | ||
MapField map[string]string `json:"mapField"` | ||
} | ||
|
||
// T1 demonstrates validations for inner fields of structs. | ||
type T1 struct { | ||
TypeMeta int `json:"typeMeta"` | ||
|
||
// M1 struct with inner field validations | ||
// +k8s:inner(StringField)=+k8s:validateFalse="inner T1.M1.StringField" | ||
// +k8s:inner(SliceField)=+k8s:validateFalse="inner T1.M1.SliceField" | ||
// +k8s:inner(PointerField)=+k8s:validateFalse="inner T1.M1.PointerField" | ||
// +k8s:inner(MapField)=+k8s:validateFalse="inner T1.M1.MapField" | ||
M1 M1 `json:"simple"` | ||
} |
73 changes: 73 additions & 0 deletions
73
...src/k8s.io/code-generator/cmd/validation-gen/output_tests/inner/validatefalse/doc_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package inner | ||
|
||
import ( | ||
"testing" | ||
|
||
operation "k8s.io/apimachinery/pkg/api/operation" | ||
) | ||
|
||
func TestInnerValidationWithValidateFalse(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
obj *T1 | ||
expectedPaths []string | ||
expectErrors bool | ||
}{ | ||
{ | ||
name: "simple inner validation", | ||
obj: &T1{ | ||
M1: M1{ | ||
StringField: "", | ||
SliceField: []string{}, | ||
PointerField: nil, | ||
MapField: map[string]string{}, | ||
}, | ||
}, | ||
expectedPaths: []string{"simple.stringField", "simple.sliceField", "simple.pointerField", "simple.mapField"}, | ||
expectErrors: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
opCtx := operation.Context{} | ||
errs := Validate_T1(opCtx, tc.obj, tc.obj, nil) | ||
if tc.expectErrors && len(errs) == 0 { | ||
t.Error("expected validation errors but got none") | ||
} | ||
if !tc.expectErrors && len(errs) > 0 { | ||
t.Errorf("unexpected validation errors: %v", errs) | ||
} | ||
expectedPathCount := 0 | ||
actualPaths := []string{} | ||
for _, err := range errs { | ||
actualPaths = append(actualPaths, err.Field) | ||
for _, expectedPath := range tc.expectedPaths { | ||
if err.Field == expectedPath { | ||
expectedPathCount++ | ||
} | ||
} | ||
|
||
} | ||
if tc.expectErrors && expectedPathCount != len(tc.expectedPaths) { | ||
t.Errorf("expected error paths %q in %q", tc.expectedPaths, actualPaths) | ||
} | ||
}) | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...generator/cmd/validation-gen/output_tests/inner/validatefalse/zz_generated.validations.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.