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

     1  package validation
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/spf13/cast"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	httpvalidate "github.com/goravel/framework/contracts/validation"
    12  )
    13  
    14  func TestMake(t *testing.T) {
    15  	type Data struct {
    16  		A string
    17  	}
    18  
    19  	tests := []struct {
    20  		description        string
    21  		data               any
    22  		rules              map[string]string
    23  		options            []httpvalidate.Option
    24  		expectValidator    bool
    25  		expectErr          error
    26  		expectData         Data
    27  		expectErrors       bool
    28  		expectErrorMessage string
    29  	}{
    30  		{
    31  			description:     "success when data is map[string]any",
    32  			data:            map[string]any{"a": "b"},
    33  			rules:           map[string]string{"a": "required"},
    34  			expectValidator: true,
    35  			expectData:      Data{A: "b"},
    36  		},
    37  		{
    38  			description:     "success when data is struct",
    39  			data:            &Data{A: "b"},
    40  			rules:           map[string]string{"A": "required"},
    41  			expectValidator: true,
    42  			expectData:      Data{A: "b"},
    43  		},
    44  		{
    45  			description: "error when data isn't map[string]any or struct",
    46  			data:        "1",
    47  			rules:       map[string]string{"a": "required"},
    48  			expectErr:   errors.New("data must be map[string]any or struct"),
    49  		},
    50  		{
    51  			description: "error when data is empty map",
    52  			data:        map[string]any{},
    53  			rules:       map[string]string{"a": "required"},
    54  			expectErr:   errors.New("data can't be empty"),
    55  		},
    56  		{
    57  			description: "error when rule is empty map",
    58  			data:        map[string]any{"a": "b"},
    59  			rules:       map[string]string{},
    60  			expectErr:   errors.New("rules can't be empty"),
    61  		},
    62  		{
    63  			description: "error when PrepareForValidation returns error",
    64  			data:        map[string]any{"a": "b"},
    65  			rules:       map[string]string{"a": "required"},
    66  			options: []httpvalidate.Option{
    67  				PrepareForValidation(func(data httpvalidate.Data) error {
    68  					return errors.New("error")
    69  				}),
    70  			},
    71  			expectErr: errors.New("error"),
    72  		},
    73  		{
    74  			description: "success when data is map[string]any and with PrepareForValidation",
    75  			data:        map[string]any{"a": "b"},
    76  			rules:       map[string]string{"a": "required"},
    77  			options: []httpvalidate.Option{
    78  				PrepareForValidation(func(data httpvalidate.Data) error {
    79  					if _, exist := data.Get("a"); exist {
    80  						return data.Set("a", "c")
    81  					}
    82  
    83  					return nil
    84  				}),
    85  			},
    86  			expectValidator: true,
    87  			expectData:      Data{A: "c"},
    88  		},
    89  		{
    90  			description: "contain errors when data is map[string]any and with Messages, Attributes, PrepareForValidation",
    91  			data:        map[string]any{"a": "aa"},
    92  			rules:       map[string]string{"a": "required", "b": "required"},
    93  			options: []httpvalidate.Option{
    94  				Messages(map[string]string{
    95  					"b.required": ":attribute can't be empty",
    96  				}),
    97  				Attributes(map[string]string{
    98  					"b": "B",
    99  				}),
   100  				PrepareForValidation(func(data httpvalidate.Data) error {
   101  					if _, exist := data.Get("a"); exist {
   102  						return data.Set("a", "c")
   103  					}
   104  
   105  					return nil
   106  				}),
   107  			},
   108  			expectValidator:    true,
   109  			expectData:         Data{A: "c"},
   110  			expectErrors:       true,
   111  			expectErrorMessage: "B can't be empty",
   112  		},
   113  		{
   114  			description: "success when data is struct and with PrepareForValidation",
   115  			data:        &Data{A: "b"},
   116  			rules:       map[string]string{"A": "required"},
   117  			options: []httpvalidate.Option{
   118  				PrepareForValidation(func(data httpvalidate.Data) error {
   119  					if _, exist := data.Get("A"); exist {
   120  						return data.Set("A", "c")
   121  					}
   122  
   123  					return nil
   124  				}),
   125  			},
   126  			expectValidator: true,
   127  			expectData:      Data{A: "c"},
   128  		},
   129  		{
   130  			description: "contain errors when data is struct and with Messages, Attributes, PrepareForValidation",
   131  			data:        &Data{A: "b"},
   132  			rules:       map[string]string{"A": "required", "B": "required"},
   133  			options: []httpvalidate.Option{
   134  				Messages(map[string]string{
   135  					"B.required": ":attribute can't be empty",
   136  				}),
   137  				Attributes(map[string]string{
   138  					"B": "b",
   139  				}),
   140  				PrepareForValidation(func(data httpvalidate.Data) error {
   141  					if _, exist := data.Get("a"); exist {
   142  						return data.Set("a", "c")
   143  					}
   144  
   145  					return nil
   146  				}),
   147  			},
   148  			expectValidator:    true,
   149  			expectData:         Data{A: "c"},
   150  			expectErrors:       true,
   151  			expectErrorMessage: "b can't be empty",
   152  		},
   153  	}
   154  
   155  	for _, test := range tests {
   156  		t.Run(test.description, func(t *testing.T) {
   157  			validation := NewValidation()
   158  			validator, err := validation.Make(test.data, test.rules, test.options...)
   159  			assert.Equal(t, test.expectValidator, validator != nil, test.description)
   160  			assert.Equal(t, test.expectErr, err, test.description)
   161  
   162  			if validator != nil {
   163  				var data Data
   164  				err = validator.Bind(&data)
   165  				assert.Nil(t, err, test.description)
   166  				assert.Equal(t, test.expectData, data, test.description)
   167  				if validator.Fails() {
   168  					assert.Equal(t, test.expectErrorMessage, validator.Errors().One(), test.description)
   169  				}
   170  				assert.Equal(t, test.expectErrors, validator.Fails(), test.description)
   171  			}
   172  		})
   173  	}
   174  }
   175  
   176  type Case struct {
   177  	description string
   178  	setup       func(Case)
   179  }
   180  
   181  func TestRule_Required(t *testing.T) {
   182  	validation := NewValidation()
   183  	tests := []Case{
   184  		{
   185  			description: "success",
   186  			setup: func(c Case) {
   187  				validator, err := validation.Make(map[string]any{
   188  					"name": "goravel",
   189  				}, map[string]string{
   190  					"name": "required",
   191  				})
   192  				assert.Nil(t, err, c.description)
   193  				assert.NotNil(t, validator, c.description)
   194  				assert.False(t, validator.Fails(), c.description)
   195  			},
   196  		},
   197  		{
   198  			description: "success with nested",
   199  			setup: func(c Case) {
   200  				validator, err := validation.Make(map[string]any{
   201  					"name": map[string]string{
   202  						"first": "Goravel",
   203  					},
   204  				}, map[string]string{
   205  					"name.first": "required",
   206  				})
   207  				assert.Nil(t, err, c.description)
   208  				assert.NotNil(t, validator, c.description)
   209  				assert.False(t, validator.Fails(), c.description)
   210  			},
   211  		},
   212  		{
   213  			description: "error when key is empty",
   214  			setup: func(c Case) {
   215  				validator, err := validation.Make(map[string]any{
   216  					"name": "",
   217  				}, map[string]string{
   218  					"name": "required",
   219  				})
   220  				assert.Nil(t, err, c.description)
   221  				assert.NotNil(t, validator, c.description)
   222  				assert.Equal(t, map[string]string{
   223  					"required": "name is required to not be empty",
   224  				}, validator.Errors().Get("name"))
   225  			},
   226  		},
   227  		{
   228  			description: "error when key isn't exist",
   229  			setup: func(c Case) {
   230  				validator, err := validation.Make(map[string]any{
   231  					"name": "Goravel",
   232  				}, map[string]string{
   233  					"name":  "required",
   234  					"name1": "required",
   235  				})
   236  				assert.Nil(t, err, c.description)
   237  				assert.NotNil(t, validator, c.description)
   238  				assert.Equal(t, map[string]string{
   239  					"required": "name1 is required to not be empty",
   240  				}, validator.Errors().Get("name1"))
   241  			},
   242  		},
   243  		{
   244  			description: "error when nested",
   245  			setup: func(c Case) {
   246  				validator, err := validation.Make(map[string]any{
   247  					"name": map[string]string{
   248  						"first": "",
   249  					},
   250  				}, map[string]string{
   251  					"name.first": "required",
   252  				})
   253  				assert.Nil(t, err, c.description)
   254  				assert.NotNil(t, validator, c.description)
   255  				assert.Equal(t, map[string]string{
   256  					"required": "name.first is required to not be empty",
   257  				}, validator.Errors().Get("name.first"))
   258  			},
   259  		},
   260  	}
   261  
   262  	for _, test := range tests {
   263  		t.Run(test.description, func(t *testing.T) {
   264  			test.setup(test)
   265  		})
   266  	}
   267  }
   268  
   269  func TestRule_RequiredIf(t *testing.T) {
   270  	validation := NewValidation()
   271  	tests := []Case{
   272  		{
   273  			description: "success when required_if is true",
   274  			setup: func(c Case) {
   275  				validator, err := validation.Make(map[string]any{
   276  					"name":  "goravel",
   277  					"name1": "goravel1",
   278  				}, map[string]string{
   279  					"name":  "required",
   280  					"name1": "required_if:name,goravel,goravel1",
   281  				})
   282  				assert.Nil(t, err, c.description)
   283  				assert.NotNil(t, validator, c.description)
   284  				assert.False(t, validator.Fails(), c.description)
   285  			},
   286  		},
   287  		{
   288  			description: "success when required_if is false",
   289  			setup: func(c Case) {
   290  				validator, err := validation.Make(map[string]any{
   291  					"name": "goravel2",
   292  				}, map[string]string{
   293  					"name":  "required",
   294  					"name1": "required_if:name,goravel,goravel1",
   295  				})
   296  				assert.Nil(t, err, c.description)
   297  				assert.NotNil(t, validator, c.description)
   298  				assert.False(t, validator.Fails(), c.description)
   299  			},
   300  		},
   301  		{
   302  			description: "error when required_if is true and key is empty",
   303  			setup: func(c Case) {
   304  				validator, err := validation.Make(map[string]any{
   305  					"name":  "goravel",
   306  					"name1": "",
   307  				}, map[string]string{
   308  					"name":  "required",
   309  					"name1": "required_if:name,goravel,goravel1",
   310  				})
   311  				assert.Nil(t, err, c.description)
   312  				assert.NotNil(t, validator, c.description)
   313  				assert.Equal(t, map[string]string{
   314  					"required_if": "name1 is required when name is [goravel,goravel1]",
   315  				}, validator.Errors().Get("name1"))
   316  			},
   317  		},
   318  		{
   319  			description: "error when required_if is true and key isn't exist",
   320  			setup: func(c Case) {
   321  				validator, err := validation.Make(map[string]any{
   322  					"name": "goravel",
   323  				}, map[string]string{
   324  					"name":  "required",
   325  					"name1": "required_if:name,goravel,goravel1",
   326  				})
   327  				assert.Nil(t, err, c.description)
   328  				assert.NotNil(t, validator, c.description)
   329  				assert.Equal(t, map[string]string{
   330  					"required_if": "name1 is required when name is [goravel,goravel1]",
   331  				}, validator.Errors().Get("name1"))
   332  			},
   333  		},
   334  	}
   335  
   336  	for _, test := range tests {
   337  		t.Run(test.description, func(t *testing.T) {
   338  			test.setup(test)
   339  		})
   340  	}
   341  }
   342  
   343  func TestRule_RequiredUnless(t *testing.T) {
   344  	validation := NewValidation()
   345  	tests := []Case{
   346  		{
   347  			description: "success when required_unless is true",
   348  			setup: func(c Case) {
   349  				validator, err := validation.Make(map[string]any{
   350  					"name":  "goravel",
   351  					"name1": "goravel1",
   352  				}, map[string]string{
   353  					"name":  "required",
   354  					"name1": "required_unless:name,hello,hello1",
   355  				})
   356  				assert.Nil(t, err, c.description)
   357  				assert.NotNil(t, validator, c.description)
   358  				assert.False(t, validator.Fails(), c.description)
   359  			},
   360  		},
   361  		{
   362  			description: "success when required_unless is false",
   363  			setup: func(c Case) {
   364  				validator, err := validation.Make(map[string]any{
   365  					"name": "goravel",
   366  				}, map[string]string{
   367  					"name":  "required",
   368  					"name1": "required_unless:name,goravel,goravel1",
   369  				})
   370  				assert.Nil(t, err, c.description)
   371  				assert.NotNil(t, validator, c.description)
   372  				assert.False(t, validator.Fails(), c.description)
   373  			},
   374  		},
   375  		{
   376  			description: "error when required_unless is true and key is empty",
   377  			setup: func(c Case) {
   378  				validator, err := validation.Make(map[string]any{
   379  					"name":  "goravel",
   380  					"name1": "",
   381  				}, map[string]string{
   382  					"name":  "required",
   383  					"name1": "required_unless:name,hello,hello1",
   384  				})
   385  				assert.Nil(t, err, c.description)
   386  				assert.NotNil(t, validator, c.description)
   387  				assert.Equal(t, map[string]string{
   388  					"required_unless": "name1 field is required unless name is in [hello,hello1]",
   389  				}, validator.Errors().Get("name1"))
   390  			},
   391  		},
   392  		{
   393  			description: "error when required_unless is true and key isn't exist",
   394  			setup: func(c Case) {
   395  				validator, err := validation.Make(map[string]any{
   396  					"name": "goravel",
   397  				}, map[string]string{
   398  					"name":  "required",
   399  					"name1": "required_unless:name,hello,hello1",
   400  				})
   401  				assert.Nil(t, err, c.description)
   402  				assert.NotNil(t, validator, c.description)
   403  				assert.Equal(t, map[string]string{
   404  					"required_unless": "name1 field is required unless name is in [hello,hello1]",
   405  				}, validator.Errors().Get("name1"))
   406  			},
   407  		},
   408  	}
   409  
   410  	for _, test := range tests {
   411  		t.Run(test.description, func(t *testing.T) {
   412  			test.setup(test)
   413  		})
   414  	}
   415  }
   416  
   417  func TestRule_RequiredWith(t *testing.T) {
   418  	validation := NewValidation()
   419  	tests := []Case{
   420  		{
   421  			description: "success when required_with is true",
   422  			setup: func(c Case) {
   423  				validator, err := validation.Make(map[string]any{
   424  					"name":  "goravel",
   425  					"name2": "goravel2",
   426  				}, map[string]string{
   427  					"name":  "required",
   428  					"name2": "required_with:name,name1",
   429  				})
   430  				assert.Nil(t, err, c.description)
   431  				assert.NotNil(t, validator, c.description)
   432  				assert.False(t, validator.Fails(), c.description)
   433  			},
   434  		},
   435  		{
   436  			description: "success when required_with is false",
   437  			setup: func(c Case) {
   438  				validator, err := validation.Make(map[string]any{
   439  					"name": "",
   440  				}, map[string]string{
   441  					"name": "required_with:name1,name2",
   442  				})
   443  				assert.Nil(t, err, c.description)
   444  				assert.NotNil(t, validator, c.description)
   445  				assert.False(t, validator.Fails(), c.description)
   446  			},
   447  		},
   448  		{
   449  			description: "error when required_with is true and key is empty",
   450  			setup: func(c Case) {
   451  				validator, err := validation.Make(map[string]any{
   452  					"name":  "goravel",
   453  					"name1": "goravel1",
   454  					"name2": "",
   455  				}, map[string]string{
   456  					"name":  "required",
   457  					"name1": "required",
   458  					"name2": "required_with:name,name1",
   459  				})
   460  				assert.Nil(t, err, c.description)
   461  				assert.NotNil(t, validator, c.description)
   462  				assert.Equal(t, map[string]string{
   463  					"required_with": "name2 field is required when [name,name1] is present",
   464  				}, validator.Errors().Get("name2"))
   465  			},
   466  		},
   467  		{
   468  			description: "error when required_with is true and key isn't exist",
   469  			setup: func(c Case) {
   470  				validator, err := validation.Make(map[string]any{
   471  					"name":  "goravel",
   472  					"name1": "goravel1",
   473  				}, map[string]string{
   474  					"name":  "required",
   475  					"name1": "required",
   476  					"name2": "required_with:name,name1",
   477  				})
   478  				assert.Nil(t, err, c.description)
   479  				assert.NotNil(t, validator, c.description)
   480  				assert.Equal(t, map[string]string{
   481  					"required_with": "name2 field is required when [name,name1] is present",
   482  				}, validator.Errors().Get("name2"))
   483  			},
   484  		},
   485  	}
   486  
   487  	for _, test := range tests {
   488  		t.Run(test.description, func(t *testing.T) {
   489  			test.setup(test)
   490  		})
   491  	}
   492  }
   493  
   494  func TestRule_RequiredWithAll(t *testing.T) {
   495  	validation := NewValidation()
   496  	tests := []Case{
   497  		{
   498  			description: "success when required_with_all is true",
   499  			setup: func(c Case) {
   500  				validator, err := validation.Make(map[string]any{
   501  					"name":  "goravel",
   502  					"name1": "goravel1",
   503  					"name2": "goravel2",
   504  				}, map[string]string{
   505  					"name":  "required",
   506  					"name1": "required",
   507  					"name2": "required_with_all:name,name1",
   508  				})
   509  				assert.Nil(t, err, c.description)
   510  				assert.NotNil(t, validator, c.description)
   511  				assert.False(t, validator.Fails(), c.description)
   512  			},
   513  		},
   514  		{
   515  			description: "success when required_with_all is true",
   516  			setup: func(c Case) {
   517  				validator, err := validation.Make(map[string]any{
   518  					"name":  "goravel",
   519  					"name1": "",
   520  					"name2": "goravel2",
   521  				}, map[string]string{
   522  					"name":  "required",
   523  					"name2": "required_with_all:name,name1",
   524  				})
   525  				assert.Nil(t, err, c.description)
   526  				assert.NotNil(t, validator, c.description)
   527  				assert.False(t, validator.Fails(), c.description)
   528  			},
   529  		},
   530  		{
   531  			description: "success when required_with_all is false",
   532  			setup: func(c Case) {
   533  				validator, err := validation.Make(map[string]any{
   534  					"name": "",
   535  				}, map[string]string{
   536  					"name": "required_with_all:name1,name2",
   537  				})
   538  				assert.Nil(t, err, c.description)
   539  				assert.NotNil(t, validator, c.description)
   540  				assert.False(t, validator.Fails(), c.description)
   541  			},
   542  		},
   543  		{
   544  			description: "error when required_with_all is true and key is empty",
   545  			setup: func(c Case) {
   546  				validator, err := validation.Make(map[string]any{
   547  					"name":  "goravel",
   548  					"name1": "goravel1",
   549  					"name2": "",
   550  				}, map[string]string{
   551  					"name":  "required",
   552  					"name1": "required",
   553  					"name2": "required_with_all:name,name1",
   554  				})
   555  				assert.Nil(t, err, c.description)
   556  				assert.NotNil(t, validator, c.description)
   557  				assert.Equal(t, map[string]string{
   558  					"required_with_all": "name2 field is required when [name,name1] is present",
   559  				}, validator.Errors().Get("name2"))
   560  			},
   561  		},
   562  		{
   563  			description: "error when required_with is true and key isn't exist",
   564  			setup: func(c Case) {
   565  				validator, err := validation.Make(map[string]any{
   566  					"name":  "goravel",
   567  					"name1": "goravel1",
   568  				}, map[string]string{
   569  					"name":  "required",
   570  					"name1": "required",
   571  					"name2": "required_with_all:name,name1",
   572  				})
   573  				assert.Nil(t, err, c.description)
   574  				assert.NotNil(t, validator, c.description)
   575  				assert.Equal(t, map[string]string{
   576  					"required_with_all": "name2 field is required when [name,name1] is present",
   577  				}, validator.Errors().Get("name2"))
   578  			},
   579  		},
   580  	}
   581  
   582  	for _, test := range tests {
   583  		t.Run(test.description, func(t *testing.T) {
   584  			test.setup(test)
   585  		})
   586  	}
   587  }
   588  
   589  func TestRule_RequiredWithout(t *testing.T) {
   590  	validation := NewValidation()
   591  	tests := []Case{
   592  		{
   593  			description: "success when required_without is true",
   594  			setup: func(c Case) {
   595  				validator, err := validation.Make(map[string]any{
   596  					"name":  "goravel",
   597  					"name2": "goravel2",
   598  				}, map[string]string{
   599  					"name":  "required",
   600  					"name2": "required_without:name,name1",
   601  				})
   602  				assert.Nil(t, err, c.description)
   603  				assert.NotNil(t, validator, c.description)
   604  				assert.False(t, validator.Fails(), c.description)
   605  			},
   606  		},
   607  		{
   608  			description: "success when required_without is false",
   609  			setup: func(c Case) {
   610  				validator, err := validation.Make(map[string]any{
   611  					"name":  "",
   612  					"name1": "",
   613  					"name2": "",
   614  				}, map[string]string{
   615  					"name": "required_without:name1,name2",
   616  				})
   617  				assert.Nil(t, err, c.description)
   618  				assert.NotNil(t, validator, c.description)
   619  				assert.False(t, validator.Fails(), c.description)
   620  			},
   621  		},
   622  		{
   623  			description: "error when required_without is true and key is empty",
   624  			setup: func(c Case) {
   625  				validator, err := validation.Make(map[string]any{
   626  					"name":  "goravel",
   627  					"name2": "",
   628  				}, map[string]string{
   629  					"name":  "required",
   630  					"name2": "required_without:name,name1",
   631  				})
   632  				assert.Nil(t, err, c.description)
   633  				assert.NotNil(t, validator, c.description)
   634  				assert.Equal(t, map[string]string{
   635  					"required_without": "name2 field is required when [name,name1] is not present",
   636  				}, validator.Errors().Get("name2"))
   637  			},
   638  		},
   639  		{
   640  			description: "error when required_without is true and key isn't exist",
   641  			setup: func(c Case) {
   642  				validator, err := validation.Make(map[string]any{
   643  					"name": "goravel",
   644  				}, map[string]string{
   645  					"name":  "required",
   646  					"name2": "required_without:name,name1",
   647  				})
   648  				assert.Nil(t, err, c.description)
   649  				assert.NotNil(t, validator, c.description)
   650  				assert.Equal(t, map[string]string{
   651  					"required_without": "name2 field is required when [name,name1] is not present",
   652  				}, validator.Errors().Get("name2"))
   653  			},
   654  		},
   655  	}
   656  
   657  	for _, test := range tests {
   658  		t.Run(test.description, func(t *testing.T) {
   659  			test.setup(test)
   660  		})
   661  	}
   662  }
   663  
   664  func TestRule_RequiredWithoutAll(t *testing.T) {
   665  	validation := NewValidation()
   666  	tests := []Case{
   667  		{
   668  			description: "success when required_without_all is true",
   669  			setup: func(c Case) {
   670  				validator, err := validation.Make(map[string]any{
   671  					"name": "goravel",
   672  				}, map[string]string{
   673  					"name": "required_without_all:name1,name2",
   674  				})
   675  				assert.Nil(t, err, c.description)
   676  				assert.NotNil(t, validator, c.description)
   677  				assert.False(t, validator.Fails(), c.description)
   678  			},
   679  		},
   680  		{
   681  			description: "success when required_without_all is false",
   682  			setup: func(c Case) {
   683  				validator, err := validation.Make(map[string]any{
   684  					"name":  "",
   685  					"name1": "",
   686  					"name2": "",
   687  				}, map[string]string{
   688  					"name": "required_without_all:name1,name2",
   689  				})
   690  				assert.Nil(t, err, c.description)
   691  				assert.NotNil(t, validator, c.description)
   692  				assert.False(t, validator.Fails(), c.description)
   693  			},
   694  		},
   695  		{
   696  			description: "error when required_without_all is true and key is empty",
   697  			setup: func(c Case) {
   698  				validator, err := validation.Make(map[string]any{
   699  					"name": "",
   700  				}, map[string]string{
   701  					"name": "required_without_all:name1,name2",
   702  				})
   703  				assert.Nil(t, err, c.description)
   704  				assert.NotNil(t, validator, c.description)
   705  				assert.Equal(t, map[string]string{
   706  					"required_without_all": "name field is required when none of [name1,name2] are present",
   707  				}, validator.Errors().Get("name"))
   708  			},
   709  		},
   710  		{
   711  			description: "error when required_without_all is true and key isn't exist",
   712  			setup: func(c Case) {
   713  				validator, err := validation.Make(map[string]any{
   714  					"name3": "goravel3",
   715  				}, map[string]string{
   716  					"name": "required_without_all:name1,name2",
   717  				})
   718  				assert.Nil(t, err, c.description)
   719  				assert.NotNil(t, validator, c.description)
   720  				assert.Equal(t, map[string]string{
   721  					"required_without_all": "name field is required when none of [name1,name2] are present",
   722  				}, validator.Errors().Get("name"))
   723  			},
   724  		},
   725  	}
   726  
   727  	for _, test := range tests {
   728  		t.Run(test.description, func(t *testing.T) {
   729  			test.setup(test)
   730  		})
   731  	}
   732  }
   733  
   734  func TestRule_Int(t *testing.T) {
   735  	validation := NewValidation()
   736  	tests := []Case{
   737  		{
   738  			description: "success",
   739  			setup: func(c Case) {
   740  				validator, err := validation.Make(map[string]any{
   741  					"name": 1,
   742  				}, map[string]string{
   743  					"name": "required|int",
   744  				})
   745  				assert.Nil(t, err, c.description)
   746  				assert.NotNil(t, validator, c.description)
   747  				assert.False(t, validator.Fails(), c.description)
   748  			},
   749  		},
   750  		{
   751  			description: "success with range",
   752  			setup: func(c Case) {
   753  				validator, err := validation.Make(map[string]any{
   754  					"name": 3,
   755  				}, map[string]string{
   756  					"name": "required|int:2,4",
   757  				})
   758  				assert.Nil(t, err, c.description)
   759  				assert.NotNil(t, validator, c.description)
   760  				assert.False(t, validator.Fails(), c.description)
   761  			},
   762  		},
   763  		{
   764  			description: "error when type error",
   765  			setup: func(c Case) {
   766  				validator, err := validation.Make(map[string]any{
   767  					"name": "1",
   768  				}, map[string]string{
   769  					"name": "required|int",
   770  				})
   771  				assert.Nil(t, err, c.description)
   772  				assert.NotNil(t, validator, c.description)
   773  				assert.Equal(t, map[string]string{
   774  					"int": "name value must be an integer",
   775  				}, validator.Errors().Get("name"))
   776  			},
   777  		},
   778  		{
   779  			description: "error when value doesn't in the right range",
   780  			setup: func(c Case) {
   781  				validator, err := validation.Make(map[string]any{
   782  					"name": 1,
   783  				}, map[string]string{
   784  					"name": "required|int:2,4",
   785  				})
   786  				assert.Nil(t, err, c.description)
   787  				assert.NotNil(t, validator, c.description)
   788  				assert.Equal(t, map[string]string{
   789  					"int": "name value must be an integer and in the range 2 - 4",
   790  				}, validator.Errors().Get("name"))
   791  			},
   792  		},
   793  	}
   794  
   795  	for _, test := range tests {
   796  		t.Run(test.description, func(t *testing.T) {
   797  			test.setup(test)
   798  		})
   799  	}
   800  }
   801  
   802  func TestRule_Uint(t *testing.T) {
   803  	validation := NewValidation()
   804  	tests := []Case{
   805  		{
   806  			description: "success",
   807  			setup: func(c Case) {
   808  				validator, err := validation.Make(map[string]any{
   809  					"name": 1,
   810  				}, map[string]string{
   811  					"name": "required|uint",
   812  				})
   813  				assert.Nil(t, err, c.description)
   814  				assert.NotNil(t, validator, c.description)
   815  				assert.False(t, validator.Fails(), c.description)
   816  			},
   817  		},
   818  		{
   819  			description: "error when type error",
   820  			setup: func(c Case) {
   821  				validator, err := validation.Make(map[string]any{
   822  					"name": "s",
   823  				}, map[string]string{
   824  					"name": "required|uint",
   825  				})
   826  				assert.Nil(t, err, c.description)
   827  				assert.NotNil(t, validator, c.description)
   828  				assert.Equal(t, map[string]string{
   829  					"uint": "name value must be an unsigned integer(>= 0)",
   830  				}, validator.Errors().Get("name"))
   831  			},
   832  		},
   833  	}
   834  
   835  	for _, test := range tests {
   836  		t.Run(test.description, func(t *testing.T) {
   837  			test.setup(test)
   838  		})
   839  	}
   840  }
   841  
   842  func TestRule_Bool(t *testing.T) {
   843  	validation := NewValidation()
   844  	tests := []Case{
   845  		{
   846  			description: "success",
   847  			setup: func(c Case) {
   848  				validator, err := validation.Make(map[string]any{
   849  					"name1":  "on",
   850  					"name2":  "off",
   851  					"name3":  "yes",
   852  					"name4":  "no",
   853  					"name5":  true,
   854  					"name6":  false,
   855  					"name7":  "true",
   856  					"name8":  "false",
   857  					"name9":  "1",
   858  					"name10": "0",
   859  				}, map[string]string{
   860  					"name1":  "bool",
   861  					"name2":  "bool",
   862  					"name3":  "bool",
   863  					"name4":  "bool",
   864  					"name5":  "bool",
   865  					"name6":  "bool",
   866  					"name7":  "bool",
   867  					"name8":  "bool",
   868  					"name9":  "bool",
   869  					"name10": "bool",
   870  				})
   871  				assert.Nil(t, err, c.description)
   872  				assert.NotNil(t, validator, c.description)
   873  				assert.False(t, validator.Fails(), c.description)
   874  			},
   875  		},
   876  		{
   877  			description: "error when type error",
   878  			setup: func(c Case) {
   879  				validator, err := validation.Make(map[string]any{
   880  					"name1": 1,
   881  					"name2": 0,
   882  					"name3": "a",
   883  				}, map[string]string{
   884  					"name1": "bool",
   885  					"name2": "bool",
   886  					"name3": "bool",
   887  				})
   888  				assert.Nil(t, err, c.description)
   889  				assert.NotNil(t, validator, c.description)
   890  				assert.Equal(t, map[string]string{"bool": "name1 value must be a bool"}, validator.Errors().Get("name1"))
   891  				assert.Nil(t, validator.Errors().Get("name2"))
   892  				assert.Equal(t, map[string]string{"bool": "name3 value must be a bool"}, validator.Errors().Get("name3"))
   893  			},
   894  		},
   895  	}
   896  
   897  	for _, test := range tests {
   898  		t.Run(test.description, func(t *testing.T) {
   899  			test.setup(test)
   900  		})
   901  	}
   902  }
   903  
   904  func TestRule_String(t *testing.T) {
   905  	validation := NewValidation()
   906  	tests := []Case{
   907  		{
   908  			description: "success",
   909  			setup: func(c Case) {
   910  				validator, err := validation.Make(map[string]any{
   911  					"name": "1",
   912  				}, map[string]string{
   913  					"name": "required|string",
   914  				})
   915  				assert.Nil(t, err, c.description)
   916  				assert.NotNil(t, validator, c.description)
   917  				assert.False(t, validator.Fails(), c.description)
   918  			},
   919  		},
   920  		{
   921  			description: "success with range",
   922  			setup: func(c Case) {
   923  				validator, err := validation.Make(map[string]any{
   924  					"name": "abc",
   925  				}, map[string]string{
   926  					"name": "required|string:2,4",
   927  				})
   928  				assert.Nil(t, err, c.description)
   929  				assert.NotNil(t, validator, c.description)
   930  				assert.False(t, validator.Fails(), c.description)
   931  			},
   932  		},
   933  		{
   934  			description: "error when type error",
   935  			setup: func(c Case) {
   936  				validator, err := validation.Make(map[string]any{
   937  					"name": 1,
   938  				}, map[string]string{
   939  					"name": "required|string",
   940  				})
   941  				assert.Nil(t, err, c.description)
   942  				assert.NotNil(t, validator, c.description)
   943  				assert.Equal(t, map[string]string{
   944  					"string": "name value must be a string",
   945  				}, validator.Errors().Get("name"))
   946  			},
   947  		},
   948  		{
   949  			description: "error when value doesn't in the right range",
   950  			setup: func(c Case) {
   951  				validator, err := validation.Make(map[string]any{
   952  					"name": "a",
   953  				}, map[string]string{
   954  					"name": "required|string:2,4",
   955  				})
   956  				assert.Nil(t, err, c.description)
   957  				assert.NotNil(t, validator, c.description)
   958  				assert.Equal(t, map[string]string{
   959  					"string": "name value must be a string",
   960  				}, validator.Errors().Get("name"))
   961  			},
   962  		},
   963  	}
   964  
   965  	for _, test := range tests {
   966  		t.Run(test.description, func(t *testing.T) {
   967  			test.setup(test)
   968  		})
   969  	}
   970  }
   971  
   972  func TestRule_Float(t *testing.T) {
   973  	validation := NewValidation()
   974  	tests := []Case{
   975  		{
   976  			description: "success",
   977  			setup: func(c Case) {
   978  				validator, err := validation.Make(map[string]any{
   979  					"name": 1.1,
   980  				}, map[string]string{
   981  					"name": "required|float",
   982  				})
   983  				assert.Nil(t, err, c.description)
   984  				assert.NotNil(t, validator, c.description)
   985  				assert.False(t, validator.Fails(), c.description)
   986  			},
   987  		},
   988  		{
   989  			description: "error when type error",
   990  			setup: func(c Case) {
   991  				validator, err := validation.Make(map[string]any{
   992  					"name": "a",
   993  				}, map[string]string{
   994  					"name": "required|float",
   995  				})
   996  				assert.Nil(t, err, c.description)
   997  				assert.NotNil(t, validator, c.description)
   998  				assert.Equal(t, map[string]string{
   999  					"float": "name value must be a float",
  1000  				}, validator.Errors().Get("name"))
  1001  			},
  1002  		},
  1003  	}
  1004  
  1005  	for _, test := range tests {
  1006  		t.Run(test.description, func(t *testing.T) {
  1007  			test.setup(test)
  1008  		})
  1009  	}
  1010  }
  1011  
  1012  func TestRule_Slice(t *testing.T) {
  1013  	validation := NewValidation()
  1014  	tests := []Case{
  1015  		{
  1016  			description: "success",
  1017  			setup: func(c Case) {
  1018  				validator, err := validation.Make(map[string]any{
  1019  					"name1": []int{1, 2},
  1020  					"name2": []uint{1, 2},
  1021  					"name3": []string{"a", "b"},
  1022  				}, map[string]string{
  1023  					"name1": "required|slice",
  1024  					"name2": "required|slice",
  1025  					"name3": "required|slice",
  1026  				})
  1027  				assert.Nil(t, err, c.description)
  1028  				assert.NotNil(t, validator, c.description)
  1029  				assert.False(t, validator.Fails(), c.description)
  1030  			},
  1031  		},
  1032  		{
  1033  			description: "error when type error",
  1034  			setup: func(c Case) {
  1035  				validator, err := validation.Make(map[string]any{
  1036  					"name1": 1,
  1037  					"name2": "a",
  1038  					"name3": true,
  1039  				}, map[string]string{
  1040  					"name1": "required|slice",
  1041  					"name2": "required|slice",
  1042  					"name3": "required|slice",
  1043  				})
  1044  				assert.Nil(t, err, c.description)
  1045  				assert.NotNil(t, validator, c.description)
  1046  				assert.Equal(t, map[string]string{"slice": "name1 value must be a slice"}, validator.Errors().Get("name1"))
  1047  				assert.Equal(t, map[string]string{"slice": "name2 value must be a slice"}, validator.Errors().Get("name2"))
  1048  				assert.Equal(t, map[string]string{"slice": "name3 value must be a slice"}, validator.Errors().Get("name3"))
  1049  			},
  1050  		},
  1051  	}
  1052  
  1053  	for _, test := range tests {
  1054  		t.Run(test.description, func(t *testing.T) {
  1055  			test.setup(test)
  1056  		})
  1057  	}
  1058  }
  1059  
  1060  func TestRule_In(t *testing.T) {
  1061  	validation := NewValidation()
  1062  	tests := []Case{
  1063  		{
  1064  			description: "success",
  1065  			setup: func(c Case) {
  1066  				validator, err := validation.Make(map[string]any{
  1067  					"name1": 1,
  1068  					"name2": "a",
  1069  				}, map[string]string{
  1070  					"name1": "required|in:1,2",
  1071  					"name2": "required|in:a,b",
  1072  				})
  1073  				assert.Nil(t, err, c.description)
  1074  				assert.NotNil(t, validator, c.description)
  1075  				assert.False(t, validator.Fails(), c.description)
  1076  			},
  1077  		},
  1078  		{
  1079  			description: "error",
  1080  			setup: func(c Case) {
  1081  				validator, err := validation.Make(map[string]any{
  1082  					"name1": 3,
  1083  					"name2": "c",
  1084  				}, map[string]string{
  1085  					"name1": "required|in:1,2",
  1086  					"name2": "required|in:a,b",
  1087  				})
  1088  				assert.Nil(t, err, c.description)
  1089  				assert.NotNil(t, validator, c.description)
  1090  				assert.Equal(t, map[string]string{"in": "name1 value must be in the enum [1 2]"}, validator.Errors().Get("name1"))
  1091  				assert.Equal(t, map[string]string{"in": "name2 value must be in the enum [a b]"}, validator.Errors().Get("name2"))
  1092  			},
  1093  		},
  1094  	}
  1095  
  1096  	for _, test := range tests {
  1097  		t.Run(test.description, func(t *testing.T) {
  1098  			test.setup(test)
  1099  		})
  1100  	}
  1101  }
  1102  
  1103  func TestRule_NotIn(t *testing.T) {
  1104  	validation := NewValidation()
  1105  	tests := []Case{
  1106  		{
  1107  			description: "success",
  1108  			setup: func(c Case) {
  1109  				validator, err := validation.Make(map[string]any{
  1110  					"name1": 3,
  1111  					"name2": "c",
  1112  				}, map[string]string{
  1113  					"name1": "required|not_in:1,2",
  1114  					"name2": "required|not_in:a,b",
  1115  				})
  1116  				assert.Nil(t, err, c.description)
  1117  				assert.NotNil(t, validator, c.description)
  1118  				assert.False(t, validator.Fails(), c.description)
  1119  			},
  1120  		},
  1121  		{
  1122  			description: "error",
  1123  			setup: func(c Case) {
  1124  				validator, err := validation.Make(map[string]any{
  1125  					"name1": 1,
  1126  					"name2": "a",
  1127  				}, map[string]string{
  1128  					"name1": "required|not_in:1,2",
  1129  					"name2": "required|not_in:a,b",
  1130  				})
  1131  				assert.Nil(t, err, c.description)
  1132  				assert.NotNil(t, validator, c.description)
  1133  				assert.Equal(t, map[string]string{"not_in": "name1 value must not be in the given enum list [%!d(string=1) %!d(string=2)]"}, validator.Errors().Get("name1"))
  1134  				assert.Equal(t, map[string]string{"not_in": "name2 value must not be in the given enum list [%!d(string=a) %!d(string=b)]"}, validator.Errors().Get("name2"))
  1135  			},
  1136  		},
  1137  	}
  1138  
  1139  	for _, test := range tests {
  1140  		t.Run(test.description, func(t *testing.T) {
  1141  			test.setup(test)
  1142  		})
  1143  	}
  1144  }
  1145  
  1146  func TestRule_StartsWith(t *testing.T) {
  1147  	validation := NewValidation()
  1148  	tests := []Case{
  1149  		{
  1150  			description: "success",
  1151  			setup: func(c Case) {
  1152  				validator, err := validation.Make(map[string]any{
  1153  					"name": "abc",
  1154  				}, map[string]string{
  1155  					"name": "required|starts_with:ab",
  1156  				})
  1157  				assert.Nil(t, err, c.description)
  1158  				assert.NotNil(t, validator, c.description)
  1159  				assert.False(t, validator.Fails(), c.description)
  1160  			},
  1161  		},
  1162  		{
  1163  			description: "error",
  1164  			setup: func(c Case) {
  1165  				validator, err := validation.Make(map[string]any{
  1166  					"name": "a",
  1167  				}, map[string]string{
  1168  					"name": "required|starts_with:ab",
  1169  				})
  1170  				assert.Nil(t, err, c.description)
  1171  				assert.NotNil(t, validator, c.description)
  1172  				assert.Equal(t, map[string]string{"starts_with": "name value does not start with ab"}, validator.Errors().Get("name"))
  1173  			},
  1174  		},
  1175  	}
  1176  
  1177  	for _, test := range tests {
  1178  		t.Run(test.description, func(t *testing.T) {
  1179  			test.setup(test)
  1180  		})
  1181  	}
  1182  }
  1183  
  1184  func TestRule_EndsWith(t *testing.T) {
  1185  	validation := NewValidation()
  1186  	tests := []Case{
  1187  		{
  1188  			description: "success",
  1189  			setup: func(c Case) {
  1190  				validator, err := validation.Make(map[string]any{
  1191  					"name": "cab",
  1192  				}, map[string]string{
  1193  					"name": "required|ends_with:ab",
  1194  				})
  1195  				assert.Nil(t, err, c.description)
  1196  				assert.NotNil(t, validator, c.description)
  1197  				assert.False(t, validator.Fails(), c.description)
  1198  			},
  1199  		},
  1200  		{
  1201  			description: "error",
  1202  			setup: func(c Case) {
  1203  				validator, err := validation.Make(map[string]any{
  1204  					"name": "a",
  1205  				}, map[string]string{
  1206  					"name": "required|ends_with:ab",
  1207  				})
  1208  				assert.Nil(t, err, c.description)
  1209  				assert.NotNil(t, validator, c.description)
  1210  				assert.Equal(t, map[string]string{"ends_with": "name value does not end with ab"}, validator.Errors().Get("name"))
  1211  			},
  1212  		},
  1213  	}
  1214  
  1215  	for _, test := range tests {
  1216  		t.Run(test.description, func(t *testing.T) {
  1217  			test.setup(test)
  1218  		})
  1219  	}
  1220  }
  1221  
  1222  func TestRule_Between(t *testing.T) {
  1223  	validation := NewValidation()
  1224  	tests := []Case{
  1225  		{
  1226  			description: "success",
  1227  			setup: func(c Case) {
  1228  				validator, err := validation.Make(map[string]any{
  1229  					"name": 2,
  1230  				}, map[string]string{
  1231  					"name": "required|between:1,3",
  1232  				})
  1233  				assert.Nil(t, err, c.description)
  1234  				assert.NotNil(t, validator, c.description)
  1235  				assert.False(t, validator.Fails(), c.description)
  1236  			},
  1237  		},
  1238  		{
  1239  			description: "error",
  1240  			setup: func(c Case) {
  1241  				validator, err := validation.Make(map[string]any{
  1242  					"name": 1,
  1243  				}, map[string]string{
  1244  					"name": "required|between:2,4",
  1245  				})
  1246  				assert.Nil(t, err, c.description)
  1247  				assert.NotNil(t, validator, c.description)
  1248  				assert.Equal(t, map[string]string{"between": "name field did not pass validation"}, validator.Errors().Get("name"))
  1249  			},
  1250  		},
  1251  	}
  1252  
  1253  	for _, test := range tests {
  1254  		t.Run(test.description, func(t *testing.T) {
  1255  			test.setup(test)
  1256  		})
  1257  	}
  1258  }
  1259  
  1260  func TestRule_Max(t *testing.T) {
  1261  	validation := NewValidation()
  1262  	tests := []Case{
  1263  		{
  1264  			description: "success",
  1265  			setup: func(c Case) {
  1266  				validator, err := validation.Make(map[string]any{
  1267  					"name": 2,
  1268  				}, map[string]string{
  1269  					"name": "required|max:3",
  1270  				})
  1271  				assert.Nil(t, err, c.description)
  1272  				assert.NotNil(t, validator, c.description)
  1273  				assert.False(t, validator.Fails(), c.description)
  1274  			},
  1275  		},
  1276  		{
  1277  			description: "error",
  1278  			setup: func(c Case) {
  1279  				validator, err := validation.Make(map[string]any{
  1280  					"name": 4,
  1281  				}, map[string]string{
  1282  					"name": "required|max:3",
  1283  				})
  1284  				assert.Nil(t, err, c.description)
  1285  				assert.NotNil(t, validator, c.description)
  1286  				assert.Equal(t, map[string]string{"max": "name max value is 3"}, validator.Errors().Get("name"))
  1287  			},
  1288  		},
  1289  	}
  1290  
  1291  	for _, test := range tests {
  1292  		t.Run(test.description, func(t *testing.T) {
  1293  			test.setup(test)
  1294  		})
  1295  	}
  1296  }
  1297  
  1298  func TestRule_Min(t *testing.T) {
  1299  	validation := NewValidation()
  1300  	tests := []Case{
  1301  		{
  1302  			description: "success",
  1303  			setup: func(c Case) {
  1304  				validator, err := validation.Make(map[string]any{
  1305  					"name": 3,
  1306  				}, map[string]string{
  1307  					"name": "required|min:3",
  1308  				})
  1309  				assert.Nil(t, err, c.description)
  1310  				assert.NotNil(t, validator, c.description)
  1311  				assert.False(t, validator.Fails(), c.description)
  1312  			},
  1313  		},
  1314  		{
  1315  			description: "error",
  1316  			setup: func(c Case) {
  1317  				validator, err := validation.Make(map[string]any{
  1318  					"name": 2,
  1319  				}, map[string]string{
  1320  					"name": "required|min:3",
  1321  				})
  1322  				assert.Nil(t, err, c.description)
  1323  				assert.NotNil(t, validator, c.description)
  1324  				assert.Equal(t, map[string]string{"min": "name min value is 3"}, validator.Errors().Get("name"))
  1325  			},
  1326  		},
  1327  	}
  1328  
  1329  	for _, test := range tests {
  1330  		t.Run(test.description, func(t *testing.T) {
  1331  			test.setup(test)
  1332  		})
  1333  	}
  1334  }
  1335  
  1336  func TestRule_Eq(t *testing.T) {
  1337  	validation := NewValidation()
  1338  	tests := []Case{
  1339  		{
  1340  			description: "success",
  1341  			setup: func(c Case) {
  1342  				validator, err := validation.Make(map[string]any{
  1343  					"name": "a",
  1344  				}, map[string]string{
  1345  					"name": "required|eq:a",
  1346  				})
  1347  				assert.Nil(t, err, c.description)
  1348  				assert.NotNil(t, validator, c.description)
  1349  				assert.False(t, validator.Fails(), c.description)
  1350  			},
  1351  		},
  1352  		{
  1353  			description: "error",
  1354  			setup: func(c Case) {
  1355  				validator, err := validation.Make(map[string]any{
  1356  					"name": "b",
  1357  				}, map[string]string{
  1358  					"name": "required|eq:a",
  1359  				})
  1360  				assert.Nil(t, err, c.description)
  1361  				assert.NotNil(t, validator, c.description)
  1362  				assert.Equal(t, map[string]string{"eq": "name field did not pass validation"}, validator.Errors().Get("name"))
  1363  			},
  1364  		},
  1365  	}
  1366  
  1367  	for _, test := range tests {
  1368  		t.Run(test.description, func(t *testing.T) {
  1369  			test.setup(test)
  1370  		})
  1371  	}
  1372  }
  1373  
  1374  func TestRule_Ne(t *testing.T) {
  1375  	validation := NewValidation()
  1376  	tests := []Case{
  1377  		{
  1378  			description: "success",
  1379  			setup: func(c Case) {
  1380  				validator, err := validation.Make(map[string]any{
  1381  					"name": "b",
  1382  				}, map[string]string{
  1383  					"name": "required|ne:a",
  1384  				})
  1385  				assert.Nil(t, err, c.description)
  1386  				assert.NotNil(t, validator, c.description)
  1387  				assert.False(t, validator.Fails(), c.description)
  1388  			},
  1389  		},
  1390  		{
  1391  			description: "error",
  1392  			setup: func(c Case) {
  1393  				validator, err := validation.Make(map[string]any{
  1394  					"name": "a",
  1395  				}, map[string]string{
  1396  					"name": "required|ne:a",
  1397  				})
  1398  				assert.Nil(t, err, c.description)
  1399  				assert.NotNil(t, validator, c.description)
  1400  				assert.Equal(t, map[string]string{"ne": "name field did not pass validation"}, validator.Errors().Get("name"))
  1401  			},
  1402  		},
  1403  	}
  1404  
  1405  	for _, test := range tests {
  1406  		t.Run(test.description, func(t *testing.T) {
  1407  			test.setup(test)
  1408  		})
  1409  	}
  1410  }
  1411  
  1412  func TestRule_Lt(t *testing.T) {
  1413  	validation := NewValidation()
  1414  	tests := []Case{
  1415  		{
  1416  			description: "success",
  1417  			setup: func(c Case) {
  1418  				validator, err := validation.Make(map[string]any{
  1419  					"name": 1,
  1420  				}, map[string]string{
  1421  					"name": "required|lt:2",
  1422  				})
  1423  				assert.Nil(t, err, c.description)
  1424  				assert.NotNil(t, validator, c.description)
  1425  				assert.False(t, validator.Fails(), c.description)
  1426  			},
  1427  		},
  1428  		{
  1429  			description: "error",
  1430  			setup: func(c Case) {
  1431  				validator, err := validation.Make(map[string]any{
  1432  					"name": 2,
  1433  				}, map[string]string{
  1434  					"name": "required|lt:1",
  1435  				})
  1436  				assert.Nil(t, err, c.description)
  1437  				assert.NotNil(t, validator, c.description)
  1438  				assert.Equal(t, map[string]string{"lt": "name value should be less than 1"}, validator.Errors().Get("name"))
  1439  			},
  1440  		},
  1441  	}
  1442  
  1443  	for _, test := range tests {
  1444  		t.Run(test.description, func(t *testing.T) {
  1445  			test.setup(test)
  1446  		})
  1447  	}
  1448  }
  1449  
  1450  func TestRule_Gt(t *testing.T) {
  1451  	validation := NewValidation()
  1452  	tests := []Case{
  1453  		{
  1454  			description: "success",
  1455  			setup: func(c Case) {
  1456  				validator, err := validation.Make(map[string]any{
  1457  					"name": 2,
  1458  				}, map[string]string{
  1459  					"name": "required|gt:1",
  1460  				})
  1461  				assert.Nil(t, err, c.description)
  1462  				assert.NotNil(t, validator, c.description)
  1463  				assert.False(t, validator.Fails(), c.description)
  1464  			},
  1465  		},
  1466  		{
  1467  			description: "error",
  1468  			setup: func(c Case) {
  1469  				validator, err := validation.Make(map[string]any{
  1470  					"name": 1,
  1471  				}, map[string]string{
  1472  					"name": "required|gt:2",
  1473  				})
  1474  				assert.Nil(t, err, c.description)
  1475  				assert.NotNil(t, validator, c.description)
  1476  				assert.Equal(t, map[string]string{"gt": "name value should be greater than 2"}, validator.Errors().Get("name"))
  1477  			},
  1478  		},
  1479  	}
  1480  
  1481  	for _, test := range tests {
  1482  		t.Run(test.description, func(t *testing.T) {
  1483  			test.setup(test)
  1484  		})
  1485  	}
  1486  }
  1487  
  1488  func TestRule_Len(t *testing.T) {
  1489  	validation := NewValidation()
  1490  	tests := []Case{
  1491  		{
  1492  			description: "success",
  1493  			setup: func(c Case) {
  1494  				validator, err := validation.Make(map[string]any{
  1495  					"name":  "abc",
  1496  					"name1": [3]string{"a", "b", "c"},
  1497  					"name2": []string{"a", "b", "c"},
  1498  					"name3": map[string]string{
  1499  						"a": "a1",
  1500  						"b": "b1",
  1501  						"c": "c1",
  1502  					},
  1503  				}, map[string]string{
  1504  					"name":  "required|len:3",
  1505  					"name1": "required|len:3",
  1506  					"name2": "required|len:3",
  1507  					"name3": "required|len:3",
  1508  				})
  1509  				assert.Nil(t, err, c.description)
  1510  				assert.NotNil(t, validator, c.description)
  1511  				assert.False(t, validator.Fails(), c.description)
  1512  			},
  1513  		},
  1514  		{
  1515  			description: "error",
  1516  			setup: func(c Case) {
  1517  				validator, err := validation.Make(map[string]any{
  1518  					"name":  "abc",
  1519  					"name1": [3]string{"a", "b", "c"},
  1520  					"name2": []string{"a", "b", "c"},
  1521  					"name3": map[string]string{
  1522  						"a": "a1",
  1523  						"b": "b1",
  1524  						"c": "c1",
  1525  					},
  1526  				}, map[string]string{
  1527  					"name":  "required|len:2",
  1528  					"name1": "required|len:2",
  1529  					"name2": "required|len:2",
  1530  					"name3": "required|len:2",
  1531  				})
  1532  				assert.Nil(t, err, c.description)
  1533  				assert.NotNil(t, validator, c.description)
  1534  				assert.Equal(t, map[string]string{"len": "name field did not pass validation"}, validator.Errors().Get("name"))
  1535  				assert.Equal(t, map[string]string{"len": "name1 field did not pass validation"}, validator.Errors().Get("name1"))
  1536  				assert.Equal(t, map[string]string{"len": "name2 field did not pass validation"}, validator.Errors().Get("name2"))
  1537  				assert.Equal(t, map[string]string{"len": "name3 field did not pass validation"}, validator.Errors().Get("name3"))
  1538  			},
  1539  		},
  1540  	}
  1541  
  1542  	for _, test := range tests {
  1543  		t.Run(test.description, func(t *testing.T) {
  1544  			test.setup(test)
  1545  		})
  1546  	}
  1547  }
  1548  
  1549  func TestRule_MinLen(t *testing.T) {
  1550  	validation := NewValidation()
  1551  	tests := []Case{
  1552  		{
  1553  			description: "success",
  1554  			setup: func(c Case) {
  1555  				validator, err := validation.Make(map[string]any{
  1556  					"name":  "abc",
  1557  					"name1": [3]string{"a", "b", "c"},
  1558  					"name2": []string{"a", "b", "c"},
  1559  					"name3": map[string]string{
  1560  						"a": "a1",
  1561  						"b": "b1",
  1562  						"c": "c1",
  1563  					},
  1564  				}, map[string]string{
  1565  					"name":  "required|min_len:2",
  1566  					"name1": "required|min_len:2",
  1567  					"name2": "required|min_len:2",
  1568  					"name3": "required|min_len:2",
  1569  				})
  1570  				assert.Nil(t, err, c.description)
  1571  				assert.NotNil(t, validator, c.description)
  1572  				assert.False(t, validator.Fails(), c.description)
  1573  			},
  1574  		},
  1575  		{
  1576  			description: "error",
  1577  			setup: func(c Case) {
  1578  				validator, err := validation.Make(map[string]any{
  1579  					"name":  "abc",
  1580  					"name1": [3]string{"a", "b", "c"},
  1581  					"name2": []string{"a", "b", "c"},
  1582  					"name3": map[string]string{
  1583  						"a": "a1",
  1584  						"b": "b1",
  1585  						"c": "c1",
  1586  					},
  1587  				}, map[string]string{
  1588  					"name":  "required|min_len:4",
  1589  					"name1": "required|min_len:4",
  1590  					"name2": "required|min_len:4",
  1591  					"name3": "required|min_len:4",
  1592  				})
  1593  				assert.Nil(t, err, c.description)
  1594  				assert.NotNil(t, validator, c.description)
  1595  				assert.Equal(t, map[string]string{"min_len": "name min length is 4"}, validator.Errors().Get("name"))
  1596  				assert.Equal(t, map[string]string{"min_len": "name1 min length is 4"}, validator.Errors().Get("name1"))
  1597  				assert.Equal(t, map[string]string{"min_len": "name2 min length is 4"}, validator.Errors().Get("name2"))
  1598  				assert.Equal(t, map[string]string{"min_len": "name3 min length is 4"}, validator.Errors().Get("name3"))
  1599  			},
  1600  		},
  1601  	}
  1602  
  1603  	for _, test := range tests {
  1604  		t.Run(test.description, func(t *testing.T) {
  1605  			test.setup(test)
  1606  		})
  1607  	}
  1608  }
  1609  
  1610  func TestRule_MaxLen(t *testing.T) {
  1611  	validation := NewValidation()
  1612  	tests := []Case{
  1613  		{
  1614  			description: "success",
  1615  			setup: func(c Case) {
  1616  				validator, err := validation.Make(map[string]any{
  1617  					"name":  "abc",
  1618  					"name1": [3]string{"a", "b", "c"},
  1619  					"name2": []string{"a", "b", "c"},
  1620  					"name3": map[string]string{
  1621  						"a": "a1",
  1622  						"b": "b1",
  1623  						"c": "c1",
  1624  					},
  1625  				}, map[string]string{
  1626  					"name":  "required|max_len:4",
  1627  					"name1": "required|max_len:4",
  1628  					"name2": "required|max_len:4",
  1629  					"name3": "required|max_len:4",
  1630  				})
  1631  				assert.Nil(t, err, c.description)
  1632  				assert.NotNil(t, validator, c.description)
  1633  				assert.False(t, validator.Fails(), c.description)
  1634  			},
  1635  		},
  1636  		{
  1637  			description: "error",
  1638  			setup: func(c Case) {
  1639  				validator, err := validation.Make(map[string]any{
  1640  					"name":  "abc",
  1641  					"name1": [3]string{"a", "b", "c"},
  1642  					"name2": []string{"a", "b", "c"},
  1643  					"name3": map[string]string{
  1644  						"a": "a1",
  1645  						"b": "b1",
  1646  						"c": "c1",
  1647  					},
  1648  				}, map[string]string{
  1649  					"name":  "required|max_len:2",
  1650  					"name1": "required|max_len:2",
  1651  					"name2": "required|max_len:2",
  1652  					"name3": "required|max_len:2",
  1653  				})
  1654  				assert.Nil(t, err, c.description)
  1655  				assert.NotNil(t, validator, c.description)
  1656  				assert.Equal(t, map[string]string{"max_len": "name max length is 2"}, validator.Errors().Get("name"))
  1657  				assert.Equal(t, map[string]string{"max_len": "name1 max length is 2"}, validator.Errors().Get("name1"))
  1658  				assert.Equal(t, map[string]string{"max_len": "name2 max length is 2"}, validator.Errors().Get("name2"))
  1659  				assert.Equal(t, map[string]string{"max_len": "name3 max length is 2"}, validator.Errors().Get("name3"))
  1660  			},
  1661  		},
  1662  	}
  1663  
  1664  	for _, test := range tests {
  1665  		t.Run(test.description, func(t *testing.T) {
  1666  			test.setup(test)
  1667  		})
  1668  	}
  1669  }
  1670  
  1671  func TestRule_Email(t *testing.T) {
  1672  	validation := NewValidation()
  1673  	tests := []Case{
  1674  		{
  1675  			description: "success",
  1676  			setup: func(c Case) {
  1677  				validator, err := validation.Make(map[string]any{
  1678  					"name": "hello@goravel.com",
  1679  				}, map[string]string{
  1680  					"name": "required|email",
  1681  				})
  1682  				assert.Nil(t, err, c.description)
  1683  				assert.NotNil(t, validator, c.description)
  1684  				assert.False(t, validator.Fails(), c.description)
  1685  			},
  1686  		},
  1687  		{
  1688  			description: "error",
  1689  			setup: func(c Case) {
  1690  				validator, err := validation.Make(map[string]any{
  1691  					"name": "abc",
  1692  				}, map[string]string{
  1693  					"name": "required|email",
  1694  				})
  1695  				assert.Nil(t, err, c.description)
  1696  				assert.NotNil(t, validator, c.description)
  1697  				assert.Equal(t, map[string]string{"email": "name value is an invalid email address"}, validator.Errors().Get("name"))
  1698  			},
  1699  		},
  1700  	}
  1701  
  1702  	for _, test := range tests {
  1703  		t.Run(test.description, func(t *testing.T) {
  1704  			test.setup(test)
  1705  		})
  1706  	}
  1707  }
  1708  
  1709  func TestRule_Array(t *testing.T) {
  1710  	validation := NewValidation()
  1711  	tests := []Case{
  1712  		{
  1713  			description: "success",
  1714  			setup: func(c Case) {
  1715  				validator, err := validation.Make(map[string]any{
  1716  					"name":  [2]string{"a", "b"},
  1717  					"name1": []string{"a", "b"},
  1718  				}, map[string]string{
  1719  					"name":  "required|array",
  1720  					"name1": "required|array",
  1721  				})
  1722  				assert.Nil(t, err, c.description)
  1723  				assert.NotNil(t, validator, c.description)
  1724  				assert.False(t, validator.Fails(), c.description)
  1725  			},
  1726  		},
  1727  		{
  1728  			description: "error",
  1729  			setup: func(c Case) {
  1730  				validator, err := validation.Make(map[string]any{
  1731  					"name":  "a",
  1732  					"name1": 1,
  1733  					"name2": true,
  1734  				}, map[string]string{
  1735  					"name":  "required|array",
  1736  					"name1": "required|array",
  1737  					"name2": "required|array",
  1738  				})
  1739  				assert.Nil(t, err, c.description)
  1740  				assert.NotNil(t, validator, c.description)
  1741  				assert.Equal(t, map[string]string{"array": "name value must be an array"}, validator.Errors().Get("name"))
  1742  				assert.Equal(t, map[string]string{"array": "name1 value must be an array"}, validator.Errors().Get("name1"))
  1743  				assert.Equal(t, map[string]string{"array": "name2 value must be an array"}, validator.Errors().Get("name2"))
  1744  			},
  1745  		},
  1746  	}
  1747  
  1748  	for _, test := range tests {
  1749  		t.Run(test.description, func(t *testing.T) {
  1750  			test.setup(test)
  1751  		})
  1752  	}
  1753  }
  1754  
  1755  func TestRule_Map(t *testing.T) {
  1756  	validation := NewValidation()
  1757  	tests := []Case{
  1758  		{
  1759  			description: "success",
  1760  			setup: func(c Case) {
  1761  				validator, err := validation.Make(map[string]any{
  1762  					"name": map[string]string{"a": "a1"},
  1763  				}, map[string]string{
  1764  					"name": "required|map",
  1765  				})
  1766  				assert.Nil(t, err, c.description)
  1767  				assert.NotNil(t, validator, c.description)
  1768  				assert.False(t, validator.Fails(), c.description)
  1769  			},
  1770  		},
  1771  		{
  1772  			description: "error",
  1773  			setup: func(c Case) {
  1774  				validator, err := validation.Make(map[string]any{
  1775  					"name":  "a",
  1776  					"name1": 1,
  1777  					"name2": true,
  1778  					"name3": []string{"a"},
  1779  				}, map[string]string{
  1780  					"name":  "required|map",
  1781  					"name1": "required|map",
  1782  					"name2": "required|map",
  1783  					"name3": "required|map",
  1784  				})
  1785  				assert.Nil(t, err, c.description)
  1786  				assert.NotNil(t, validator, c.description)
  1787  				assert.Equal(t, map[string]string{"map": "name value must be a map"}, validator.Errors().Get("name"))
  1788  				assert.Equal(t, map[string]string{"map": "name1 value must be a map"}, validator.Errors().Get("name1"))
  1789  				assert.Equal(t, map[string]string{"map": "name2 value must be a map"}, validator.Errors().Get("name2"))
  1790  				assert.Equal(t, map[string]string{"map": "name3 value must be a map"}, validator.Errors().Get("name3"))
  1791  			},
  1792  		},
  1793  	}
  1794  
  1795  	for _, test := range tests {
  1796  		t.Run(test.description, func(t *testing.T) {
  1797  			test.setup(test)
  1798  		})
  1799  	}
  1800  }
  1801  
  1802  func TestRule_EqField(t *testing.T) {
  1803  	validation := NewValidation()
  1804  	tests := []Case{
  1805  		{
  1806  			description: "success",
  1807  			setup: func(c Case) {
  1808  				validator, err := validation.Make(map[string]any{
  1809  					"name":  "a",
  1810  					"name1": "a",
  1811  				}, map[string]string{
  1812  					"name":  "required",
  1813  					"name1": "required|eq_field:name",
  1814  				})
  1815  				assert.Nil(t, err, c.description)
  1816  				assert.NotNil(t, validator, c.description)
  1817  				assert.False(t, validator.Fails(), c.description)
  1818  			},
  1819  		},
  1820  		{
  1821  			description: "error",
  1822  			setup: func(c Case) {
  1823  				validator, err := validation.Make(map[string]any{
  1824  					"name":  "a",
  1825  					"name1": "b",
  1826  				}, map[string]string{
  1827  					"name":  "required",
  1828  					"name1": "required|eq_field:name",
  1829  				})
  1830  				assert.Nil(t, err, c.description)
  1831  				assert.NotNil(t, validator, c.description)
  1832  				assert.Equal(t, map[string]string{"eq_field": "name1 value must be equal the field name"}, validator.Errors().Get("name1"))
  1833  			},
  1834  		},
  1835  	}
  1836  
  1837  	for _, test := range tests {
  1838  		t.Run(test.description, func(t *testing.T) {
  1839  			test.setup(test)
  1840  		})
  1841  	}
  1842  }
  1843  
  1844  func TestRule_NeField(t *testing.T) {
  1845  	validation := NewValidation()
  1846  	tests := []Case{
  1847  		{
  1848  			description: "success",
  1849  			setup: func(c Case) {
  1850  				validator, err := validation.Make(map[string]any{
  1851  					"name":  "a",
  1852  					"name1": "b",
  1853  				}, map[string]string{
  1854  					"name":  "required",
  1855  					"name1": "required|ne_field:name",
  1856  				})
  1857  				assert.Nil(t, err, c.description)
  1858  				assert.NotNil(t, validator, c.description)
  1859  				assert.False(t, validator.Fails(), c.description)
  1860  			},
  1861  		},
  1862  		{
  1863  			description: "error",
  1864  			setup: func(c Case) {
  1865  				validator, err := validation.Make(map[string]any{
  1866  					"name":  "a",
  1867  					"name1": "a",
  1868  				}, map[string]string{
  1869  					"name":  "required",
  1870  					"name1": "required|ne_field:name",
  1871  				})
  1872  				assert.Nil(t, err, c.description)
  1873  				assert.NotNil(t, validator, c.description)
  1874  				assert.Equal(t, map[string]string{"ne_field": "name1 value cannot be equal to the field name"}, validator.Errors().Get("name1"))
  1875  			},
  1876  		},
  1877  	}
  1878  
  1879  	for _, test := range tests {
  1880  		t.Run(test.description, func(t *testing.T) {
  1881  			test.setup(test)
  1882  		})
  1883  	}
  1884  }
  1885  
  1886  func TestRule_GtField(t *testing.T) {
  1887  	validation := NewValidation()
  1888  	tests := []Case{
  1889  		{
  1890  			description: "success",
  1891  			setup: func(c Case) {
  1892  				validator, err := validation.Make(map[string]any{
  1893  					"name":  1,
  1894  					"name1": 2,
  1895  				}, map[string]string{
  1896  					"name":  "required",
  1897  					"name1": "required|gt_field:name",
  1898  				})
  1899  				assert.Nil(t, err, c.description)
  1900  				assert.NotNil(t, validator, c.description)
  1901  				assert.False(t, validator.Fails(), c.description)
  1902  			},
  1903  		},
  1904  		{
  1905  			description: "error",
  1906  			setup: func(c Case) {
  1907  				validator, err := validation.Make(map[string]any{
  1908  					"name":  2,
  1909  					"name1": 1,
  1910  				}, map[string]string{
  1911  					"name":  "required",
  1912  					"name1": "required|gt_field:name",
  1913  				})
  1914  				assert.Nil(t, err, c.description)
  1915  				assert.NotNil(t, validator, c.description)
  1916  				assert.Equal(t, map[string]string{"gt_field": "name1 value must be greater than the field name"}, validator.Errors().Get("name1"))
  1917  			},
  1918  		},
  1919  	}
  1920  
  1921  	for _, test := range tests {
  1922  		t.Run(test.description, func(t *testing.T) {
  1923  			test.setup(test)
  1924  		})
  1925  	}
  1926  }
  1927  
  1928  func TestRule_GteField(t *testing.T) {
  1929  	validation := NewValidation()
  1930  	tests := []Case{
  1931  		{
  1932  			description: "success",
  1933  			setup: func(c Case) {
  1934  				validator, err := validation.Make(map[string]any{
  1935  					"name":  1,
  1936  					"name1": 2,
  1937  					"name2": 1,
  1938  				}, map[string]string{
  1939  					"name":  "required",
  1940  					"name1": "required|gte_field:name",
  1941  					"name2": "required|gte_field:name",
  1942  				})
  1943  				assert.Nil(t, err, c.description)
  1944  				assert.NotNil(t, validator, c.description)
  1945  				assert.False(t, validator.Fails(), c.description)
  1946  			},
  1947  		},
  1948  		{
  1949  			description: "error",
  1950  			setup: func(c Case) {
  1951  				validator, err := validation.Make(map[string]any{
  1952  					"name":  2,
  1953  					"name1": 1,
  1954  				}, map[string]string{
  1955  					"name":  "required",
  1956  					"name1": "required|gte_field:name",
  1957  				})
  1958  				assert.Nil(t, err, c.description)
  1959  				assert.NotNil(t, validator, c.description)
  1960  				assert.Equal(t, map[string]string{"gte_field": "name1 value should be greater or equal to the field name"}, validator.Errors().Get("name1"))
  1961  			},
  1962  		},
  1963  	}
  1964  
  1965  	for _, test := range tests {
  1966  		t.Run(test.description, func(t *testing.T) {
  1967  			test.setup(test)
  1968  		})
  1969  	}
  1970  }
  1971  
  1972  func TestRule_LtField(t *testing.T) {
  1973  	validation := NewValidation()
  1974  	tests := []Case{
  1975  		{
  1976  			description: "success",
  1977  			setup: func(c Case) {
  1978  				validator, err := validation.Make(map[string]any{
  1979  					"name":  2,
  1980  					"name1": 1,
  1981  				}, map[string]string{
  1982  					"name":  "required",
  1983  					"name1": "required|lt_field:name",
  1984  				})
  1985  				assert.Nil(t, err, c.description)
  1986  				assert.NotNil(t, validator, c.description)
  1987  				assert.False(t, validator.Fails(), c.description)
  1988  			},
  1989  		},
  1990  		{
  1991  			description: "error",
  1992  			setup: func(c Case) {
  1993  				validator, err := validation.Make(map[string]any{
  1994  					"name":  1,
  1995  					"name1": 2,
  1996  				}, map[string]string{
  1997  					"name":  "required",
  1998  					"name1": "required|lt_field:name",
  1999  				})
  2000  				assert.Nil(t, err, c.description)
  2001  				assert.NotNil(t, validator, c.description)
  2002  				assert.Equal(t, map[string]string{"lt_field": "name1 value should be less than the field name"}, validator.Errors().Get("name1"))
  2003  			},
  2004  		},
  2005  	}
  2006  
  2007  	for _, test := range tests {
  2008  		t.Run(test.description, func(t *testing.T) {
  2009  			test.setup(test)
  2010  		})
  2011  	}
  2012  }
  2013  
  2014  func TestRule_LteField(t *testing.T) {
  2015  	validation := NewValidation()
  2016  	tests := []Case{
  2017  		{
  2018  			description: "success",
  2019  			setup: func(c Case) {
  2020  				validator, err := validation.Make(map[string]any{
  2021  					"name":  2,
  2022  					"name1": 2,
  2023  					"name2": 1,
  2024  				}, map[string]string{
  2025  					"name":  "required",
  2026  					"name1": "required|lte_field:name",
  2027  					"name2": "required|lte_field:name",
  2028  				})
  2029  				assert.Nil(t, err, c.description)
  2030  				assert.NotNil(t, validator, c.description)
  2031  				assert.False(t, validator.Fails(), c.description)
  2032  			},
  2033  		},
  2034  		{
  2035  			description: "error",
  2036  			setup: func(c Case) {
  2037  				validator, err := validation.Make(map[string]any{
  2038  					"name":  1,
  2039  					"name1": 2,
  2040  				}, map[string]string{
  2041  					"name":  "required",
  2042  					"name1": "required|lte_field:name",
  2043  				})
  2044  				assert.Nil(t, err, c.description)
  2045  				assert.NotNil(t, validator, c.description)
  2046  				assert.Equal(t, map[string]string{"lte_field": "name1 value should be less than or equal to the field name"}, validator.Errors().Get("name1"))
  2047  			},
  2048  		},
  2049  	}
  2050  
  2051  	for _, test := range tests {
  2052  		t.Run(test.description, func(t *testing.T) {
  2053  			test.setup(test)
  2054  		})
  2055  	}
  2056  }
  2057  
  2058  func TestRule_Date(t *testing.T) {
  2059  	validation := NewValidation()
  2060  	tests := []Case{
  2061  		{
  2062  			description: "success",
  2063  			setup: func(c Case) {
  2064  				validator, err := validation.Make(map[string]any{
  2065  					"name":  "2022-12-25",
  2066  					"name1": "2022/12/25",
  2067  					"name2": "",
  2068  				}, map[string]string{
  2069  					"name":  "required|date",
  2070  					"name1": "required|date",
  2071  					"name2": "date",
  2072  					"name3": "date",
  2073  				})
  2074  				assert.Nil(t, err, c.description)
  2075  				assert.NotNil(t, validator, c.description)
  2076  				assert.False(t, validator.Fails(), c.description)
  2077  			},
  2078  		},
  2079  		{
  2080  			description: "error",
  2081  			setup: func(c Case) {
  2082  				validator, err := validation.Make(map[string]any{
  2083  					"name":  "2022.12.25",
  2084  					"name1": "a",
  2085  				}, map[string]string{
  2086  					"name":  "required|date",
  2087  					"name1": "required|date",
  2088  				})
  2089  				assert.Nil(t, err, c.description)
  2090  				assert.NotNil(t, validator, c.description)
  2091  				assert.Equal(t, map[string]string{"date": "name value should be a date string"}, validator.Errors().Get("name"))
  2092  				assert.Equal(t, map[string]string{"date": "name1 value should be a date string"}, validator.Errors().Get("name1"))
  2093  			},
  2094  		},
  2095  	}
  2096  
  2097  	for _, test := range tests {
  2098  		t.Run(test.description, func(t *testing.T) {
  2099  			test.setup(test)
  2100  		})
  2101  	}
  2102  }
  2103  
  2104  func TestRule_GtDate(t *testing.T) {
  2105  	validation := NewValidation()
  2106  	tests := []Case{
  2107  		{
  2108  			description: "success",
  2109  			setup: func(c Case) {
  2110  				validator, err := validation.Make(map[string]any{
  2111  					"name": "2022-12-25",
  2112  				}, map[string]string{
  2113  					"name": "required|gt_date:2022-12-24",
  2114  				})
  2115  				assert.Nil(t, err, c.description)
  2116  				assert.NotNil(t, validator, c.description)
  2117  				assert.False(t, validator.Fails(), c.description)
  2118  			},
  2119  		},
  2120  		{
  2121  			description: "error",
  2122  			setup: func(c Case) {
  2123  				validator, err := validation.Make(map[string]any{
  2124  					"name": "2022-12-25",
  2125  				}, map[string]string{
  2126  					"name": "required|gt_date:2022-12-26",
  2127  				})
  2128  				assert.Nil(t, err, c.description)
  2129  				assert.NotNil(t, validator, c.description)
  2130  				assert.Equal(t, map[string]string{"gt_date": "name field did not pass validation"}, validator.Errors().Get("name"))
  2131  			},
  2132  		},
  2133  	}
  2134  
  2135  	for _, test := range tests {
  2136  		t.Run(test.description, func(t *testing.T) {
  2137  			test.setup(test)
  2138  		})
  2139  	}
  2140  }
  2141  
  2142  func TestRule_LtDate(t *testing.T) {
  2143  	validation := NewValidation()
  2144  	tests := []Case{
  2145  		{
  2146  			description: "success",
  2147  			setup: func(c Case) {
  2148  				validator, err := validation.Make(map[string]any{
  2149  					"name": "2022-12-25",
  2150  				}, map[string]string{
  2151  					"name": "required|lt_date:2022-12-26",
  2152  				})
  2153  				assert.Nil(t, err, c.description)
  2154  				assert.NotNil(t, validator, c.description)
  2155  				assert.False(t, validator.Fails(), c.description)
  2156  			},
  2157  		},
  2158  		{
  2159  			description: "error",
  2160  			setup: func(c Case) {
  2161  				validator, err := validation.Make(map[string]any{
  2162  					"name": "2022-12-25",
  2163  				}, map[string]string{
  2164  					"name": "required|lt_date:2022-12-24",
  2165  				})
  2166  				assert.Nil(t, err, c.description)
  2167  				assert.NotNil(t, validator, c.description)
  2168  				assert.Equal(t, map[string]string{"lt_date": "name field did not pass validation"}, validator.Errors().Get("name"))
  2169  			},
  2170  		},
  2171  	}
  2172  
  2173  	for _, test := range tests {
  2174  		t.Run(test.description, func(t *testing.T) {
  2175  			test.setup(test)
  2176  		})
  2177  	}
  2178  }
  2179  
  2180  func TestRule_GteDate(t *testing.T) {
  2181  	validation := NewValidation()
  2182  	tests := []Case{
  2183  		{
  2184  			description: "success",
  2185  			setup: func(c Case) {
  2186  				validator, err := validation.Make(map[string]any{
  2187  					"name":  "2022-12-25",
  2188  					"name1": "2022-12-25",
  2189  				}, map[string]string{
  2190  					"name":  "required|gte_date:2022-12-25",
  2191  					"name1": "required|gte_date:2022-12-24",
  2192  				})
  2193  				assert.Nil(t, err, c.description)
  2194  				assert.NotNil(t, validator, c.description)
  2195  				assert.False(t, validator.Fails(), c.description)
  2196  			},
  2197  		},
  2198  		{
  2199  			description: "error",
  2200  			setup: func(c Case) {
  2201  				validator, err := validation.Make(map[string]any{
  2202  					"name": "2022-12-25",
  2203  				}, map[string]string{
  2204  					"name": "required|gte_date:2022-12-26",
  2205  				})
  2206  				assert.Nil(t, err, c.description)
  2207  				assert.NotNil(t, validator, c.description)
  2208  				assert.Equal(t, map[string]string{"gte_date": "name field did not pass validation"}, validator.Errors().Get("name"))
  2209  			},
  2210  		},
  2211  	}
  2212  
  2213  	for _, test := range tests {
  2214  		t.Run(test.description, func(t *testing.T) {
  2215  			test.setup(test)
  2216  		})
  2217  	}
  2218  }
  2219  
  2220  func TestRule_lteDate(t *testing.T) {
  2221  	validation := NewValidation()
  2222  	tests := []Case{
  2223  		{
  2224  			description: "success",
  2225  			setup: func(c Case) {
  2226  				validator, err := validation.Make(map[string]any{
  2227  					"name":  "2022-12-25",
  2228  					"name1": "2022-12-25",
  2229  				}, map[string]string{
  2230  					"name":  "required|lte_date:2022-12-25",
  2231  					"name1": "required|lte_date:2022-12-26",
  2232  				})
  2233  				assert.Nil(t, err, c.description)
  2234  				assert.NotNil(t, validator, c.description)
  2235  				assert.False(t, validator.Fails(), c.description)
  2236  			},
  2237  		},
  2238  		{
  2239  			description: "error",
  2240  			setup: func(c Case) {
  2241  				validator, err := validation.Make(map[string]any{
  2242  					"name": "2022-12-25",
  2243  				}, map[string]string{
  2244  					"name": "required|lte_date:2022-12-24",
  2245  				})
  2246  				assert.Nil(t, err, c.description)
  2247  				assert.NotNil(t, validator, c.description)
  2248  				assert.Equal(t, map[string]string{"lte_date": "name field did not pass validation"}, validator.Errors().Get("name"))
  2249  			},
  2250  		},
  2251  	}
  2252  
  2253  	for _, test := range tests {
  2254  		t.Run(test.description, func(t *testing.T) {
  2255  			test.setup(test)
  2256  		})
  2257  	}
  2258  }
  2259  
  2260  func TestRule_Alpha(t *testing.T) {
  2261  	validation := NewValidation()
  2262  	tests := []Case{
  2263  		{
  2264  			description: "success",
  2265  			setup: func(c Case) {
  2266  				validator, err := validation.Make(map[string]any{
  2267  					"name": "abcABC",
  2268  				}, map[string]string{
  2269  					"name": "required|alpha",
  2270  				})
  2271  				assert.Nil(t, err, c.description)
  2272  				assert.NotNil(t, validator, c.description)
  2273  				assert.False(t, validator.Fails(), c.description)
  2274  			},
  2275  		},
  2276  		{
  2277  			description: "error",
  2278  			setup: func(c Case) {
  2279  				validator, err := validation.Make(map[string]any{
  2280  					"name":  "abcABC123",
  2281  					"name1": "abc.",
  2282  				}, map[string]string{
  2283  					"name":  "required|alpha",
  2284  					"name1": "required|alpha",
  2285  				})
  2286  				assert.Nil(t, err, c.description)
  2287  				assert.NotNil(t, validator, c.description)
  2288  				assert.Equal(t, map[string]string{"alpha": "name value contains only alpha char"}, validator.Errors().Get("name"))
  2289  				assert.Equal(t, map[string]string{"alpha": "name1 value contains only alpha char"}, validator.Errors().Get("name1"))
  2290  			},
  2291  		},
  2292  	}
  2293  
  2294  	for _, test := range tests {
  2295  		t.Run(test.description, func(t *testing.T) {
  2296  			test.setup(test)
  2297  		})
  2298  	}
  2299  }
  2300  
  2301  func TestRule_AlphaNum(t *testing.T) {
  2302  	validation := NewValidation()
  2303  	tests := []Case{
  2304  		{
  2305  			description: "success",
  2306  			setup: func(c Case) {
  2307  				validator, err := validation.Make(map[string]any{
  2308  					"name": "abcABC123",
  2309  				}, map[string]string{
  2310  					"name": "required|alpha_num",
  2311  				})
  2312  				assert.Nil(t, err, c.description)
  2313  				assert.NotNil(t, validator, c.description)
  2314  				assert.False(t, validator.Fails(), c.description)
  2315  			},
  2316  		},
  2317  		{
  2318  			description: "error",
  2319  			setup: func(c Case) {
  2320  				validator, err := validation.Make(map[string]any{
  2321  					"name": "abcABC123.",
  2322  				}, map[string]string{
  2323  					"name": "required|alpha_num",
  2324  				})
  2325  				assert.Nil(t, err, c.description)
  2326  				assert.NotNil(t, validator, c.description)
  2327  				assert.Equal(t, map[string]string{"alpha_num": "name field did not pass validation"}, validator.Errors().Get("name"))
  2328  			},
  2329  		},
  2330  	}
  2331  
  2332  	for _, test := range tests {
  2333  		t.Run(test.description, func(t *testing.T) {
  2334  			test.setup(test)
  2335  		})
  2336  	}
  2337  }
  2338  
  2339  func TestRule_AlphaDash(t *testing.T) {
  2340  	validation := NewValidation()
  2341  	tests := []Case{
  2342  		{
  2343  			description: "success",
  2344  			setup: func(c Case) {
  2345  				validator, err := validation.Make(map[string]any{
  2346  					"name": "abcABC123-_",
  2347  				}, map[string]string{
  2348  					"name": "required|alpha_dash",
  2349  				})
  2350  				assert.Nil(t, err, c.description)
  2351  				assert.NotNil(t, validator, c.description)
  2352  				assert.False(t, validator.Fails(), c.description)
  2353  			},
  2354  		},
  2355  		{
  2356  			description: "error",
  2357  			setup: func(c Case) {
  2358  				validator, err := validation.Make(map[string]any{
  2359  					"name": "abcABC123-_.",
  2360  				}, map[string]string{
  2361  					"name": "required|alpha_dash",
  2362  				})
  2363  				assert.Nil(t, err, c.description)
  2364  				assert.NotNil(t, validator, c.description)
  2365  				assert.Equal(t, map[string]string{"alpha_dash": "name field did not pass validation"}, validator.Errors().Get("name"))
  2366  			},
  2367  		},
  2368  	}
  2369  
  2370  	for _, test := range tests {
  2371  		t.Run(test.description, func(t *testing.T) {
  2372  			test.setup(test)
  2373  		})
  2374  	}
  2375  }
  2376  
  2377  func TestRule_Json(t *testing.T) {
  2378  	validation := NewValidation()
  2379  	tests := []Case{
  2380  		{
  2381  			description: "success",
  2382  			setup: func(c Case) {
  2383  				validator, err := validation.Make(map[string]any{
  2384  					"name": "{\"a\":1}",
  2385  				}, map[string]string{
  2386  					"name": "required|json",
  2387  				})
  2388  				assert.Nil(t, err, c.description)
  2389  				assert.NotNil(t, validator, c.description)
  2390  				assert.False(t, validator.Fails(), c.description)
  2391  			},
  2392  		},
  2393  		{
  2394  			description: "error",
  2395  			setup: func(c Case) {
  2396  				validator, err := validation.Make(map[string]any{
  2397  					"name": "a",
  2398  				}, map[string]string{
  2399  					"name": "required|json",
  2400  				})
  2401  				assert.Nil(t, err, c.description)
  2402  				assert.NotNil(t, validator, c.description)
  2403  				assert.Equal(t, map[string]string{"json": "name value should be a json string"}, validator.Errors().Get("name"))
  2404  			},
  2405  		},
  2406  	}
  2407  
  2408  	for _, test := range tests {
  2409  		t.Run(test.description, func(t *testing.T) {
  2410  			test.setup(test)
  2411  		})
  2412  	}
  2413  }
  2414  
  2415  func TestRule_Number(t *testing.T) {
  2416  	validation := NewValidation()
  2417  	tests := []Case{
  2418  		{
  2419  			description: "success",
  2420  			setup: func(c Case) {
  2421  				validator, err := validation.Make(map[string]any{
  2422  					"name": 1,
  2423  				}, map[string]string{
  2424  					"name": "required|number",
  2425  				})
  2426  				assert.Nil(t, err, c.description)
  2427  				assert.NotNil(t, validator, c.description)
  2428  				assert.False(t, validator.Fails(), c.description)
  2429  			},
  2430  		},
  2431  		{
  2432  			description: "error",
  2433  			setup: func(c Case) {
  2434  				validator, err := validation.Make(map[string]any{
  2435  					"name": "a",
  2436  				}, map[string]string{
  2437  					"name": "required|number",
  2438  				})
  2439  				assert.Nil(t, err, c.description)
  2440  				assert.NotNil(t, validator, c.description)
  2441  				assert.Equal(t, map[string]string{"number": "name field did not pass validation"}, validator.Errors().Get("name"))
  2442  			},
  2443  		},
  2444  	}
  2445  
  2446  	for _, test := range tests {
  2447  		t.Run(test.description, func(t *testing.T) {
  2448  			test.setup(test)
  2449  		})
  2450  	}
  2451  }
  2452  
  2453  func TestRule_FullUrl(t *testing.T) {
  2454  	validation := NewValidation()
  2455  	tests := []Case{
  2456  		{
  2457  			description: "success",
  2458  			setup: func(c Case) {
  2459  				validator, err := validation.Make(map[string]any{
  2460  					"name":  "https://www.goravel.dev",
  2461  					"name1": "http://www.goravel.dev",
  2462  				}, map[string]string{
  2463  					"name":  "required|full_url",
  2464  					"name1": "required|full_url",
  2465  				})
  2466  				assert.Nil(t, err, c.description)
  2467  				assert.NotNil(t, validator, c.description)
  2468  				assert.False(t, validator.Fails(), c.description)
  2469  			},
  2470  		},
  2471  		{
  2472  			description: "error",
  2473  			setup: func(c Case) {
  2474  				validator, err := validation.Make(map[string]any{
  2475  					"name": "a",
  2476  				}, map[string]string{
  2477  					"name": "required|full_url",
  2478  				})
  2479  				assert.Nil(t, err, c.description)
  2480  				assert.NotNil(t, validator, c.description)
  2481  				assert.Equal(t, map[string]string{"full_url": "name must be a valid full URL address"}, validator.Errors().Get("name"))
  2482  			},
  2483  		},
  2484  	}
  2485  
  2486  	for _, test := range tests {
  2487  		t.Run(test.description, func(t *testing.T) {
  2488  			test.setup(test)
  2489  		})
  2490  	}
  2491  }
  2492  
  2493  func TestRule_Ip(t *testing.T) {
  2494  	validation := NewValidation()
  2495  	tests := []Case{
  2496  		{
  2497  			description: "success",
  2498  			setup: func(c Case) {
  2499  				validator, err := validation.Make(map[string]any{
  2500  					"name":  "192.168.1.1",
  2501  					"name1": "FE80:CD00:0000:0CDE:1257:0000:211E:729C",
  2502  				}, map[string]string{
  2503  					"name":  "required|ip",
  2504  					"name1": "required|ip",
  2505  				})
  2506  				assert.Nil(t, err, c.description)
  2507  				assert.NotNil(t, validator, c.description)
  2508  				assert.False(t, validator.Fails(), c.description)
  2509  			},
  2510  		},
  2511  		{
  2512  			description: "error",
  2513  			setup: func(c Case) {
  2514  				validator, err := validation.Make(map[string]any{
  2515  					"name":  "a",
  2516  					"name1": "192.168.1.300",
  2517  				}, map[string]string{
  2518  					"name":  "required|ip",
  2519  					"name1": "required|ip",
  2520  				})
  2521  				assert.Nil(t, err, c.description)
  2522  				assert.NotNil(t, validator, c.description)
  2523  				assert.Equal(t, map[string]string{"ip": "name value should be an IP (v4 or v6) string"}, validator.Errors().Get("name"))
  2524  				assert.Equal(t, map[string]string{"ip": "name1 value should be an IP (v4 or v6) string"}, validator.Errors().Get("name1"))
  2525  			},
  2526  		},
  2527  	}
  2528  
  2529  	for _, test := range tests {
  2530  		t.Run(test.description, func(t *testing.T) {
  2531  			test.setup(test)
  2532  		})
  2533  	}
  2534  }
  2535  
  2536  func TestRule_Ipv4(t *testing.T) {
  2537  	validation := NewValidation()
  2538  	tests := []Case{
  2539  		{
  2540  			description: "success",
  2541  			setup: func(c Case) {
  2542  				validator, err := validation.Make(map[string]any{
  2543  					"name": "192.168.1.1",
  2544  				}, map[string]string{
  2545  					"name": "required|ipv4",
  2546  				})
  2547  				assert.Nil(t, err, c.description)
  2548  				assert.NotNil(t, validator, c.description)
  2549  				assert.False(t, validator.Fails(), c.description)
  2550  			},
  2551  		},
  2552  		{
  2553  			description: "error",
  2554  			setup: func(c Case) {
  2555  				validator, err := validation.Make(map[string]any{
  2556  					"name":  "a",
  2557  					"name1": "FE80:CD00:0000:0CDE:1257:0000:211E:729C",
  2558  					"name2": "192.168.1.300",
  2559  				}, map[string]string{
  2560  					"name":  "required|ipv4",
  2561  					"name1": "required|ipv4",
  2562  					"name2": "required|ipv4",
  2563  				})
  2564  				assert.Nil(t, err, c.description)
  2565  				assert.NotNil(t, validator, c.description)
  2566  				assert.Equal(t, map[string]string{"ipv4": "name value should be an IPv4 string"}, validator.Errors().Get("name"))
  2567  				assert.Equal(t, map[string]string{"ipv4": "name1 value should be an IPv4 string"}, validator.Errors().Get("name1"))
  2568  				assert.Equal(t, map[string]string{"ipv4": "name2 value should be an IPv4 string"}, validator.Errors().Get("name2"))
  2569  			},
  2570  		},
  2571  	}
  2572  
  2573  	for _, test := range tests {
  2574  		t.Run(test.description, func(t *testing.T) {
  2575  			test.setup(test)
  2576  		})
  2577  	}
  2578  }
  2579  
  2580  func TestRule_Ipv6(t *testing.T) {
  2581  	validation := NewValidation()
  2582  	tests := []Case{
  2583  		{
  2584  			description: "success",
  2585  			setup: func(c Case) {
  2586  				validator, err := validation.Make(map[string]any{
  2587  					"name": "FE80:CD00:0000:0CDE:1257:0000:211E:729C",
  2588  				}, map[string]string{
  2589  					"name": "required|ipv6",
  2590  				})
  2591  				assert.Nil(t, err, c.description)
  2592  				assert.NotNil(t, validator, c.description)
  2593  				assert.False(t, validator.Fails(), c.description)
  2594  			},
  2595  		},
  2596  		{
  2597  			description: "error",
  2598  			setup: func(c Case) {
  2599  				validator, err := validation.Make(map[string]any{
  2600  					"name":  "a",
  2601  					"name1": "192.168.1.300",
  2602  				}, map[string]string{
  2603  					"name":  "required|ipv6",
  2604  					"name1": "required|ipv6",
  2605  				})
  2606  				assert.Nil(t, err, c.description)
  2607  				assert.NotNil(t, validator, c.description)
  2608  				assert.Equal(t, map[string]string{"ipv6": "name value should be an IPv6 string"}, validator.Errors().Get("name"))
  2609  				assert.Equal(t, map[string]string{"ipv6": "name1 value should be an IPv6 string"}, validator.Errors().Get("name1"))
  2610  			},
  2611  		},
  2612  	}
  2613  
  2614  	for _, test := range tests {
  2615  		t.Run(test.description, func(t *testing.T) {
  2616  			test.setup(test)
  2617  		})
  2618  	}
  2619  }
  2620  
  2621  func TestAddRule(t *testing.T) {
  2622  	validation := NewValidation()
  2623  	err := validation.AddRules([]httpvalidate.Rule{&Uppercase{}})
  2624  	assert.Nil(t, err)
  2625  
  2626  	err = validation.AddRules([]httpvalidate.Rule{&Duplicate{}})
  2627  	assert.EqualError(t, err, "duplicate rule name: required")
  2628  }
  2629  
  2630  func TestCustomRule(t *testing.T) {
  2631  	validation := NewValidation()
  2632  	err := validation.AddRules([]httpvalidate.Rule{&Uppercase{}, &Lowercase{}})
  2633  	assert.Nil(t, err)
  2634  
  2635  	tests := []Case{
  2636  		{
  2637  			description: "success",
  2638  			setup: func(c Case) {
  2639  				validator, err := validation.Make(map[string]any{
  2640  					"name":    "ABC",
  2641  					"address": "de",
  2642  				}, map[string]string{
  2643  					"name":    "required|uppercase:3",
  2644  					"address": "required|lowercase:2",
  2645  				})
  2646  				assert.Nil(t, err, c.description)
  2647  				assert.NotNil(t, validator, c.description)
  2648  				assert.False(t, validator.Fails(), c.description)
  2649  			},
  2650  		},
  2651  		{
  2652  			description: "error",
  2653  			setup: func(c Case) {
  2654  				validator, err := validation.Make(map[string]any{
  2655  					"name":    "abc",
  2656  					"address": "DE",
  2657  				}, map[string]string{
  2658  					"name":    "required|uppercase:3",
  2659  					"address": "required|lowercase:2",
  2660  				})
  2661  				assert.Nil(t, err, c.description)
  2662  				assert.NotNil(t, validator, c.description)
  2663  				assert.Equal(t, map[string]string{"uppercase": "name must be upper"}, validator.Errors().Get("name"))
  2664  				assert.Equal(t, map[string]string{"lowercase": "address must be lower"}, validator.Errors().Get("address"))
  2665  			},
  2666  		},
  2667  	}
  2668  
  2669  	for _, test := range tests {
  2670  		t.Run(test.description, func(t *testing.T) {
  2671  			test.setup(test)
  2672  		})
  2673  	}
  2674  }
  2675  
  2676  type Uppercase struct {
  2677  }
  2678  
  2679  //Signature The name of the rule.
  2680  func (receiver *Uppercase) Signature() string {
  2681  	return "uppercase"
  2682  }
  2683  
  2684  //Passes Determine if the validation rule passes.
  2685  func (receiver *Uppercase) Passes(data httpvalidate.Data, val any, options ...any) bool {
  2686  	name, exist := data.Get("name")
  2687  
  2688  	return strings.ToUpper(val.(string)) == val.(string) && len(val.(string)) == cast.ToInt(options[0]) && name == val && exist
  2689  }
  2690  
  2691  //Message Get the validation error message.
  2692  func (receiver *Uppercase) Message() string {
  2693  	return ":attribute must be upper"
  2694  }
  2695  
  2696  type Lowercase struct {
  2697  }
  2698  
  2699  //Signature The name of the rule.
  2700  func (receiver *Lowercase) Signature() string {
  2701  	return "lowercase"
  2702  }
  2703  
  2704  //Passes Determine if the validation rule passes.
  2705  func (receiver *Lowercase) Passes(data httpvalidate.Data, val any, options ...any) bool {
  2706  	address, exist := data.Get("address")
  2707  
  2708  	return strings.ToLower(val.(string)) == val.(string) && len(val.(string)) == cast.ToInt(options[0]) && address == val && exist
  2709  }
  2710  
  2711  //Message Get the validation error message.
  2712  func (receiver *Lowercase) Message() string {
  2713  	return ":attribute must be lower"
  2714  }
  2715  
  2716  type Duplicate struct {
  2717  }
  2718  
  2719  //Signature The name of the rule.
  2720  func (receiver *Duplicate) Signature() string {
  2721  	return "required"
  2722  }
  2723  
  2724  //Passes Determine if the validation rule passes.
  2725  func (receiver *Duplicate) Passes(data httpvalidate.Data, val any, options ...any) bool {
  2726  	return true
  2727  }
  2728  
  2729  //Message Get the validation error message.
  2730  func (receiver *Duplicate) Message() string {
  2731  	return ""
  2732  }