github.com/goravel/framework@v1.13.9/validation/errors_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  
     6  	httpvalidate "github.com/goravel/framework/contracts/validation"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestOne(t *testing.T) {
    12  	var maker *Validation
    13  	tests := []struct {
    14  		describe  string
    15  		data      any
    16  		rules     map[string]string
    17  		options   []httpvalidate.Option
    18  		expectRes string
    19  	}{
    20  		{
    21  			describe: "errors is empty",
    22  			data:     map[string]any{"a": "aa"},
    23  			rules:    map[string]string{"a": "required"},
    24  		},
    25  		{
    26  			describe:  "errors isn't empty",
    27  			data:      map[string]any{"a": ""},
    28  			rules:     map[string]string{"a": "required"},
    29  			expectRes: "a is required to not be empty",
    30  		},
    31  		{
    32  			describe: "errors isn't empty when setting messages option",
    33  			data:     map[string]any{"a": ""},
    34  			rules:    map[string]string{"a": "required"},
    35  			options: []httpvalidate.Option{
    36  				Messages(map[string]string{"a.required": "a can't be empty"}),
    37  			},
    38  			expectRes: "a can't be empty",
    39  		},
    40  		{
    41  			describe: "errors isn't empty when setting attributes option",
    42  			data:     map[string]any{"a": ""},
    43  			rules:    map[string]string{"a": "required"},
    44  			options: []httpvalidate.Option{
    45  				Attributes(map[string]string{"a": "aa"}),
    46  			},
    47  			expectRes: "aa is required to not be empty",
    48  		},
    49  		{
    50  			describe: "errors isn't empty when setting messages and attributes option",
    51  			data:     map[string]any{"a": ""},
    52  			rules:    map[string]string{"a": "required"},
    53  			options: []httpvalidate.Option{
    54  				Messages(map[string]string{"a.required": ":attribute can't be empty"}),
    55  				Attributes(map[string]string{"a": "aa"}),
    56  			},
    57  			expectRes: "aa can't be empty",
    58  		},
    59  	}
    60  
    61  	for _, test := range tests {
    62  		maker = NewValidation()
    63  		validator, err := maker.Make(
    64  			test.data,
    65  			test.rules,
    66  			test.options...,
    67  		)
    68  		assert.Nil(t, err, test.describe)
    69  		if test.expectRes != "" {
    70  			assert.Equal(t, test.expectRes, validator.Errors().One(), test.describe)
    71  		}
    72  	}
    73  }
    74  
    75  func TestGet(t *testing.T) {
    76  	var maker *Validation
    77  	tests := []struct {
    78  		describe string
    79  		data     any
    80  		rules    map[string]string
    81  		expectA  map[string]string
    82  		expectB  map[string]string
    83  	}{
    84  		{
    85  			describe: "errors is empty",
    86  			data:     map[string]any{"a": "aa", "b": "bb"},
    87  			rules:    map[string]string{"a": "required", "b": "required"},
    88  		},
    89  		{
    90  			describe: "errors isn't empty",
    91  			data:     map[string]any{"c": "cc"},
    92  			rules:    map[string]string{"a": "required", "b": "required"},
    93  			expectA:  map[string]string{"required": "a is required to not be empty"},
    94  			expectB:  map[string]string{"required": "b is required to not be empty"},
    95  		},
    96  	}
    97  
    98  	for _, test := range tests {
    99  		maker = NewValidation()
   100  		validator, err := maker.Make(
   101  			test.data,
   102  			test.rules,
   103  		)
   104  		assert.Nil(t, err, test.describe)
   105  		if len(test.expectA) > 0 {
   106  			assert.Equal(t, test.expectA, validator.Errors().Get("a"), test.describe)
   107  		}
   108  		if len(test.expectB) > 0 {
   109  			assert.Equal(t, test.expectB, validator.Errors().Get("b"), test.describe)
   110  		}
   111  	}
   112  }
   113  
   114  func TestAll(t *testing.T) {
   115  	var maker *Validation
   116  	tests := []struct {
   117  		describe  string
   118  		data      any
   119  		rules     map[string]string
   120  		expectRes map[string]map[string]string
   121  	}{
   122  		{
   123  			describe:  "errors is empty",
   124  			data:      map[string]any{"a": "aa", "b": "bb"},
   125  			rules:     map[string]string{"a": "required", "b": "required"},
   126  			expectRes: map[string]map[string]string{},
   127  		},
   128  		{
   129  			describe: "errors isn't empty",
   130  			data:     map[string]any{"c": "cc"},
   131  			rules:    map[string]string{"a": "required", "b": "required"},
   132  			expectRes: map[string]map[string]string{
   133  				"a": {"required": "a is required to not be empty"},
   134  				"b": {"required": "b is required to not be empty"},
   135  			},
   136  		},
   137  	}
   138  
   139  	for _, test := range tests {
   140  		maker = NewValidation()
   141  		validator, err := maker.Make(
   142  			test.data,
   143  			test.rules,
   144  		)
   145  		assert.Nil(t, err, test.describe)
   146  		if len(test.expectRes) > 0 {
   147  			assert.Equal(t, test.expectRes, validator.Errors().All(), test.describe)
   148  		}
   149  	}
   150  }
   151  
   152  func TestHas(t *testing.T) {
   153  	var maker *Validation
   154  	tests := []struct {
   155  		describe  string
   156  		data      any
   157  		rules     map[string]string
   158  		expectRes bool
   159  	}{
   160  		{
   161  			describe: "errors is empty",
   162  			data:     map[string]any{"a": "aa", "b": "bb"},
   163  			rules:    map[string]string{"a": "required", "b": "required"},
   164  		},
   165  		{
   166  			describe:  "errors isn't empty",
   167  			data:      map[string]any{"c": "cc"},
   168  			rules:     map[string]string{"a": "required", "b": "required"},
   169  			expectRes: true,
   170  		},
   171  	}
   172  
   173  	for _, test := range tests {
   174  		maker = NewValidation()
   175  		validator, err := maker.Make(
   176  			test.data,
   177  			test.rules,
   178  		)
   179  		assert.Nil(t, err, test.describe)
   180  		if test.expectRes {
   181  			assert.Equal(t, test.expectRes, validator.Errors().Has("a"), test.describe)
   182  		}
   183  	}
   184  }