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 Nov 3, 2024
1 parent a2e6b49 commit 385b250
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
10 changes: 10 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,13 @@ func Optional[T comparable](_ operation.Context, fldPath *field.Path, value, _ *
}
return field.ErrorList{field.Required(fldPath, "optional value was not specified")}
}

func MaxItems[T any](_ operation.Context, fldPath *field.Path, value, _ []T, max int) field.ErrorList {
if value == nil {
return nil
}
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,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

// 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

// +k8s:validation:maxItems=3
M1 []T `json:"m1"`
}

type T struct{}
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.
*/

package maxitems

import (
"testing"

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

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

st.Value(&M{M1: []T{{}, {}}}).ExpectValid()

st.Value(&M{M1: []T{{}, {}, {}, {}}}).ExpectInvalid(
field.TooMany(field.NewPath("m1"), 4, 3),
)
}

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
Expand Up @@ -35,12 +35,14 @@ const (
markerPrefix = "k8s:validation:"
formatTagName = markerPrefix + ":format"
maxLengthTagName = markerPrefix + ":maxLength"
maxItemsTagName = markerPrefix + ":maxItems"
)

var (
ipValidator = types.Name{Package: libValidationPkg, Name: "IP"}
dnsLabelValidator = types.Name{Package: libValidationPkg, Name: "DNSLabel"}
maxLengthValidator = types.Name{Package: libValidationPkg, Name: "MaxLength"}
maxItemsValidator = types.Name{Package: libValidationPkg, Name: "MaxItems"}
)

func (openAPIDeclarativeValidator) ExtractValidations(t *types.Type, comments []string) (Validations, error) {
Expand All @@ -53,6 +55,9 @@ func (openAPIDeclarativeValidator) ExtractValidations(t *types.Type, comments []
if schema.MaxLength != nil {
result.AddFunction(Function(maxLengthTagName, DefaultFlags, maxLengthValidator, *schema.MaxLength))
}
if schema.MaxItems != nil {
result.AddFunction(Function(maxItemsTagName, DefaultFlags, maxItemsValidator, *schema.MaxItems))
}
if len(schema.Format) > 0 {
formatFunction := FormatValidationFunction(schema.Format)
if formatFunction != nil {
Expand Down

0 comments on commit 385b250

Please sign in to comment.