Skip to content

Commit

Permalink
wip: tests and optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDubyaEey committed Jan 26, 2024
1 parent 4756ce0 commit d9f22a2
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
22 changes: 22 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ func Regex(pattern string) *RuleConfig {
},
}
}

func IsAlpha() *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("'%s' is not alphabetic", r.Value)
},
ValidateFunc: func(r RuleConfig) bool {
return regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(r.Value.(string))
},
}
}

func IsAlphaNumeric() *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("'%s' is not alphanumeric", r.Value)
},
ValidateFunc: func(r RuleConfig) bool {
return regexp.MustCompile(`^[a-zA-Z0-9]+$`).MatchString(r.Value.(string))
},
}
}
4 changes: 1 addition & 3 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ func (r *MultipleRules) Validate() *ValidationResult {
res := &ValidationResult{}

for _, rule := range *r {
for _, err := range rule.Validate().Errors {
res.Errors = append(res.Errors, err)
}
res.Errors = append(res.Errors, rule.Validate().Errors...)
}

return res
Expand Down
108 changes: 108 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,111 @@ func Test_RegexWithInvalidValue_ReturnsFalse(t *testing.T) {
assert.Equal(t, false, rule.Validate().IsValid())

}

func Test_RegexWithInvalidValueAndCustomMessage_ReturnsFalse(t *testing.T) {

user := User{
Firstname: "john",
}

rule := validator.RuleFor(user.Firstname, validator.Regex("^[0-9]+$").WithMessage("Only numbers allowed"))

assert.Equal(t, false, rule.Validate().IsValid())

}

func Test_RegexWithInvalidValueAndCustomMessage_ReturnsCustomMessage(t *testing.T) {

user := User{
Firstname: "john",
}

rule := validator.RuleFor(user.Firstname, validator.Regex("^[0-9]+$").WithMessage("Only numbers allowed"))

assert.Equal(t, "Only numbers allowed", rule.Validate().Errors[0].Error())

}

func Test_RegexWithValidValueAndCustomMessage_ReturnsTrue(t *testing.T) {

user := User{
Firstname: "john",
}

rule := validator.RuleFor(user.Firstname, validator.Regex("^[a-z]+$").WithMessage("Only letters allowed"))

assert.Equal(t, true, rule.Validate().IsValid())

}

func Test_IsAlphaWithValidValue_ReturnsTrue(t *testing.T) {

user := User{
Firstname: "john",
}

rule := validator.RuleFor(user.Firstname, validator.IsAlpha())

assert.Equal(t, true, rule.Validate().IsValid())

}

func Test_IsAlphaWithInvalidValue_ReturnsFalse(t *testing.T) {

user := User{
Firstname: "21",
}

rule := validator.RuleFor(user.Age, validator.IsAlpha())

assert.Equal(t, false, rule.Validate().IsValid())

}

func Test_IsAlphaNumericWithValidValue_ReturnsTrue(t *testing.T) {

user := User{
Firstname: "john21",
}

rule := validator.RuleFor(user.Firstname, validator.IsAlphaNumeric())

assert.Equal(t, true, rule.Validate().IsValid())

}

func Test_IsAlphaNumericWithInvalidValue_ReturnsFalse(t *testing.T) {

user := User{
Firstname: "john21!",
}

rule := validator.RuleFor(user.Firstname, validator.IsAlphaNumeric())

assert.Equal(t, false, rule.Validate().IsValid())

}

func Test_IsAlphaNumericWithInvalidValueAndCustomMessage_ReturnsFalse(t *testing.T) {

user := User{
Firstname: "john21!",
}

rule := validator.RuleFor(user.Firstname, validator.IsAlphaNumeric().WithMessage("Only letters and numbers allowed"))

assert.Equal(t, false, rule.Validate().IsValid())

}

func Test_IsAlphaNumericWithInvalidValueAndCustomMessage_ReturnsCustomMessage(t *testing.T) {

user := User{
Firstname: "john21!",
}

rule := validator.RuleFor(user.Firstname, validator.IsAlphaNumeric().WithMessage("Only letters and numbers allowed"))

assert.Equal(t, "Only letters and numbers allowed", rule.Validate().Errors[0].Error())

}

0 comments on commit d9f22a2

Please sign in to comment.