Skip to content

Commit

Permalink
Merge pull request #24 from twharmon/flexible-usage
Browse files Browse the repository at this point in the history
Allow more flexible usage
  • Loading branch information
twharmon authored Oct 12, 2020
2 parents 930c6d5 + 7df7b87 commit 1e4748a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
4 changes: 2 additions & 2 deletions govalid.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"errors"
)

// ErrNotPtrToStruct is encountered when an attempt is made to validate
// ErrNotStruct is encountered when an attempt is made to validate
// a type that is not a struct is made.
var ErrNotPtrToStruct = errors.New("only pointers to structs can be validated")
var ErrNotStruct = errors.New("only structs can be validated")

// ErrNotRegistered is encountered when an attempt is made to
// validate a type that has not yet been registered.
Expand Down
29 changes: 13 additions & 16 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ func (v *Validator) AddCustom(s interface{}, f ...func(interface{}) string) erro
}

// Violation checks the struct s against all constraints and custom
// validation functions, if any. It returns an error if the struct
// fails validation. If the type being validated is not a struct,
// ErrNotPtrToStruct will be returned. If the type being validated
// validation functions, if any. It returns an violation if the
// struct fails validation. If the type being validated is not a
// struct, ErrNotStruct will be returned. If the type being validated
// has not yet been registered, ErrNotRegistered is returned.
func (v *Validator) Violation(s interface{}) (string, error) {
t := reflect.TypeOf(s)
if t.Kind() != reflect.Ptr {
return "", ErrNotPtrToStruct
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return "", ErrNotPtrToStruct
return "", ErrNotStruct
}
m := v.modelStore[t.Name()]
if m == nil {
Expand All @@ -62,19 +61,17 @@ func (v *Validator) Violation(s interface{}) (string, error) {
}

// Violations checks the struct s against all constraints and custom
// validation functions, if any. It returns a slice of errors if the
// struct fails validation. If the type being validated is not a
// struct, ErrNotPtrToStruct alone will be returned. If the type
// being validated has not yet been registered, ErrNotRegistered
// alone is returned.
// validation functions, if any. It returns a slice of violations if
// the struct fails validation. If the type being validated is not a
// struct, ErrNotStruct will be returned. If the type being validated
// has not yet been registered, ErrNotRegistered is returned.
func (v *Validator) Violations(s interface{}) ([]string, error) {
t := reflect.TypeOf(s)
if t.Kind() != reflect.Ptr {
return nil, ErrNotPtrToStruct
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return nil, ErrNotPtrToStruct
return nil, ErrNotStruct
}
m := v.modelStore[t.Name()]
if m == nil {
Expand Down
13 changes: 2 additions & 11 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,13 @@ func TestValidatorCustomNotRegistered(t *testing.T) {
notEqual(t, err, nil)
}

func TestValidatorNotPtr(t *testing.T) {
v := govalid.New()
type Foo struct{}
_, err := v.Violation(Foo{})
equals(t, err, govalid.ErrNotPtrToStruct)
_, err = v.Violations(Foo{})
equals(t, err, govalid.ErrNotPtrToStruct)
}

func TestValidatorNotStruct(t *testing.T) {
v := govalid.New()
m := make(map[string]string)
_, err := v.Violation(&m)
equals(t, err, govalid.ErrNotPtrToStruct)
equals(t, err, govalid.ErrNotStruct)
_, err = v.Violations(&m)
equals(t, err, govalid.ErrNotPtrToStruct)
equals(t, err, govalid.ErrNotStruct)
}

func TestValidatorRegisterNotStruct(t *testing.T) {
Expand Down

0 comments on commit 1e4748a

Please sign in to comment.