Skip to content

Commit

Permalink
Merge branch 'master' into haozi/optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi authored Sep 11, 2024
2 parents 021515c + 413083b commit ca34907
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,119 @@ type Case struct {
setup func(Case)
}

func TestRule_Regex(t *testing.T) {
validation := NewValidation()
tests := []Case{
{
description: "success with valid regex match",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"email": "[email protected]",
}, map[string]string{
"email": "regex:^\\S+@\\S+\\.\\S+$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.False(t, validator.Fails(), c.description)
},
},
{
description: "error with invalid regex match",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"email": "testexample.com",
}, map[string]string{
"email": "regex:^\\S+@\\S+\\.\\S+$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.Equal(t, map[string]string{
"regex": "email value does not pass the regex check",
}, validator.Errors().Get("email"))
},
},
{
description: "success with regex and nested structure",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"user": map[string]string{
"email": "[email protected]",
},
}, map[string]string{
"user.email": "regex:^\\S+@\\S+\\.\\S+$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.False(t, validator.Fails(), c.description)
},
},
{
description: "error with regex and nested structure",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"user": map[string]string{
"email": "testexample.com",
},
}, map[string]string{
"user.email": "regex:^\\S+@\\S+\\.\\S+$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.Equal(t, map[string]string{
"regex": "user.email value does not pass the regex check",
}, validator.Errors().Get("user.email"))
},
},
{
description: "panic when regex pattern is missing",
setup: func(c Case) {
assert.Panics(t, func() {
_, err := validation.Make(map[string]any{
"email": "[email protected]",
}, map[string]string{
"email": "regex:",
})
assert.NotNil(t, err, c.description)
}, c.description)
},
},
{
description: "success with valid regexp match",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"phone": "+1-800-555-5555",
}, map[string]string{
"phone": "regexp:^\\+\\d{1,3}-\\d{3}-\\d{3}-\\d{4}$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.False(t, validator.Fails(), c.description)
},
},
{
description: "error with invalid regexp match",
setup: func(c Case) {
validator, err := validation.Make(map[string]any{
"phone": "18005555555",
}, map[string]string{
"phone": "regexp:^\\+\\d{1,3}-\\d{3}-\\d{3}-\\d{4}$",
})
assert.Nil(t, err, c.description)
assert.NotNil(t, validator, c.description)
assert.Equal(t, map[string]string{
"regexp": "phone must match pattern ^\\+\\d{1,3}-\\d{3}-\\d{3}-\\d{4}$",
}, validator.Errors().Get("phone"))
},
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
test.setup(test)
})
}
}

func TestRule_Required(t *testing.T) {
validation := NewValidation()
tests := []Case{
Expand Down

0 comments on commit ca34907

Please sign in to comment.