Skip to content

Commit

Permalink
add some validators
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Oct 9, 2019
1 parent af04eb8 commit 40b6b4f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
42 changes: 42 additions & 0 deletions validators.go → validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func NewURLValidator() Validator {
}
}

// NewMaybeURLValidator returns a validator to validate the value may be empty
// or a URL.
func NewMaybeURLValidator() Validator {
return Or(NewEmptyStrValidator(), NewURLValidator())
}

// NewURLSliceValidator returns a validator to validate whether the string element
// of the []string value is a valid URL.
func NewURLSliceValidator() Validator {
Expand All @@ -192,6 +198,12 @@ func NewIPValidator() Validator {
}
}

// NewMaybeIPValidator returns a validator to validate the value may be empty
// or a ip.
func NewMaybeIPValidator() Validator {
return Or(NewEmptyStrValidator(), NewIPValidator())
}

// NewIPSliceValidator returns a validator to validate whether the string element
// of the []string value is a valid IP.
func NewIPSliceValidator() Validator {
Expand All @@ -213,6 +225,12 @@ func NewEmailValidator() Validator {
}
}

// NewMaybeEmailValidator returns a validator to validate the value may be empty
// or an email.
func NewMaybeEmailValidator() Validator {
return Or(NewEmptyStrValidator(), NewEmailValidator())
}

// NewEmailSliceValidator returns a validator to validate whether the string element
// of the []string value is a valid email.
func NewEmailSliceValidator() Validator {
Expand Down Expand Up @@ -244,12 +262,36 @@ func NewAddressValidator() Validator {
}
}

// NewMaybeAddressValidator returns a validator to validate the value may be
// empty or an address.
func NewMaybeAddressValidator() Validator {
return Or(NewEmptyStrValidator(), NewAddressValidator())
}

// NewAddressOrIPValidator is equal to NewAddressValidator, but it maybe miss
// the port.
func NewAddressOrIPValidator() Validator {
return Or(NewIPValidator(), NewAddressValidator())
}

// NewMaybeAddressOrIPValidator returns a validator to validate the value may be
// empty or an address or an ip.
func NewMaybeAddressOrIPValidator() Validator {
return Or(NewEmptyStrValidator(), NewAddressOrIPValidator())
}

// NewAddressSliceValidator returns a validator to validate whether the string element
// of the []string value is a valid address.
func NewAddressSliceValidator() Validator {
return NewStrSliceValidator(NewAddressValidator())
}

// NewAddressOrIPSliceValidator returns a validator to validate whether
// the string element of the []string value is an address or ip.
func NewAddressOrIPSliceValidator() Validator {
return NewStrSliceValidator(NewAddressOrIPValidator())
}

// NewIntegerRangeValidator returns a validator to validate whether the integer
// value is between the min and the max.
//
Expand Down
66 changes: 66 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gconf

import "testing"

func TestNewMaybeURLValidator(t *testing.T) {
validate := NewMaybeURLValidator()
if err := validate(""); err != nil {
t.Error(err)
} else if err = validate("http://www.example.com"); err != nil {
t.Error(err)
}
}

func TestNewMaybeIPValidator(t *testing.T) {
validate := NewMaybeIPValidator()
if err := validate(""); err != nil {
t.Error(err)
} else if err = validate("1.2.3.4"); err != nil {
t.Error(err)
}
}

func TestNewMaybeEmailValidator(t *testing.T) {
validate := NewMaybeEmailValidator()
if err := validate(""); err != nil {
t.Error(err)
} else if err = validate("[email protected]"); err != nil {
t.Error(err)
}
}

func TestNewMaybeAddressValidator(t *testing.T) {
validate := NewMaybeAddressValidator()
if err := validate(""); err != nil {
t.Error(err)
} else if err = validate("1.2.3.4:80"); err != nil {
t.Error(err)
}
}

func TestNewAddressOrIPValidator(t *testing.T) {
validate := NewAddressOrIPValidator()
if err := validate("1.2.3.4"); err != nil {
t.Error(err)
} else if err = validate("1.2.3.4:80"); err != nil {
t.Error(err)
}
}

func TestNewMaybeAddressOrIPValidator(t *testing.T) {
validate := NewMaybeAddressOrIPValidator()
if err := validate(""); err != nil {
t.Error(err)
} else if err = validate("1.2.3.4"); err != nil {
t.Error(err)
} else if err = validate("1.2.3.4:80"); err != nil {
t.Error(err)
}
}

func TestNewAddressOrIPSliceValidator(t *testing.T) {
validate := NewAddressOrIPSliceValidator()
if err := validate([]string{"1.2.3.4", "1.2.3.4:80"}); err != nil {
t.Error(err)
}
}

0 comments on commit 40b6b4f

Please sign in to comment.