github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/validator_z__test.go (about) 1 package validator_test 2 3 import ( 4 "context" 5 "reflect" 6 7 "github.com/machinefi/w3bstream/pkg/depends/kit/validator" 8 "github.com/machinefi/w3bstream/pkg/depends/x/typesx" 9 ) 10 11 var ( 12 rtInt = reflect.TypeOf(int(1)) 13 rtInt8 = reflect.TypeOf(int8(1)) 14 rtInt16 = reflect.TypeOf(int16(1)) 15 rtInt32 = reflect.TypeOf(int32(1)) 16 rtInt64 = reflect.TypeOf(int64(1)) 17 rtUint = reflect.TypeOf(uint(1)) 18 rtUint8 = reflect.TypeOf(uint8(1)) 19 rtUint16 = reflect.TypeOf(uint16(1)) 20 rtUint32 = reflect.TypeOf(uint32(1)) 21 rtUint64 = reflect.TypeOf(uint64(1)) 22 rtFloat32 = reflect.TypeOf(float32(1)) 23 rtFloat64 = reflect.TypeOf(float64(1)) 24 rtMapStringString = reflect.TypeOf(map[string]string{}) 25 rtMapStringMapStringString = reflect.TypeOf(map[string]map[string]string{}) 26 rtString = reflect.TypeOf("") 27 rtArrayString = reflect.TypeOf([1]string{}) 28 rtSliceString = reflect.TypeOf([]string{}) 29 rtSliceFloat64 = reflect.TypeOf([]float64{}) 30 31 rtSomeStruct = reflect.TypeOf(&SomeStruct{}) 32 rttSomeStruct = typesx.FromReflectType(rtSomeStruct.Elem()) 33 rttSomeStructPtr = typesx.FromReflectType(rtSomeStruct) 34 35 rttInt = typesx.FromReflectType(rtInt) 36 rttInt8 = typesx.FromReflectType(rtInt8) 37 rttInt16 = typesx.FromReflectType(rtInt16) 38 rttInt32 = typesx.FromReflectType(rtInt32) 39 rttInt64 = typesx.FromReflectType(rtInt64) 40 rttFloat32 = typesx.FromReflectType(rtFloat32) 41 rttFloat64 = typesx.FromReflectType(rtFloat64) 42 rttSliceFloat64 = typesx.FromReflectType(rtSliceFloat64) 43 rttMapStringString = typesx.FromReflectType(rtMapStringString) 44 rttMapStringMapStringString = typesx.FromReflectType(rtMapStringMapStringString) 45 rttString = typesx.FromReflectType(rtString) 46 47 bg = context.Background() 48 ctx = validator.ContextWithFactory(bg, validator.DefaultFactory) 49 ) 50 51 type StringType string 52 53 type Named string 54 55 type SubPtrStruct struct { 56 PtrInt *int `validate:"@int[1,]"` 57 PtrFloat *float32 `validate:"@float[1,]"` 58 PtrUint *uint `validate:"@uint[1,]"` 59 } 60 61 type SubStruct struct { 62 Int int `json:"int" validate:"@int[1,]"` 63 Float float32 `json:"float" validate:"@float[1,]"` 64 Uint uint `json:"uint" validate:"@uint[1,]"` 65 } 66 67 type SomeStruct struct { 68 JustRequired string 69 CanEmpty *string `validate:"@string[0,]?"` 70 String string `validate:"@string[1,]"` 71 Named Named `validate:"@string[2,]"` 72 PtrString *string `validate:"@string[3,]" default:"123"` 73 SomeStringer *SomeTextMarshaler `validate:"@string[20,]"` 74 Slice []string `validate:"@slice<@string[1,]>"` 75 SliceStruct []SubStruct `validate:"@slice"` 76 Map map[string]string `validate:"@map<@string[2,],@string[1,]>"` 77 MapStruct map[string]SubStruct `validate:"@map<@string[2,],>"` 78 Struct SubStruct 79 SubStruct 80 *SubPtrStruct 81 } 82 83 type SomeTextMarshaler struct { 84 } 85 86 func (*SomeTextMarshaler) MarshalText() ([]byte, error) { 87 return []byte("SomeTextMarshaler"), nil 88 }