Skip to content

Commit

Permalink
feat: Support maxItems validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yongruilin committed Dec 2, 2024
1 parent 606a9a0 commit 9b0f6b9
Show file tree
Hide file tree
Showing 8 changed files with 790 additions and 1 deletion.
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,206 @@
/*
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 maxitems

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"`

// typedef-of-slice-of-primitive
// Validation on type only.
M1 IntSliceLimited `json:"m1"`
// Validation on field only.
// +k8s:validation:maxItems=1
M2 IntSlice `json:"m2"`
// Validation on both type and field.
// +k8s:validation:maxItems=2
M3 IntSliceLimited `json:"m3"`

// pointers to slice-of-primitive
// Validation on field only.

// +k8s:validation:maxItems=1
M4 *[]int `json:"m4"`

// pointers to typedef-of-slice-of-primitive
// Validation on type only
M5 *IntSliceLimited `json:"m5"`
// Validation on field only.
// +k8s:validation:maxItems=1
M6 *IntSlice `json:"m6"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M7 *IntSliceLimited `json:"m7"`

// slice-of-pointer-to-primitive
// Validation on field only.
// +k8s:validation:maxItems=1
M8 []*int `json:"m8"`

// typedef-of-slice-of-pointer-to-primitive
// Validation on type only.
M9 IntPtrSliceLimited `json:"m9"`
// Validation on field only.
// +k8s:validation:maxItems=1
M10 IntPtrSlice `json:"m10"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M11 IntPtrSliceLimited `json:"m11"`

// pointers to slice-of-pointer-to-primitive
// Validation on field only.
// +k8s:validation:maxItems=1
M12 *[]*int `json:"m12"`

// pointers to typedef-of-slice-of-pointer-to-primitive
// Validation on type only.
M13 *IntPtrSliceLimited `json:"m13"`
// Validation on field only.
// +k8s:validation:maxItems=1
M14 *IntPtrSlice `json:"m14"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M15 *IntPtrSliceLimited `json:"m15"`

// slice-of-struct
// +k8s:validation:maxItems=1
M16 []S `json:"m16"`

// typedef-of-slice-of-struct
// Validation on type only.
M17 SSliceLimited `json:"m17"`
// Validation on field only.
// +k8s:validation:maxItems=1
M18 SSlice `json:"m18"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M19 SSliceLimited `json:"m19"`

// pointers to slice-of-struct
// Validation on type only.
M20 *[]SSliceLimited `json:"m20"`
// Validation on field only.
// +k8s:validation:maxItems=1
M21 *[]SSlice `json:"m21"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M22 *[]SSliceLimited `json:"m22"`

// pointers to typedef-of-slice-of-struct
// Validation on type only.
M23 *SSliceLimited `json:"m23"`
// Validation on field only.
// +k8s:validation:maxItems=1
M24 *SSlice `json:"m24"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M25 *SSliceLimited `json:"m25"`

// slice-of-pointer-to-struct
// +k8s:validation:maxItems=1
M26 []*S `json:"m26"`

// typedef-of-slice-of-pointer-to-struct
// Validation on type only.
M27 SPtrSliceLimited `json:"m27"`
// Validation on field only.
// +k8s:validation:maxItems=1
M28 SPtrSlice `json:"m28"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M29 SPtrSliceLimited `json:"m29"`

// pointers to slice-of-pointer-to-struct
// +k8s:validation:maxItems=1
M30 *[]*S `json:"m30"`

// pointers to typedef-of-slice-of-pointer-to-struct
// Validation on type only.
M31 *SPtrSliceLimited `json:"m31"`
// Validation on field only.
// +k8s:validation:maxItems=1
M32 *SPtrSlice `json:"m32"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M33 *SPtrSliceLimited `json:"m33"`

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

// slice-of-typedef-of-slice-of-value
// Validation on type only.
M36 []IntSliceLimited `json:"m36"`
// Validation on field only.
// +k8s:validation:maxItems=1
M35 []IntSlice `json:"m35"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M37 []IntSliceLimited `json:"m37"`

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

// slice-of-typedef-of-slice-of-pointer
// Validation on field only.
// +k8s:validation:maxItems=1
M39 []IntPtrSlice `json:"m39"`
// Validation on type only.
M40 []IntPtrSliceLimited `json:"m40"`
// Validation on both type and field.
// +k8s:validation:maxItems=1
M41 []IntPtrSliceLimited `json:"m41"`
}

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

// +k8s:validation:maxItems=2
type IntSliceLimited []int

// Note: no limit here
type IntPtrSlice []*int

// +k8s:validation:maxItems=2
type IntPtrSliceLimited []*int

type S struct{}

// Note: no limit here
type SSlice []S

// +k8s:validation:maxItems=2
type SSliceLimited []S

// +k8s:validation:maxItems=2
type SPtrSliceLimited []*S

// Note: no limit here
type SPtrSlice []*S
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
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 maxitems

import (
"testing"
// "k8s.io/apimachinery/pkg/util/validation/field"
)

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

// Testing M0
st.Value(&M{M0: []int{1}, M4: &[]int{1}}).ExpectValid()
}
Loading

0 comments on commit 9b0f6b9

Please sign in to comment.