...

Source file src/github.com/go-playground/validator/v10/non-standard/validators/notblank_test.go

Documentation: github.com/go-playground/validator/v10/non-standard/validators

     1  package validators
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-playground/assert/v2"
     7  	"github.com/go-playground/validator/v10"
     8  )
     9  
    10  type test struct {
    11  	String    string      `validate:"notblank"`
    12  	Array     []int       `validate:"notblank"`
    13  	Pointer   *int        `validate:"notblank"`
    14  	Number    int         `validate:"notblank"`
    15  	Interface interface{} `validate:"notblank"`
    16  	Func      func()      `validate:"notblank"`
    17  }
    18  
    19  func TestNotBlank(t *testing.T) {
    20  	v := validator.New()
    21  	err := v.RegisterValidation("notblank", NotBlank)
    22  	assert.Equal(t, nil, err)
    23  
    24  	// Errors
    25  	var x *int
    26  	invalid := test{
    27  		String:    " \x1c\x1d\x1e\x1f\r\n",
    28  		Array:     []int{},
    29  		Pointer:   x,
    30  		Number:    0,
    31  		Interface: nil,
    32  		Func:      nil,
    33  	}
    34  	fieldsWithError := []string{
    35  		"String",
    36  		"Array",
    37  		"Pointer",
    38  		"Number",
    39  		"Interface",
    40  		"Func",
    41  	}
    42  
    43  	errors := v.Struct(invalid).(validator.ValidationErrors)
    44  	var fields []string
    45  	for _, err := range errors {
    46  		fields = append(fields, err.Field())
    47  	}
    48  
    49  	assert.Equal(t, fieldsWithError, fields)
    50  
    51  	// No errors
    52  	y := 1
    53  	x = &y
    54  	valid := test{
    55  		String:    "str",
    56  		Array:     []int{1},
    57  		Pointer:   x,
    58  		Number:    1,
    59  		Interface: "value",
    60  		Func:      func() {},
    61  	}
    62  
    63  	err = v.Struct(valid)
    64  	assert.Equal(t, nil, err)
    65  }
    66  

View as plain text