Skip to content

Commit

Permalink
feat: add inner validation support with one level nesting for field v…
Browse files Browse the repository at this point in the history
…alidators to validation-gen
  • Loading branch information
aaron-prindle committed Dec 13, 2024
1 parent f6f3a16 commit 2e4d2d2
Show file tree
Hide file tree
Showing 6 changed files with 439 additions and 20 deletions.
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.
*/

// +k8s:validation-gen=*
// +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 (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/code-generator/cmd/validation-gen/testscheme"
)

var localSchemeBuilder = testscheme.New()

type T1 struct {
// +k8s:inner(Name)=+k8s:validateFalse="inner T1.ObjectMeta.Name"
metav1.ObjectMeta
}

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,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)=+k8s:validateFalse="inner T1.M1.StringField"
// +k8s:inner(SliceField)=+k8s:validateFalse="inner T1.M1.SliceField"
// +k8s:inner(PointerField)=+k8s:validateFalse="inner T1.M1.PointerField"
// +k8s:inner(MapField)=+k8s:validateFalse="inner T1.M1.MapField"
M1 M1 `json:"simple"`
}
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 TestInnerValidationWithValidateFalse(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)
}
})
}
}

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

Loading

0 comments on commit 2e4d2d2

Please sign in to comment.