Skip to content

Commit

Permalink
feat(core/errors): improved the way that errors were created
Browse files Browse the repository at this point in the history
WIP on issue #16
  • Loading branch information
typerandom committed May 14, 2015
1 parent f9d65fc commit f647e85
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type context struct {
field *core.ReflectedField
isNil bool

errors *core.Errors
errors core.ErrorList
source interface{}
}

Expand Down
32 changes: 13 additions & 19 deletions core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
)

func NewValidatorError(field *ReflectedField, tag *parser.Method, err error) *Error {
func NewError(field *ReflectedField, tag *parser.Method, err error) *Error {
return &Error{
Field: field,
Tag: tag,
Expand All @@ -30,34 +30,28 @@ func (this *Error) Error() string {
return message
}

type Errors struct {
Items []error
}

func NewErrors() *Errors {
return &Errors{}
}
type ErrorList []error

func (this *Errors) Add(err error) {
this.Items = append(this.Items, err)
func (this *ErrorList) Add(err error) {
*this = append(*this, err)
}

func (this *Errors) AddMany(errs *Errors) {
for _, err := range errs.Items {
this.Items = append(this.Items, err)
func (this *ErrorList) AddMany(errs ErrorList) {
for _, err := range errs {
this.Add(err)
}
}

func (this *Errors) First() error {
return this.Items[0]
func (this ErrorList) First() error {
return this[0]
}

func (this *Errors) Any() bool {
return len(this.Items) > 0
func (this ErrorList) Any() bool {
return len(this) > 0
}

func (this *Errors) PrintAll() {
for _, err := range this.Items {
func (this ErrorList) PrintAll() {
for _, err := range this {
fmt.Println(err)
}
}
5 changes: 2 additions & 3 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ func (this *validator) Register(name string, validator core.ValidatorFn) {
this.registry.Register(name, validator)
}

func (this *validator) Validate(value interface{}) *core.Errors {
func (this *validator) Validate(value interface{}) core.ErrorList {
context := &context{
validator: this,
errors: core.NewErrors(),
}

walkValidate(context, value, nil)
Expand Down Expand Up @@ -51,6 +50,6 @@ func Register(name string, validator core.ValidatorFn) {
getGlobalValidator().Register(name, validator)
}

func Validate(value interface{}) *core.Errors {
func Validate(value interface{}) core.ErrorList {
return getGlobalValidator().Validate(value)
}
15 changes: 6 additions & 9 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func walkValidateStruct(context *context, normalized *core.NormalizedValue, pare
normalizedFieldValue, err := core.Normalize(fieldValue)

if err != nil {
context.errors.Add(core.NewValidatorError(field, nil, err))
context.errors.Add(core.NewError(field, nil, err))
continue
}

Expand All @@ -61,10 +61,10 @@ func walkValidateStruct(context *context, normalized *core.NormalizedValue, pare
context.setSource(normalized.Value)
context.setValue(normalizedFieldValue)

var mostRecentErrors *core.Errors
var mostRecentErrors core.ErrorList

for _, methods := range field.MethodGroups {
var errors *core.Errors
var errors core.ErrorList

for _, method := range methods {
validate, err := context.validator.registry.Get(method.Name)
Expand All @@ -75,21 +75,18 @@ func walkValidateStruct(context *context, normalized *core.NormalizedValue, pare
}

if err = validate(context, method.Arguments); err != nil {
if errors == nil {
errors = core.NewErrors()
}
errors.Add(core.NewValidatorError(field, method, err))
errors.Add(core.NewError(field, method, err))
}
}

mostRecentErrors = errors

if errors == nil {
if !errors.Any() {
break
}
}

if mostRecentErrors != nil {
if mostRecentErrors.Any() {
context.errors.AddMany(mostRecentErrors)
}

Expand Down
18 changes: 9 additions & 9 deletions walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
func testThatValidatorCanWalkItems(t *testing.T, items interface{}, numItems int) {
errs := Validate(items)

if len(errs.Items) != numItems {
t.Fatalf("Expected %d errors, got %d.", numItems, len(errs.Items))
if len(errs) != numItems {
t.Fatalf("Expected %d errors, got %d.", numItems, len(errs))
}

for _, err := range errs.Items {
for _, err := range errs {
if err.Error() != "Value cannot be empty." {
t.Fatalf("Expected validation error, got %s.", err)
}
Expand Down Expand Up @@ -53,8 +53,8 @@ func TestThatValidatorCanWalkStruct(t *testing.T) {

totalFields := reflect.TypeOf(dummy).NumField()

if len(errs.Items) != totalFields {
t.Fatalf("Expected %d errors, got %d.", totalFields, len(errs.Items))
if len(errs) != totalFields {
t.Fatalf("Expected %d errors, got %d.", totalFields, len(errs))
}

expectedErrors := []string{
Expand All @@ -66,8 +66,8 @@ func TestThatValidatorCanWalkStruct(t *testing.T) {
}

for i, err := range expectedErrors {
if errs.Items[i].Error() != err {
t.Fatalf("Expected error '%s', but got '%s'.", err, errs.Items[i].Error())
if errs[i].Error() != err {
t.Fatalf("Expected error '%s', but got '%s'.", err, errs[i].Error())
}
}
}
Expand All @@ -79,8 +79,8 @@ func testThatValidatorCannotWalkValue(t *testing.T, dummy interface{}, typeName
t.Fatalf("Expected error, but didn't get any.")
}

if len(errs.Items) != 1 {
t.Fatalf("Expected 1 error, but got %d.", len(errs.Items))
if len(errs) != 1 {
t.Fatalf("Expected 1 error, but got %d.", len(errs))
}

expectedErr := "Unable to directly validate type '" + typeName + "'."
Expand Down

0 comments on commit f647e85

Please sign in to comment.