From 207b714da8cace765dbf44fa6200b1ef06f085fe Mon Sep 17 00:00:00 2001 From: Dean Karn Date: Fri, 13 Feb 2015 09:01:11 -0500 Subject: [PATCH] issue-#2 add Error interfaces to error structs add const variable to production string variables. --- validator.go | 27 +++++++++++++++++++++++++-- validator_test.go | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/validator.go b/validator.go index e9f62b4..adbfeca 100644 --- a/validator.go +++ b/validator.go @@ -2,6 +2,7 @@ package validator import ( "errors" + "fmt" "log" "reflect" "strings" @@ -9,7 +10,9 @@ import ( ) const ( - omitempty string = "omitempty" + defaultTagName = "validate" + omitempty string = "omitempty" + validationErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n" ) // FieldValidationError contains a single fields validation error @@ -18,6 +21,12 @@ type FieldValidationError struct { ErrorTag string } +// This is intended for use in development + debugging and not intended to be a production error message. +// it also allows FieldValidationError to be used as an Error interface +func (e FieldValidationError) Error() string { + return fmt.Sprintf(validationErrMsg, e.Field, e.ErrorTag) +} + // StructValidationErrors is a struct of errors for struct fields ( Excluding fields of type struct ) // NOTE: if a field within a struct is a struct it's errors will not be contained within the current // StructValidationErrors but rather a new StructValidationErrors is created for each struct resulting in @@ -27,6 +36,19 @@ type StructValidationErrors struct { Errors map[string]*FieldValidationError } +// This is intended for use in development + debugging and not intended to be a production error message. +// it also allows StructValidationErrors to be used as an Error interface +func (e StructValidationErrors) Error() string { + + s := "" + + for _, err := range e.Errors { + s += fmt.Sprintf(validationErrMsg, err.Field, err.ErrorTag) + } + + return s +} + // ValidationFunc that accepts the value of a field and parameter for use in validation (parameter not always used or needed) type ValidationFunc func(v interface{}, param string) bool @@ -43,7 +65,7 @@ type Validator struct { // var bakedInValidators = map[string]ValidationFunc{} -var internalValidator = NewValidator("validate", bakedInValidators) +var internalValidator = NewValidator(defaultTagName, bakedInValidators) // NewValidator creates a new Validator instance // NOTE: it is not necessary to create a new validator as the internal one will do in 99.9% of cases, but the option is there. @@ -237,6 +259,7 @@ func (v *Validator) validateFieldByNameAndTag(f interface{}, name string, tag st log.Fatalf("Invalid validation tag on field %s", name) } + // OK to continue because we checked it's existance before getting into this loop if key == omitempty { continue } diff --git a/validator_test.go b/validator_test.go index 5a4a344..1010506 100644 --- a/validator_test.go +++ b/validator_test.go @@ -35,6 +35,7 @@ func TestValidateStruct(t *testing.T) { for _, j := range i.Errors { fmt.Printf("Error Field:%s Error Tag:%s\n", j.Field, j.ErrorTag) + fmt.Println(j.Error()) } }