Skip to content

Commit

Permalink
feat: added new rules
Browse files Browse the repository at this point in the history
  • Loading branch information
JayDubyaEey committed Jan 26, 2024
1 parent 5ca6f64 commit 4756ce0
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 3 deletions.
65 changes: 63 additions & 2 deletions rules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package validator

import "fmt"
import (
"fmt"
"regexp"
"strings"
)

type RuleConfig struct {
Value any
Expand Down Expand Up @@ -62,7 +66,64 @@ func Required() *RuleConfig {
return false
}

return len(val) > 0
trimmedVal := strings.TrimSpace(val)
return len(trimmedVal) > 0
},
}
}

func Range(min, max int) *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("%v is not within the range %v - %v", r.Value, min, max)
},
ValidateFunc: func(r RuleConfig) bool {
val, ok := r.Value.(int)
if !ok {
return false
}
return val >= min && val <= max
},
}
}

func LessThan(max int) *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("%d must be less than %d", r.Value, max)
},
ValidateFunc: func(r RuleConfig) bool {
val, ok := interface{}(r.Value).(int)
if !ok {
return false
}
return val < max
},
}
}
func GreaterThan(min int) *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("%d must be greater than %d", r.Value, min)
},
ValidateFunc: func(r RuleConfig) bool {
val, ok := interface{}(r.Value).(int)
if !ok {
return false
}
return val > min
},
}
}

func Regex(pattern string) *RuleConfig {
return &RuleConfig{
MessageFunc: func(r RuleConfig) string {
return fmt.Sprintf("'%s' does not match the pattern '%s'", r.Value, pattern)
},
ValidateFunc: func(r RuleConfig) bool {
re := regexp.MustCompile(pattern)
return re.MatchString(r.Value.(string))
},
}
}
102 changes: 101 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package validator_test
import (
"testing"

"github.com/Jamess-Lucass/validator-go"
"github.com/stretchr/testify/assert"

"github.com/Jamess-Lucass/validator-go"
)

type User struct {
Firstname string
Lastname string
Age int
}

func Test_MinWithValidLength_ReturnsTrue(t *testing.T) {
Expand Down Expand Up @@ -96,3 +98,101 @@ func Test_RequiredWithNoValue_ReturnsFalse(t *testing.T) {

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

func Test_RequiredWithEmptyString_ReturnsFalse(t *testing.T) {
user := User{
Firstname: "",
}

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

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

func Test_RequiredWithWhitespaceString_ReturnsFalse(t *testing.T) {
user := User{
Firstname: " ",
}

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

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

func Test_LessThanWithValidValue_ReturnsTrue(t *testing.T) {

user := User{
Age: 21,
}

rule := validator.RuleFor(user.Age, validator.LessThan(22))

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

}

func Test_LessThanWithInvalidValue_ReturnsFalse(t *testing.T) {

user := User{
Age: 21,
}

rule := validator.RuleFor(user.Age, validator.LessThan(20))

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

}

func Test_GreaterThanWithValidValue_ReturnsTrue(t *testing.T) {

user := User{
Age: 21,
}

rule := validator.RuleFor(user.Age, validator.GreaterThan(20))

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

}

func Test_GreaterThanWithInvalidValue_ReturnsFalse(t *testing.T) {

user := User{
Age: 21,
}

rule := validator.RuleFor(user.Age, validator.GreaterThan(22))

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

}

func Test_RegexWithValidValue_ReturnsTrue(t *testing.T) {

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

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

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

}

func Test_RegexWithInvalidValue_ReturnsFalse(t *testing.T) {

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

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

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

}

0 comments on commit 4756ce0

Please sign in to comment.