...

Source file src/github.com/go-openapi/validate/type_test.go

Documentation: github.com/go-openapi/validate

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  import (
    18  	"io"
    19  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/go-openapi/strfmt"
    24  	"github.com/go-openapi/swag"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  type expectedJSONType struct {
    30  	value                 interface{}
    31  	expectedJSONType      string
    32  	expectedSwaggerFormat string
    33  }
    34  
    35  func TestType_schemaInfoForType(t *testing.T) {
    36  	testTypes := []expectedJSONType{
    37  		{
    38  			value:                 []byte("abc"),
    39  			expectedJSONType:      stringType,
    40  			expectedSwaggerFormat: "byte",
    41  		},
    42  		{
    43  			value:                 strfmt.Date(time.Date(2014, 10, 10, 0, 0, 0, 0, time.UTC)),
    44  			expectedJSONType:      stringType,
    45  			expectedSwaggerFormat: "date",
    46  		},
    47  		{
    48  			value:                 strfmt.NewDateTime(),
    49  			expectedJSONType:      stringType,
    50  			expectedSwaggerFormat: "date-time",
    51  		},
    52  		{
    53  			// TODO: this exception is really prone to errors: should alias runtime.File in strfmt
    54  			value:                 swag.File{},
    55  			expectedJSONType:      "file",
    56  			expectedSwaggerFormat: "",
    57  		},
    58  		{
    59  			value:                 strfmt.URI("http://thisisleadingusnowhere.com"),
    60  			expectedJSONType:      stringType,
    61  			expectedSwaggerFormat: "uri",
    62  		},
    63  		{
    64  			value:                 strfmt.Email("fred@esasymoney.com"),
    65  			expectedJSONType:      stringType,
    66  			expectedSwaggerFormat: "email",
    67  		},
    68  		{
    69  			value:                 strfmt.Hostname("www.github.com"),
    70  			expectedJSONType:      stringType,
    71  			expectedSwaggerFormat: "hostname",
    72  		},
    73  		{
    74  			value:                 strfmt.Password("secret"),
    75  			expectedJSONType:      stringType,
    76  			expectedSwaggerFormat: "password",
    77  		},
    78  		{
    79  			value:                 strfmt.IPv4("192.168.224.1"),
    80  			expectedJSONType:      stringType,
    81  			expectedSwaggerFormat: "ipv4",
    82  		},
    83  		{
    84  			value:                 strfmt.IPv6("::1"),
    85  			expectedJSONType:      stringType,
    86  			expectedSwaggerFormat: "ipv6",
    87  		},
    88  		{
    89  			value:                 strfmt.MAC("01:02:03:04:05:06"),
    90  			expectedJSONType:      stringType,
    91  			expectedSwaggerFormat: "mac",
    92  		},
    93  		{
    94  			value:                 strfmt.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e"),
    95  			expectedJSONType:      stringType,
    96  			expectedSwaggerFormat: "uuid",
    97  		},
    98  		{
    99  			value:                 strfmt.UUID3("bcd02e22-68f0-3046-a512-327cca9def8f"),
   100  			expectedJSONType:      stringType,
   101  			expectedSwaggerFormat: "uuid3",
   102  		},
   103  		{
   104  			value:                 strfmt.UUID4("025b0d74-00a2-4048-bf57-227c5111bb34"),
   105  			expectedJSONType:      stringType,
   106  			expectedSwaggerFormat: "uuid4",
   107  		},
   108  		{
   109  			value:                 strfmt.UUID5("886313e1-3b8a-5372-9b90-0c9aee199e5d"),
   110  			expectedJSONType:      stringType,
   111  			expectedSwaggerFormat: "uuid5",
   112  		},
   113  		{
   114  			value:                 strfmt.ISBN("0321751043"),
   115  			expectedJSONType:      stringType,
   116  			expectedSwaggerFormat: "isbn",
   117  		},
   118  		{
   119  			value:                 strfmt.ISBN10("0321751043"),
   120  			expectedJSONType:      stringType,
   121  			expectedSwaggerFormat: "isbn10",
   122  		},
   123  		{
   124  			value:                 strfmt.ISBN13("978-0321751041"),
   125  			expectedJSONType:      stringType,
   126  			expectedSwaggerFormat: "isbn13",
   127  		},
   128  		{
   129  			value:                 strfmt.CreditCard("4111-1111-1111-1111"),
   130  			expectedJSONType:      stringType,
   131  			expectedSwaggerFormat: "creditcard",
   132  		},
   133  		{
   134  			value:                 strfmt.SSN("111-11-1111"),
   135  			expectedJSONType:      stringType,
   136  			expectedSwaggerFormat: "ssn",
   137  		},
   138  		{
   139  			value:                 strfmt.HexColor("#FFFFFF"),
   140  			expectedJSONType:      stringType,
   141  			expectedSwaggerFormat: "hexcolor",
   142  		},
   143  		{
   144  			value:                 strfmt.RGBColor("rgb(255,255,255)"),
   145  			expectedJSONType:      stringType,
   146  			expectedSwaggerFormat: "rgbcolor",
   147  		},
   148  		// Numerical values
   149  		{
   150  			value:                 true,
   151  			expectedJSONType:      "boolean",
   152  			expectedSwaggerFormat: "",
   153  		},
   154  		{
   155  			value:                 int8(12),
   156  			expectedJSONType:      "integer",
   157  			expectedSwaggerFormat: "int32",
   158  		},
   159  		{
   160  			value:                 uint8(12),
   161  			expectedJSONType:      "integer",
   162  			expectedSwaggerFormat: "int32",
   163  		},
   164  		{
   165  			value:                 int16(12),
   166  			expectedJSONType:      "integer",
   167  			expectedSwaggerFormat: "int32",
   168  		},
   169  		{
   170  			value:            uint16(12),
   171  			expectedJSONType: "integer",
   172  			// TODO: should be uint32
   173  			expectedSwaggerFormat: "int32",
   174  		},
   175  		{
   176  			value:                 int32(12),
   177  			expectedJSONType:      "integer",
   178  			expectedSwaggerFormat: "int32",
   179  		},
   180  		{
   181  			value:            uint32(12),
   182  			expectedJSONType: "integer",
   183  			// TODO: should be uint32
   184  			expectedSwaggerFormat: "int32",
   185  		},
   186  		{
   187  			value:                 int(12),
   188  			expectedJSONType:      "integer",
   189  			expectedSwaggerFormat: "int64",
   190  		},
   191  		{
   192  			value:            uint(12),
   193  			expectedJSONType: "integer",
   194  			// TODO: should be uint64
   195  			expectedSwaggerFormat: "int64",
   196  		},
   197  		{
   198  			value:                 int64(12),
   199  			expectedJSONType:      "integer",
   200  			expectedSwaggerFormat: "int64",
   201  		},
   202  		{
   203  			value:            uint64(12),
   204  			expectedJSONType: "integer",
   205  			// TODO: should be uint64
   206  			expectedSwaggerFormat: "int64",
   207  		},
   208  		{
   209  			value:            float32(12),
   210  			expectedJSONType: "number",
   211  			// TODO: should be float
   212  			expectedSwaggerFormat: "float32",
   213  		},
   214  		{
   215  			value:            float64(12),
   216  			expectedJSONType: "number",
   217  			// TODO: should be double
   218  			expectedSwaggerFormat: "float64",
   219  		},
   220  		{
   221  			value:                 []string{},
   222  			expectedJSONType:      "array",
   223  			expectedSwaggerFormat: "",
   224  		},
   225  		{
   226  			value:                 expectedJSONType{},
   227  			expectedJSONType:      "object",
   228  			expectedSwaggerFormat: "",
   229  		},
   230  		{
   231  			value:                 map[string]bool{"key": false},
   232  			expectedJSONType:      "object",
   233  			expectedSwaggerFormat: "",
   234  		},
   235  		{
   236  			value:                 "simply a string",
   237  			expectedJSONType:      stringType,
   238  			expectedSwaggerFormat: "",
   239  		},
   240  		{
   241  			// NOTE: Go array returns no JSON type
   242  			value:                 [4]int{1, 2, 4, 4},
   243  			expectedJSONType:      "",
   244  			expectedSwaggerFormat: "",
   245  		},
   246  		{
   247  			value:                 strfmt.Base64("ZWxpemFiZXRocG9zZXk="),
   248  			expectedJSONType:      stringType,
   249  			expectedSwaggerFormat: "byte",
   250  		},
   251  		{
   252  			value:                 strfmt.Duration(0),
   253  			expectedJSONType:      stringType,
   254  			expectedSwaggerFormat: "duration",
   255  		},
   256  		{
   257  			value:                 strfmt.NewObjectId("507f1f77bcf86cd799439011"),
   258  			expectedJSONType:      stringType,
   259  			expectedSwaggerFormat: "bsonobjectid",
   260  		},
   261  		/*
   262  			Test case for : case reflect.Interface:
   263  				// What to do here?
   264  				panic("dunno what to do here")
   265  		*/
   266  	}
   267  
   268  	v := newTypeValidator(
   269  		"", "", nil, false, "", nil,
   270  	)
   271  
   272  	t.Run("should not apply", func(t *testing.T) {
   273  		require.False(t, v.Applies("x", reflect.Map))
   274  	})
   275  
   276  	for _, x := range testTypes {
   277  		value := x.value
   278  
   279  		jsonType, swaggerFormat := v.schemaInfoForType(value)
   280  		assert.Equal(t, x.expectedJSONType, jsonType)
   281  		assert.Equal(t, x.expectedSwaggerFormat, swaggerFormat)
   282  
   283  		jsonType, swaggerFormat = v.schemaInfoForType(&value)
   284  		assert.Equal(t, x.expectedJSONType, jsonType)
   285  		assert.Equal(t, x.expectedSwaggerFormat, swaggerFormat)
   286  	}
   287  
   288  	// Check file declarations as io.ReadCloser are properly detected
   289  	myFile := swag.File{}
   290  	var myReader io.ReadCloser = &myFile
   291  	jsonType, swaggerFormat := v.schemaInfoForType(myReader)
   292  	assert.Equal(t, "file", jsonType)
   293  	assert.Equal(t, "", swaggerFormat)
   294  }
   295  

View as plain text