Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for common name formats to validation markers #451

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions pkg/generators/markers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,17 @@ func (c commentTags) ValidationSchema() (*spec.Schema, error) {
SchemaProps: c.SchemaProps,
}

if len(c.CEL) > 0 {
if res.AllOf != nil {
res.AllOf = append([]spec.Schema{}, res.AllOf...)
}
Schnides123 marked this conversation as resolved.
Show resolved Hide resolved
ccel := append([]CELTag{}, c.CEL...)
if _, exists := NameFormats[c.Format]; exists {
ccel = append([]CELTag{{Rule: "!format." + c.Format + "().validate(self).hasValue()", MessageExpression: "format." + c.Format + "().validate(self).value()"}}, ccel...)
}

if len(ccel) > 0 {
// Convert the CELTag to a map[string]interface{} via JSON
celTagJSON, err := json.Marshal(c.CEL)
celTagJSON, err := json.Marshal(ccel)
if err != nil {
return nil, fmt.Errorf("failed to marshal CEL tag: %w", err)
}
Expand All @@ -108,6 +116,17 @@ func (c commentTags) ValidationSchema() (*spec.Schema, error) {
return &res, nil
}

var NameFormats = map[string]struct{}{
"dns1123Label": struct{}{},
"dns1123Subdomain": struct{}{},
"httpPath": struct{}{},
"qualifiedName": struct{}{},
"wildcardDNS1123Subdomain": struct{}{},
"cIdentifier": struct{}{},
"dns1035Label": struct{}{},
"labelValue": struct{}{},
}

// validates the parameters in a CommentTags instance. Returns any errors encountered.
func (c commentTags) Validate() error {

Expand Down Expand Up @@ -164,6 +183,13 @@ func (c commentTags) Validate() error {
err = errors.Join(err, fmt.Errorf("invalid CEL tag at index %d: %w", i, celError))
}

if c.Format != "" {
_, ok := NameFormats[c.Format]
Schnides123 marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
err = errors.Join(err, fmt.Errorf("invalid nameFormat: %v", c.Format))
}
}

return err
}

Expand Down Expand Up @@ -226,6 +252,9 @@ func (c commentTags) ValidateType(t *types.Type) error {
if c.ExclusiveMaximum && !isInt && !isFloat {
err = errors.Join(err, fmt.Errorf("exclusiveMaximum can only be used on numeric types"))
}
if c.Format != "" && !isString {
err = errors.Join(err, fmt.Errorf("Format can only be used on string types"))
}

return err
}
Expand Down
70 changes: 70 additions & 0 deletions pkg/generators/markers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package generators_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -512,6 +513,75 @@ func TestParseCommentTags(t *testing.T) {
}
}

func TestFormat(t *testing.T) {

formatNames := []string{
Schnides123 marked this conversation as resolved.
Show resolved Hide resolved
"dns1123Label",
"dns1123Subdomain",
"httpPath",
"qualifiedName",
"wildcardDNS1123Subdomain",
"cIdentifier",
"dns1035Label",
"labelValue",
}

cases := []struct {
t *types.Type
name string
comments []string
expected *spec.Schema
expectedError string
}{}

for _, formatName := range formatNames {

cases = append(cases, struct {
t *types.Type
name string
comments []string
expected *spec.Schema
expectedError string
Schnides123 marked this conversation as resolved.
Show resolved Hide resolved
}{
t: types.String,
name: formatName,
comments: []string{
fmt.Sprintf("+k8s:validation:format=\"%s\"", formatName),
},
expected: &spec.Schema{
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{
map[string]interface{}{
"messageExpression": "format." + formatName + "().validate(self).value()",
"rule": "!format." + formatName + "().validate(self).hasValue()",
},
},
},
},
SchemaProps: spec.SchemaProps{
Format: formatName,
},
},
})
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
actual, err := generators.ParseCommentTags(tc.t, tc.comments, "+k8s:validation:")
if tc.expectedError != "" {
require.Error(t, err)
require.Regexp(t, tc.expectedError, err.Error())
return
} else {
require.NoError(t, err)
}

require.Equal(t, tc.expected, actual)
})
}
}

// Test comment tag validation function
func TestCommentTags_Validate(t *testing.T) {

Expand Down
6 changes: 1 addition & 5 deletions pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,7 @@ func (g openAPITypeWriter) generateValueValidations(vs *spec.SchemaProps) error
g.Do("MaxProperties: $.ptrTo|raw$[int64]($.spec.MaxProperties$),\n", args)
}
if len(vs.Pattern) > 0 {
p, err := json.Marshal(vs.Pattern)
if err != nil {
return err
}
g.Do("Pattern: $.$,\n", string(p))
g.Do("Pattern: $.$,\n", fmt.Sprintf("%#v", vs.Pattern))
}
if vs.MultipleOf != nil {
g.Do("MultipleOf: $.ptrTo|raw$[float64]($.spec.MultipleOf$),\n", args)
Expand Down
173 changes: 172 additions & 1 deletion pkg/generators/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,7 @@ func TestMarkerComments(t *testing.T) {
Default: "",
MinLength: ptr.To[int64](1),
MaxLength: ptr.To[int64](10),
Pattern: "^foo$[0-9]+",
Pattern: ` + fmt.Sprintf("%#v", "^foo$[0-9]+") + `,
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -2813,6 +2813,177 @@ func TestRequired(t *testing.T) {
})
}

func TestFormatMarkerComments(t *testing.T) {

inputFile := `
package foo

// +k8s:openapi-gen=true
type Blah struct {
// +k8s:validation:format="dns1123Label"
dns string
// +k8s:validation:format="dns1123Subdomain"
subdomain string
// +k8s:validation:format="httpPath"
path string
// +k8s:validation:format="qualifiedName"
qualified string
// +k8s:validation:format="wildcardDNS1123Subdomain"
wildcard string
// +k8s:validation:format="cIdentifier"
identifier string
// +k8s:validation:format="dns1035Label"
label string
// +k8s:validation:format="labelValue"
value string
}
`
packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{{
Name: "example.com/base/foo",
Files: map[string]interface{}{
"foo.go": inputFile,
},
}})
defer e.Cleanup()

callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
if funcErr != nil {
t.Fatalf("Unexpected funcErr: %v", funcErr)
}
if callErr != nil {
t.Fatalf("Unexpected callErr: %v", callErr)
}
expImports := []string{
`foo "example.com/base/foo"`,
`common "k8s.io/kube-openapi/pkg/common"`,
`spec "k8s.io/kube-openapi/pkg/validation/spec"`,
}
if !cmp.Equal(imports, expImports) {
t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
}

if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes()))
} else {
formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
return common.OpenAPIDefinition{
Schema: spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: map[string]spec.Schema{
"dns": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.dns1123Label().validate(self).value()", "rule": "!format.dns1123Label().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"subdomain": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.dns1123Subdomain().validate(self).value()", "rule": "!format.dns1123Subdomain().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"path": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.httpPath().validate(self).value()", "rule": "!format.httpPath().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"qualified": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.qualifiedName().validate(self).value()", "rule": "!format.qualifiedName().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"wildcard": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.wildcardDNS1123Subdomain().validate(self).value()", "rule": "!format.wildcardDNS1123Subdomain().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"identifier": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.cIdentifier().validate(self).value()", "rule": "!format.cIdentifier().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"label": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.dns1035Label().validate(self).value()", "rule": "!format.dns1035Label().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
"value": {
VendorExtensible: spec.VendorExtensible{
Extensions: spec.Extensions{
"x-kubernetes-validations": []interface{}{map[string]interface{}{"messageExpression": "format.labelValue().validate(self).value()", "rule": "!format.labelValue().validate(self).hasValue()"}},
},
},
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
},
},
Required: []string{"dns", "subdomain", "path", "qualified", "wildcard", "identifier", "label", "value"},
},
},
}
}

`))
if ree != nil {
t.Fatal(ree)
}
assertEqual(t, string(formatted_expected), string(formatted))
}
})
}

func TestMarkerCommentsCustomDefsV3(t *testing.T) {
inputFile := `
package foo
Expand Down
Loading