github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/validator_z_struct_test.go (about)

     1  package validator_test
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"strconv"
     8  	"testing"
     9  
    10  	. "github.com/onsi/gomega"
    11  
    12  	. "github.com/machinefi/w3bstream/pkg/depends/kit/validator"
    13  	"github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
    14  	"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
    15  )
    16  
    17  func TestStruct_New(t *testing.T) {
    18  	v := NewStructValidator("json")
    19  	_, err := v.New(ctx, &Rule{Type: rttSomeStruct})
    20  
    21  	NewWithT(t).Expect(err).To(BeNil())
    22  }
    23  
    24  func TestStruct_NewFailed(t *testing.T) {
    25  	type Named string
    26  
    27  	type Struct struct {
    28  		Int    int  `validate:"@int[1,"`
    29  		PtrInt *int `validate:"@uint[2,"`
    30  	}
    31  
    32  	type SubStruct struct {
    33  		Float    float32  `validate:"@string"`
    34  		PtrFloat *float32 `validate:"@unknown"`
    35  	}
    36  
    37  	type SomeStruct struct {
    38  		String string   `validate:"@string[1,"`
    39  		Named  Named    `validate:"@int"`
    40  		Slice  []string `validate:"@slice<@int>"`
    41  		SubStruct
    42  		Struct Struct
    43  	}
    44  
    45  	v := NewStructValidator("json")
    46  	rt := typesx.FromReflectType(reflect.TypeOf(&SomeStruct{}).Elem())
    47  
    48  	_, err := v.New(ctx, &Rule{Type: rt})
    49  	NewWithT(t).Expect(err).NotTo(BeNil())
    50  	t.Logf("\n%v", err)
    51  
    52  	_, err = v.New(ctx, &Rule{Type: rttString})
    53  	NewWithT(t).Expect(err).NotTo(BeNil())
    54  	t.Logf("\n%v", err)
    55  }
    56  
    57  func ExampleNewStructValidator() {
    58  	v := NewStructValidator("json")
    59  
    60  	sv, err := v.New(ctx, &Rule{
    61  		Type: typesx.FromReflectType(reflect.TypeOf(&SomeStruct{}).Elem()),
    62  	})
    63  	if err != nil {
    64  		return
    65  	}
    66  
    67  	s := SomeStruct{
    68  		Slice:       []string{"", ""},
    69  		SliceStruct: []SubStruct{{Int: 0}},
    70  		Map:         map[string]string{"1": "", "11": "", "12": ""},
    71  		MapStruct:   map[string]SubStruct{"222": SubStruct{}},
    72  	}
    73  
    74  	err = sv.Validate(s)
    75  	var (
    76  		errs     = map[string]string{}
    77  		keyPaths = make([]string, 0)
    78  		flatten  = err.(*errors.ErrorSet).Flatten()
    79  	)
    80  
    81  	flatten.Each(
    82  		func(ferr *errors.FieldError) {
    83  			errs[ferr.Field.String()] = strconv.Quote(ferr.Error.Error())
    84  			keyPaths = append(keyPaths, ferr.Field.String())
    85  		},
    86  	)
    87  
    88  	sort.Strings(keyPaths)
    89  
    90  	for i := range keyPaths {
    91  		k := keyPaths[i]
    92  		fmt.Println(k, errs[k])
    93  	}
    94  
    95  	// Output:
    96  	// JustRequired "missing required field"
    97  	// Map.1 "missing required field"
    98  	// Map.1/key "string length should be larger than 2, but got invalid value 1"
    99  	// Map.11 "missing required field"
   100  	// Map.12 "missing required field"
   101  	// MapStruct.222.float "missing required field"
   102  	// MapStruct.222.int "missing required field"
   103  	// MapStruct.222.uint "missing required field"
   104  	// Named "missing required field"
   105  	// PtrFloat "missing required field"
   106  	// PtrInt "missing required field"
   107  	// PtrString "missing required field"
   108  	// PtrUint "missing required field"
   109  	// SliceStruct[0].float "missing required field"
   110  	// SliceStruct[0].int "missing required field"
   111  	// SliceStruct[0].uint "missing required field"
   112  	// Slice[0] "missing required field"
   113  	// Slice[1] "missing required field"
   114  	// SomeStringer "missing required field"
   115  	// String "missing required field"
   116  	// Struct.float "missing required field"
   117  	// Struct.int "missing required field"
   118  	// Struct.uint "missing required field"
   119  	// float "missing required field"
   120  	// int "missing required field"
   121  	// uint "missing required field"
   122  }