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 12, 2024
1 parent c807683 commit 8547537
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 22 deletions.
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)=+validateFalse="inner T1.M1.StringField"
// +k8s:inner(SliceField)=+validateFalse="inner T1.M1.SliceField"
// +k8s:inner(PointerField)=+validateFalse="inner T1.M1.PointerField"
// +k8s:inner(MapField)=+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 TestInnerValidationWithValidateTrue(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.

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
// +k8s:validation-gen-test-fixture=validateFalse

// This is a test package.
package keyonly

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

var localSchemeBuilder = testscheme.New()

// +validateFalse="type T1"
type T1 struct {
TypeMeta int

// +validateFalse="field T1.MSS"
// +eachKey=+validateFalse="T1.MSS[keys]"
MSS map[string]string `json:"mss"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"*keyonly.T1": {
"": [
"type T1"
],
"mss": [
"T1.MSS[keys]",
"field T1.MSS"
]
}
}

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

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

Loading

0 comments on commit 8547537

Please sign in to comment.