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
c807683
commit 8547537
Showing
8 changed files
with
479 additions
and
22 deletions.
There are no files selected for viewing
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)=+validateFalse="inner T1.M1.StringField" | ||
// +k8s:inner(SliceField)=+validateFalse="inner T1.M1.SliceField" | ||
// +k8s:inner(PointerField)=+validateFalse="inner T1.M1.PointerField" | ||
// +k8s:inner(MapField)=+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 TestInnerValidationWithValidateTrue(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.
35 changes: 35 additions & 0 deletions
35
staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/my-test/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,35 @@ | ||
/* | ||
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 | ||
// +k8s:validation-gen-test-fixture=validateFalse | ||
|
||
// This is a test package. | ||
package keyonly | ||
|
||
import "k8s.io/code-generator/cmd/validation-gen/testscheme" | ||
|
||
var localSchemeBuilder = testscheme.New() | ||
|
||
// +validateFalse="type T1" | ||
type T1 struct { | ||
TypeMeta int | ||
|
||
// +validateFalse="field T1.MSS" | ||
// +eachKey=+validateFalse="T1.MSS[keys]" | ||
MSS map[string]string `json:"mss"` | ||
} |
11 changes: 11 additions & 0 deletions
11
...8s.io/code-generator/cmd/validation-gen/output_tests/my-test/testdata/validate-false.json
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,11 @@ | ||
{ | ||
"*keyonly.T1": { | ||
"": [ | ||
"type T1" | ||
], | ||
"mss": [ | ||
"T1.MSS[keys]", | ||
"field T1.MSS" | ||
] | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...k8s.io/code-generator/cmd/validation-gen/output_tests/my-test/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.
30 changes: 30 additions & 0 deletions
30
...o/code-generator/cmd/validation-gen/output_tests/my-test/zz_generated.validations_test.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.