Skip to content

Commit

Permalink
Merge pull request #40 from yongruilin/validation-gen-plus-yongrlin-m…
Browse files Browse the repository at this point in the history
…axitem

feat: Support `maxItems` validation
  • Loading branch information
yongruilin authored Dec 3, 2024
2 parents 0296bc1 + 0310624 commit c807683
Show file tree
Hide file tree
Showing 14 changed files with 939 additions and 0 deletions.
8 changes: 8 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/validate/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,11 @@ func Optional[T comparable](_ operation.Context, fldPath *field.Path, value, _ *
}
return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
}

// MaxItems verifies that the specified slice is not longer than max items.
func MaxItems[T any](_ operation.Context, fldPath *field.Path, value, _ []T, max int) field.ErrorList {
if len(value) > max {
return field.ErrorList{field.TooMany(fldPath, len(value), max)}
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
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

// This is a test package.
package sliceofprimitive

import "k8s.io/code-generator/cmd/validation-gen/testscheme"

var localSchemeBuilder = testscheme.New()

// Maxitems
type M struct {
TypeMeta int

// slice-of-primitive
// +k8s:validation:maxItems=1
M0 []int `json:"m0"`

// slice-of-pointer-to-primitive
// Validation on field only.
// +k8s:validation:maxItems=1
M1 []*int `json:"m1"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
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 sliceofprimitive

import (
"testing"

"k8s.io/apimachinery/pkg/util/validation/field"
)

func Test(t *testing.T) {
st := localSchemeBuilder.Test(t)

st.Value(&M{M0: []int{0, 0}}).
ExpectInvalid(
field.TooMany(field.NewPath("m0"), 2, 1),
)

st.Value(&M{M0: []int{0}}).
ExpectValid()

st.Value(&M{M1: []*int{new(int), new(int)}}).
ExpectInvalid(
field.TooMany(field.NewPath("m1"), 2, 1),
)

st.Value(&M{M1: []*int{new(int)}}).
ExpectValid()
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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

// This is a test package.
package sliceofslice

import "k8s.io/code-generator/cmd/validation-gen/testscheme"

var localSchemeBuilder = testscheme.New()

// Maxitems
type M struct {
TypeMeta int

// slice-of-slice-of-value
// +k8s:validation:maxItems=1
M0 [][]int `json:"m0"`

// slice-of-typedef-of-slice-of-value
// +k8s:validation:maxItems=1
M1 []IntSlice `json:"m1"`

// slice-of-slice-of-pointer
// +k8s:validation:maxItems=1
M2 [][]*int `json:"m2"`

// slice-of-typedef-of-slice-of-pointer
// Validation on field only.
// +k8s:validation:maxItems=1
M3 []IntPtrSlice `json:"m3"`
}

// Note: no limit here
type IntSlice []int

// Note: no limit here
type IntPtrSlice []*int
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 sliceofslice

import (
"testing"

"k8s.io/apimachinery/pkg/util/validation/field"
)

func Test(t *testing.T) {
st := localSchemeBuilder.Test(t)

st.Value(&M{M0: [][]int{{0, 0}, {0, 0}}}).
ExpectInvalid(
field.TooMany(field.NewPath("m0"), 2, 1),
)

st.Value(&M{M0: [][]int{{0, 0}}}).
ExpectValid()

st.Value(&M{M1: []IntSlice{{0}}}).
ExpectValid()

st.Value(&M{M1: []IntSlice{{}, {}}}).
ExpectInvalid(
field.TooMany(field.NewPath("m1"), 2, 1),
)
st.Value(&M{M2: [][]*int{}}).
ExpectValid()

st.Value(&M{M2: [][]*int{{}, {}}}).
ExpectInvalid(
field.TooMany(field.NewPath("m2"), 2, 1),
)

st.Value(&M{M3: []IntPtrSlice{}}).
ExpectValid()

st.Value(&M{M3: []IntPtrSlice{{}, {}}}).
ExpectInvalid(
field.TooMany(field.NewPath("m3"), 2, 1),
)

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c807683

Please sign in to comment.