Skip to content

Commit 1177529

Browse files
authored
Merge pull request kubernetes#130706 from jpbetz/handle-optional-value-types-with-defaults
Declarative validation: Add default + optional handling
2 parents 3782b55 + 92aeb63 commit 1177529

File tree

9 files changed

+582
-7
lines changed

9 files changed

+582
-7
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +k8s:validation-gen=TypeMeta
18+
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme
19+
20+
// This is a test package.
21+
package nonzerodefaults
22+
23+
import "k8s.io/code-generator/cmd/validation-gen/testscheme"
24+
25+
var localSchemeBuilder = testscheme.New()
26+
27+
type Struct struct {
28+
TypeMeta int
29+
30+
// +k8s:optional
31+
// +default="foobar"
32+
StringField string `json:"stringField"`
33+
34+
// +k8s:optional
35+
// +default="foobar"
36+
StringPtrField *string `json:"stringPtrField"`
37+
38+
// +k8s:optional
39+
// +default=123
40+
IntField int `json:"intField"`
41+
42+
// +k8s:optional
43+
// +default=123
44+
IntPtrField *int `json:"intPtrField"`
45+
46+
// +k8s:optional
47+
// +default=true
48+
BoolField bool `json:"boolField"`
49+
50+
// +k8s:optional
51+
// +default=true
52+
BoolPtrField *bool `json:"boolPtrField"`
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package nonzerodefaults
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/utils/ptr"
23+
)
24+
25+
func Test(t *testing.T) {
26+
st := localSchemeBuilder.Test(t)
27+
28+
st.Value(&Struct{
29+
// All zero-values.
30+
}).ExpectRegexpsByPath(map[string][]string{
31+
"stringField": {"Required value"},
32+
"stringPtrField": {"Required value"},
33+
"intField": {"Required value"},
34+
"intPtrField": {"Required value"},
35+
"boolField": {"Required value"},
36+
"boolPtrField": {"Required value"},
37+
})
38+
39+
st.Value(&Struct{
40+
StringField: "abc",
41+
StringPtrField: ptr.To(""),
42+
IntField: 123,
43+
IntPtrField: ptr.To(0),
44+
BoolField: true,
45+
BoolPtrField: ptr.To(false),
46+
}).ExpectValid()
47+
}

staging/src/k8s.io/code-generator/cmd/validation-gen/output_tests/tags/optional/nonzero_defaults/zz_generated.validations.go

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// +k8s:validation-gen=TypeMeta
18+
// +k8s:validation-gen-scheme-registry=k8s.io/code-generator/cmd/validation-gen/testscheme.Scheme
19+
20+
// This is a test package.
21+
package zerodefaults
22+
23+
import "k8s.io/code-generator/cmd/validation-gen/testscheme"
24+
25+
var localSchemeBuilder = testscheme.New()
26+
27+
type Struct struct {
28+
TypeMeta int
29+
30+
// +k8s:optional
31+
// +default=""
32+
StringField string `json:"stringField"`
33+
34+
// +k8s:optional
35+
// +default=""
36+
StringPtrField *string `json:"stringPtrField"`
37+
38+
// +k8s:optional
39+
// +default=0
40+
IntField int `json:"intField"`
41+
42+
// +k8s:optional
43+
// +default=0
44+
IntPtrField *int `json:"intPtrField"`
45+
46+
// +k8s:optional
47+
// +default=false
48+
BoolField bool `json:"boolField"`
49+
50+
// +k8s:optional
51+
// +default=false
52+
BoolPtrField *bool `json:"boolPtrField"`
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package zerodefaults
18+
19+
import (
20+
"testing"
21+
22+
"k8s.io/utils/ptr"
23+
)
24+
25+
func Test(t *testing.T) {
26+
st := localSchemeBuilder.Test(t)
27+
28+
st.Value(&Struct{
29+
// All zero-values.
30+
}).ExpectRegexpsByPath(map[string][]string{
31+
// "stringField": optional value fields with zero defaults are just docs
32+
// "intField": optional value fields with zero defaults are just docs
33+
// "boolField": optional value fields with zero defaults are just docs
34+
"stringPtrField": {"Required value"},
35+
"intPtrField": {"Required value"},
36+
"boolPtrField": {"Required value"},
37+
})
38+
39+
st.Value(&Struct{
40+
StringField: "abc",
41+
StringPtrField: ptr.To(""),
42+
IntField: 123,
43+
IntPtrField: ptr.To(0),
44+
BoolField: true,
45+
BoolPtrField: ptr.To(false),
46+
}).ExpectValid()
47+
}

0 commit comments

Comments
 (0)