Merge branch 'issue-#2' into v1-development

pull/16/head
Dean Karn 10 years ago
commit 4139d8bd75
  1. 27
      validator.go
  2. 1
      validator_test.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
}

@ -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())
}
}

Loading…
Cancel
Save