github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/validate/validate_test.go (about) 1 package validate 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/artisanhe/tools/validate/validatetpl" 9 ) 10 11 func TestValidateItemWithPredefineTag(t *testing.T) { 12 char := string("中文中文") 13 if ok, str := ValidateItem("@char[0,2]", char); ok { 14 t.Log(str) 15 t.Error("should be invalid") 16 } else { 17 t.Log(str) 18 } 19 value_uint8 := uint8(0) 20 if ok, str := ValidateItem("@uint8[1,]", value_uint8); ok { 21 t.Error("should be invalid") 22 } else { 23 t.Log(str) 24 } 25 value_int8 := int8(-100) 26 if ok, str := ValidateItem("@int8[-99,-10]", value_int8); ok { 27 t.Error("should be invalid") 28 } else { 29 t.Log(str) 30 } 31 32 value_uint16 := uint16(0) 33 if ok, str := ValidateItem("@uint16[1,]", value_uint16); ok { 34 t.Error("should be invalid") 35 } else { 36 t.Log(str) 37 } 38 value_int16 := int16(-100) 39 if ok, str := ValidateItem("@int16[-99,0]", value_int16); ok { 40 t.Error("should be invalid") 41 } else { 42 t.Log(str) 43 } 44 value_uint32 := uint32(0) 45 if ok, str := ValidateItem("@uint32[1,]", value_uint32); ok { 46 t.Error("should be invalid") 47 } else { 48 t.Log(str) 49 } 50 value_int32 := int32(-100) 51 if ok, str := ValidateItem("@int32[-99,0]", value_int32); ok { 52 t.Error("should be invalid") 53 } else { 54 t.Log(str) 55 } 56 value_uint64 := uint64(0) 57 if ok, str := ValidateItem("@uint64[1,]", value_uint64); ok { 58 t.Error("should be invalid") 59 } else { 60 t.Log(str) 61 } 62 value_int64 := int64(-100) 63 if ok, str := ValidateItem("@int64[-99,1]", value_int64); ok { 64 t.Error("should be invalid") 65 } else { 66 t.Log(str) 67 } 68 value_string := string("hellow word") 69 if ok, str := ValidateItem("@string[0,10]", value_string); ok { 70 t.Error("should be invalid") 71 } else { 72 t.Log(str) 73 } 74 value_unlimit_string := string("unlimit") 75 if ok, str := ValidateItem("@string[0,unlimit]", value_unlimit_string); !ok { 76 t.Error("should be invalid") 77 } else { 78 t.Log(str) 79 } 80 value_not_num_string := string("1234 ") 81 if ok, str := ValidateItem("@numString[0,]", value_not_num_string); ok { 82 t.Error("should be invalid") 83 } else { 84 t.Log(str) 85 } 86 value_mobile := string("28520270839") 87 if ok, str := ValidateItem("@regexp[^((\\+86)|(86))?1\\d{10}$]", value_mobile); ok { 88 t.Error("should be invalid") 89 } else { 90 t.Log(str) 91 } 92 // test [,] 93 value_int8 = int8(1) 94 if ok, str := ValidateItem("@int8[,1]", value_int8); !ok { 95 t.Error("should be valid") 96 } else { 97 t.Log(str) 98 } 99 value_int16 = int16(1) 100 if ok, str := ValidateItem("@int16[,1]", value_int16); !ok { 101 t.Error("should be valid") 102 } else { 103 t.Log(str) 104 } 105 value_int32 = int32(1) 106 if ok, str := ValidateItem("@int32[,1]", value_int32); !ok { 107 t.Error("should be valid") 108 } else { 109 t.Log(str) 110 } 111 value_int64 = int64(1) 112 if ok, str := ValidateItem("@int64[,1]", value_int64); !ok { 113 t.Error("should be valid") 114 } else { 115 t.Log(str) 116 } 117 118 value_uint8 = uint8(1) 119 if ok, str := ValidateItem("@uint8[,1]", value_uint8); !ok { 120 t.Error("should be valid") 121 } else { 122 t.Log(str) 123 } 124 value_uint16 = uint16(1) 125 if ok, str := ValidateItem("@uint16[,1]", value_uint16); !ok { 126 t.Error("should be valid") 127 } else { 128 t.Log(str) 129 } 130 value_uint32 = uint32(1) 131 if ok, str := ValidateItem("@uint32[,1]", value_uint32); !ok { 132 t.Error("should be valid") 133 } else { 134 t.Log(str) 135 } 136 value_uint64 = uint64(0) 137 if ok, str := ValidateItem("@uint64[,1]", value_uint64); !ok { 138 t.Error("should be valid") 139 } else { 140 t.Log(str) 141 } 142 value_float32 := float32(1.1) 143 if ok, str := ValidateItem("@float32[1.1,1.2]", value_float32); !ok { 144 t.Error("should be valid") 145 } else { 146 t.Log(str) 147 } 148 value_float64 := float64(0) 149 if ok, str := ValidateItem("@float64[,1.2]", value_float64); !ok { 150 t.Error("should be invalid") 151 } else { 152 t.Log(str) 153 } 154 155 value_ip := string("1.0.1") 156 if ok, str := ValidateItem("@ipv4", value_ip); ok { 157 t.Error("should be invalid") 158 } else { 159 t.Log(str) 160 } 161 } 162 163 func TestValidateInt8ItemWithPredefineTag(t *testing.T) { 164 // left [ 165 value_int8 := int8(10) 166 if ok, str := ValidateItem("@int8[10,20]", value_int8); !ok { 167 t.Error("should be valid") 168 } else { 169 t.Log(str) 170 } 171 // right ] 172 if ok, str := ValidateItem("@int8[,10]", value_int8); !ok { 173 t.Error("should be valid") 174 } else { 175 t.Log(str) 176 } 177 // left ( 178 if ok, str := ValidateItem("@int8(10,20]", value_int8); ok { 179 t.Error("should be invalid") 180 } else { 181 t.Log(str) 182 } 183 // right ) 184 if ok, str := ValidateItem("@int8[,10)", value_int8); ok { 185 t.Error("should be invalid") 186 } else { 187 t.Log(str) 188 } 189 // enum 190 if ok, str := ValidateItem("@int8{1,2,3}", value_int8); ok { 191 t.Error("should be invalid") 192 } else { 193 t.Log(str) 194 } 195 if ok, str := ValidateItem("@int8{1,2,3,10}", value_int8); !ok { 196 t.Error("should be valid") 197 t.Log(str) 198 } else { 199 t.Log(str) 200 } 201 } 202 203 func TestValidateUint8ItemWithPredefineTag(t *testing.T) { 204 // left [ 205 value_uint8 := uint8(10) 206 if ok, str := ValidateItem("@uint8[10,20]", value_uint8); !ok { 207 t.Error("should be valid") 208 } else { 209 t.Log(str) 210 } 211 // right ] 212 if ok, str := ValidateItem("@uint8[,10]", value_uint8); !ok { 213 t.Error("should be valid") 214 } else { 215 t.Log(str) 216 } 217 // left ( 218 if ok, str := ValidateItem("@uint8(10,20]", value_uint8); ok { 219 t.Error("should be invalid") 220 } else { 221 t.Log(str) 222 } 223 // right ) 224 if ok, str := ValidateItem("@uint8[,10)", value_uint8); ok { 225 t.Error("should be invalid") 226 } else { 227 t.Log(str) 228 } 229 } 230 231 func TestValidateEnumWithPredefineTag(t *testing.T) { 232 value_uint8 := uint8(10) 233 // enum 234 if ok, str := ValidateItem("@uint8{1,2,3}", value_uint8); ok { 235 t.Error("should be invalid") 236 } else { 237 t.Log(str) 238 } 239 if ok, str := ValidateItem("@uint8{1,2,3,10}", value_uint8); !ok { 240 t.Error("should be valid") 241 } else { 242 t.Log(str) 243 } 244 245 var tStr string = "NORMAL" 246 if ok, str := ValidateItem("@string{NORMAL}", tStr); !ok { 247 t.Error("should be valid") 248 t.Log(str) 249 } else { 250 t.Log(str) 251 } 252 // with blank 253 if ok, str := ValidateItem("@string{,NORMAL}", tStr); !ok { 254 t.Error("should be valid") 255 t.Log(str) 256 } else { 257 t.Log(str) 258 } 259 // with _ 260 if ok, str := ValidateItem("@string{NORMAL_A}", tStr); ok { 261 t.Error("should be invalid") 262 t.Log(str) 263 } else { 264 t.Log(str) 265 } 266 } 267 268 func TestValidateSliceWithPredefineTag(t *testing.T) { 269 // normal 270 var slice []int32 = []int32{1, 2, 3} 271 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", slice); !ok { 272 t.Error("should be valid") 273 t.Log(str) 274 } else { 275 t.Log(str) 276 } 277 // elem type invalid 278 var invalidSlice []uint32 = []uint32{1, 2, 3} 279 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", invalidSlice); ok { 280 t.Error("should be invalid") 281 t.Log(str) 282 } else { 283 t.Log(str) 284 } 285 // elem too few 286 slice = []int32{} 287 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", slice); ok { 288 t.Error("should be invalid") 289 t.Log(str) 290 } else { 291 t.Log(str) 292 } 293 // elem too many 294 slice = []int32{1, 2, 2, 3} 295 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", slice); ok { 296 t.Error("should be invalid") 297 t.Log(str) 298 } else { 299 t.Log(str) 300 } 301 // elem too small 302 slice = []int32{0, 2, 3} 303 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", slice); ok { 304 t.Error("should be invalid") 305 t.Log(str) 306 } else { 307 t.Log(str) 308 } 309 // elem too big 310 slice = []int32{1, 2, 4} 311 if ok, str := ValidateItem("@array[1,3]:@int32[1,3]", slice); ok { 312 t.Error("should be invalid") 313 t.Log(str) 314 } else { 315 t.Log(str) 316 } 317 318 AddValidateFunc("@ipv4", validatetpl.ValidateIPv4) 319 strSlice := []string{"1.0"} 320 if ok, str := ValidateItem("@array[0,]:@ipv4", strSlice); ok { 321 t.Error("should be invalid") 322 t.Log(str) 323 } else { 324 t.Log(str) 325 } 326 327 // invalid item validate 328 slice = []int32{1, 2, 4, 4} 329 if ok, str := ValidateItem("@array[1,3]:@invalid", slice); ok { 330 t.Error("should be invalid") 331 t.Log(str) 332 } else { 333 t.Log(str) 334 } 335 336 // invalid item validate 337 slice = []int32{1, 2, 4, 4} 338 if ok, str := ValidateItem("@array[1,4]", slice); !ok { 339 t.Error("should be invalid") 340 t.Log(str) 341 } else { 342 t.Log(str) 343 } 344 345 // invalid item validate 346 slice = []int32{1, 2, 4, 4} 347 if ok, str := ValidateItem("@array[1,4]:@", slice); !ok { 348 t.Error("should be invalid") 349 t.Log(str) 350 } else { 351 t.Log(str) 352 } 353 } 354 355 func TestFloat32Range(t *testing.T) { 356 valueFloat32 := float32(99.9999) 357 ok, str := ValidateItem("@float32[0,99]", valueFloat32) 358 assert.False(t, ok) 359 assert.Equal(t, "浮点值不在[0,99]范围内,当前值:99.9999", str) 360 } 361 362 func TestFloat64Range(t *testing.T) { 363 value_float64 := float64(321.1234567890123456789) 364 ok, str := ValidateItem("@float64[1,1.2]", value_float64) 365 assert.False(t, ok) 366 assert.Equal(t, "浮点值不在[1,1.2]范围内,当前值:321.1234567890123", str) 367 } 368 369 func TestFloat32Decimal(t *testing.T) { 370 value_float32 := float32(0.12345) 371 ok, str := ValidateItem("@float32<7,4>[0,99]", value_float32) 372 assert.False(t, ok) 373 assert.Equal(t, "浮点值小数位必须为4,总位数不能超过7位,当前值:0.12345", str) 374 } 375 376 func TestFloat64Decimal(t *testing.T) { 377 value_float64 := float64(99999.999) 378 ok, str := ValidateItem("@float64<8,4>", value_float64) 379 assert.False(t, ok) 380 assert.Equal(t, "浮点值小数位必须为4,总位数不能超过8位,当前值:99999.999", str) 381 } 382 383 func TestNumStringRange(t *testing.T) { 384 numString := string("123456") 385 ok, str := ValidateItem("@numString[10,20]", numString) 386 assert.False(t, ok) 387 assert.Equal(t, "字符串长度不在[10, 20]范围内,当前长度:6", str) 388 }