k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/validate/formats_test.go (about)

     1  package validate
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"k8s.io/kube-openapi/pkg/validation/spec"
     9  	"k8s.io/kube-openapi/pkg/validation/strfmt"
    10  )
    11  
    12  // valueValidator for string formats
    13  func TestFormatValidator_EdgeCases(t *testing.T) {
    14  	// Apply
    15  	v := formatValidator{
    16  		KnownFormats: strfmt.Default,
    17  	}
    18  
    19  	// formatValidator applies to: Items, Parameter,Schema
    20  
    21  	s := spec.Schema{}
    22  	s.Typed(stringType, "uuid")
    23  
    24  	sources := []interface{}{&s}
    25  
    26  	for _, source := range sources {
    27  		// Default formats for strings
    28  		assert.True(t, v.Applies(source, reflect.String))
    29  		// Do not apply for number formats
    30  		assert.False(t, v.Applies(source, reflect.Int))
    31  	}
    32  
    33  	assert.False(t, v.Applies("A string", reflect.String))
    34  	assert.False(t, v.Applies(nil, reflect.String))
    35  }