diff --git a/README.md b/README.md index b94dbb5..830f138 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ It has the following **unique** features: - Handles type interface by determining it's underlying type prior to validation. - Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29) - Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs +- Extraction of custom defined Field Name e.g. can specify to extract the JSON name while validating and have it available in the resulting FieldError Installation ------------ @@ -201,36 +202,36 @@ func ValidateValuer(field reflect.Value) interface{} { Benchmarks ------ -###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go 1.5 +###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go 1.5.1 ```go $ go test -cpu=4 -bench=. -benchmem=true PASS -BenchmarkFieldSuccess-4 5000000 296 ns/op 16 B/op 1 allocs/op +BenchmarkFieldSuccess-4 5000000 291 ns/op 16 B/op 1 allocs/op BenchmarkFieldFailure-4 5000000 294 ns/op 16 B/op 1 allocs/op -BenchmarkFieldDiveSuccess-4 500000 2529 ns/op 384 B/op 19 allocs/op -BenchmarkFieldDiveFailure-4 500000 3056 ns/op 768 B/op 23 allocs/op -BenchmarkFieldCustomTypeSuccess-4 3000000 443 ns/op 32 B/op 2 allocs/op -BenchmarkFieldCustomTypeFailure-4 2000000 753 ns/op 384 B/op 4 allocs/op -BenchmarkFieldOrTagSuccess-4 1000000 1334 ns/op 32 B/op 2 allocs/op -BenchmarkFieldOrTagFailure-4 1000000 1172 ns/op 416 B/op 6 allocs/op -BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1206 ns/op 80 B/op 5 allocs/op -BenchmarkStructSimpleCustomTypeFailure-4 1000000 1737 ns/op 592 B/op 11 allocs/op -BenchmarkStructPartialSuccess-4 1000000 1367 ns/op 400 B/op 11 allocs/op -BenchmarkStructPartialFailure-4 1000000 1914 ns/op 800 B/op 16 allocs/op -BenchmarkStructExceptSuccess-4 2000000 909 ns/op 368 B/op 9 allocs/op -BenchmarkStructExceptFailure-4 1000000 1350 ns/op 400 B/op 11 allocs/op -BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1218 ns/op 128 B/op 6 allocs/op -BenchmarkStructSimpleCrossFieldFailure-4 1000000 1783 ns/op 544 B/op 11 allocs/op -BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1806 ns/op 160 B/op 8 allocs/op -BenchmarkStructSimpleCrossStructCrossFieldFailure-4 1000000 2369 ns/op 576 B/op 13 allocs/op +BenchmarkFieldDiveSuccess-4 500000 3498 ns/op 528 B/op 28 allocs/op +BenchmarkFieldDiveFailure-4 300000 4094 ns/op 928 B/op 32 allocs/op +BenchmarkFieldCustomTypeSuccess-4 3000000 460 ns/op 32 B/op 2 allocs/op +BenchmarkFieldCustomTypeFailure-4 2000000 758 ns/op 400 B/op 4 allocs/op +BenchmarkFieldOrTagSuccess-4 1000000 1393 ns/op 32 B/op 2 allocs/op +BenchmarkFieldOrTagFailure-4 1000000 1181 ns/op 432 B/op 6 allocs/op +BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1218 ns/op 80 B/op 5 allocs/op +BenchmarkStructSimpleCustomTypeFailure-4 1000000 1748 ns/op 624 B/op 11 allocs/op +BenchmarkStructPartialSuccess-4 1000000 1392 ns/op 400 B/op 11 allocs/op +BenchmarkStructPartialFailure-4 1000000 1938 ns/op 816 B/op 16 allocs/op +BenchmarkStructExceptSuccess-4 2000000 903 ns/op 368 B/op 9 allocs/op +BenchmarkStructExceptFailure-4 1000000 1381 ns/op 400 B/op 11 allocs/op +BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1215 ns/op 128 B/op 6 allocs/op +BenchmarkStructSimpleCrossFieldFailure-4 1000000 1781 ns/op 560 B/op 11 allocs/op +BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1801 ns/op 160 B/op 8 allocs/op +BenchmarkStructSimpleCrossStructCrossFieldFailure-4 1000000 2357 ns/op 592 B/op 13 allocs/op BenchmarkStructSimpleSuccess-4 1000000 1161 ns/op 48 B/op 3 allocs/op -BenchmarkStructSimpleFailure-4 1000000 1813 ns/op 592 B/op 11 allocs/op -BenchmarkStructSimpleSuccessParallel-4 5000000 353 ns/op 48 B/op 3 allocs/op -BenchmarkStructSimpleFailureParallel-4 2000000 656 ns/op 592 B/op 11 allocs/op -BenchmarkStructComplexSuccess-4 200000 7637 ns/op 432 B/op 27 allocs/op -BenchmarkStructComplexFailure-4 100000 12775 ns/op 3128 B/op 69 allocs/op -BenchmarkStructComplexSuccessParallel-4 1000000 2270 ns/op 432 B/op 27 allocs/op -BenchmarkStructComplexFailureParallel-4 300000 4328 ns/op 3128 B/op 69 allocs/op +BenchmarkStructSimpleFailure-4 1000000 1818 ns/op 624 B/op 11 allocs/op +BenchmarkStructSimpleSuccessParallel-4 5000000 375 ns/op 48 B/op 3 allocs/op +BenchmarkStructSimpleFailureParallel-4 2000000 757 ns/op 624 B/op 11 allocs/op +BenchmarkStructComplexSuccess-4 200000 8053 ns/op 432 B/op 27 allocs/op +BenchmarkStructComplexFailure-4 100000 12634 ns/op 3335 B/op 69 allocs/op +BenchmarkStructComplexSuccessParallel-4 1000000 2718 ns/op 432 B/op 27 allocs/op +BenchmarkStructComplexFailureParallel-4 300000 5086 ns/op 3336 B/op 69 allocs/op ``` How to Contribute diff --git a/validator.go b/validator.go index 1703ddc..bf04f5d 100644 --- a/validator.go +++ b/validator.go @@ -78,6 +78,7 @@ func (s *tagCacheMap) Set(key string, value *cachedTag) { // Validate contains the validator settings passed in using the Config struct type Validate struct { tagName string + fieldNameTag string validationFuncs map[string]Func customTypeFuncs map[reflect.Type]CustomTypeFunc aliasValidators map[string]string @@ -96,7 +97,8 @@ func (v *Validate) initCheck() { // Config contains the options that a Validator instance will use. // It is passed to the New() function type Config struct { - TagName string + TagName string + FieldNameTag string } // CustomTypeFunc allows for overriding or adding custom field type handler functions @@ -137,6 +139,7 @@ func (ve ValidationErrors) Error() string { // with other properties that may be needed for error message creation type FieldError struct { Field string + Name string Tag string ActualTag string Kind reflect.Kind @@ -149,8 +152,9 @@ type FieldError struct { func New(config *Config) *Validate { v := &Validate{ - tagName: config.TagName, - tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}, + tagName: config.TagName, + fieldNameTag: config.FieldNameTag, + tagsCache: &tagCacheMap{m: map[string]*cachedTag{}}, errsPool: &sync.Pool{New: func() interface{} { return ValidationErrors{} }}} @@ -245,7 +249,7 @@ func (v *Validate) Field(field interface{}, tag string) error { errs := v.errsPool.Get().(ValidationErrors) fieldVal := reflect.ValueOf(field) - v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, false, false, nil) + v.traverseField(fieldVal, fieldVal, fieldVal, blank, errs, false, tag, blank, blank, false, false, nil) if len(errs) == 0 { v.errsPool.Put(errs) @@ -265,7 +269,7 @@ func (v *Validate) FieldWithValue(val interface{}, field interface{}, tag string errs := v.errsPool.Get().(ValidationErrors) topVal := reflect.ValueOf(val) - v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, false, false, nil) + v.traverseField(topVal, topVal, reflect.ValueOf(field), blank, errs, false, tag, blank, blank, false, false, nil) if len(errs) == 0 { v.errsPool.Put(errs) @@ -400,6 +404,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec numFields := current.NumField() var fld reflect.StructField + var customName string for i := 0; i < numFields; i++ { fld = typ.Field(i) @@ -417,12 +422,23 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec } } - v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, partial, exclude, includeExclude) + customName = fld.Name + if v.fieldNameTag != "" { + + name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0] + + // dash check is for json "-" means don't output in json + if name != "" && name != "-" { + customName = name + } + } + + v.traverseField(topStruct, currentStruct, current.Field(i), errPrefix, errs, true, fld.Tag.Get(v.tagName), fld.Name, customName, partial, exclude, includeExclude) } } // traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options -func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { +func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, isStructField bool, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) { if tag == skipValidationTag { return @@ -448,6 +464,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. if kind == reflect.Invalid { errs[errPrefix+name] = &FieldError{ + Name: customName, Field: name, Tag: cTag.tags[0].tag, ActualTag: cTag.tags[0].tagVals[0][0], @@ -458,6 +475,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. } errs[errPrefix+name] = &FieldError{ + Name: customName, Field: name, Tag: cTag.tags[0].tag, ActualTag: cTag.tags[0].tagVals[0][0], @@ -520,7 +538,7 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. continue } - if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, errs, valTag, name) { + if v.validateField(topStruct, currentStruct, current, typ, kind, errPrefix, errs, valTag, name, customName) { return } } @@ -530,9 +548,9 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. // or panic ;) switch kind { case reflect.Slice, reflect.Array: - v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude) + v.traverseSlice(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude) case reflect.Map: - v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, partial, exclude, includeExclude) + v.traverseMap(topStruct, currentStruct, current, errPrefix, errs, diveSubTag, name, customName, partial, exclude, includeExclude) default: // throw error, if not a slice or map then should not have gotten here // bad dive tag @@ -542,23 +560,23 @@ func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect. } // traverseSlice traverses a Slice or Array's elements and passes them to traverseField for validation -func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { +func (v *Validate) traverseSlice(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) { for i := 0; i < current.Len(); i++ { - v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), partial, exclude, includeExclude) + v.traverseField(topStruct, currentStruct, current.Index(i), errPrefix, errs, false, tag, fmt.Sprintf(arrayIndexFieldName, name, i), fmt.Sprintf(arrayIndexFieldName, customName, i), partial, exclude, includeExclude) } } // traverseMap traverses a map's elements and passes them to traverseField for validation -func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag string, name string, partial bool, exclude bool, includeExclude map[string]*struct{}) { +func (v *Validate) traverseMap(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, errs ValidationErrors, tag, name, customName string, partial bool, exclude bool, includeExclude map[string]*struct{}) { for _, key := range current.MapKeys() { - v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), partial, exclude, includeExclude) + v.traverseField(topStruct, currentStruct, current.MapIndex(key), errPrefix, errs, false, tag, fmt.Sprintf(mapIndexFieldName, name, key.Interface()), fmt.Sprintf(mapIndexFieldName, customName, key.Interface()), partial, exclude, includeExclude) } } // validateField validates a field based on the provided tag's key and param values and returns true if there is an error or false if all ok -func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, valTag *tagVals, name string) bool { +func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, currentType reflect.Type, currentKind reflect.Kind, errPrefix string, errs ValidationErrors, valTag *tagVals, name, customName string) bool { var valFunc Func var ok bool @@ -583,6 +601,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. if valTag.isAlias { errs[errPrefix+name] = &FieldError{ + Name: customName, Field: name, Tag: valTag.tag, ActualTag: errTag[1:], @@ -592,6 +611,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. } } else { errs[errPrefix+name] = &FieldError{ + Name: customName, Field: name, Tag: errTag[1:], ActualTag: errTag[1:], @@ -614,6 +634,7 @@ func (v *Validate) validateField(topStruct reflect.Value, currentStruct reflect. } errs[errPrefix+name] = &FieldError{ + Name: customName, Field: name, Tag: valTag.tag, ActualTag: valTag.tagVals[0][0], diff --git a/validator_test.go b/validator_test.go index 071a24f..6875693 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4930,3 +4930,30 @@ func TestInvalidValidatorFunction(t *testing.T) { PanicMatches(t, func() { validate.Field(s.Test, "zzxxBadFunction") }, "Undefined validation function on field") } + +func TestCustomFieldName(t *testing.T) { + type A struct { + B string `schema:"b" validate:"required"` + C string `schema:"c" validate:"required"` + D []bool `schema:"d" validate:"required"` + E string `schema:"-" validate:"required"` + } + + a := &A{} + + errs := New(&Config{TagName: "validate", FieldNameTag: "schema"}).Struct(a).(ValidationErrors) + NotEqual(t, errs, nil) + Equal(t, len(errs), 4) + Equal(t, errs["A.B"].Name, "b") + Equal(t, errs["A.C"].Name, "c") + Equal(t, errs["A.D"].Name, "d") + Equal(t, errs["A.E"].Name, "E") + + errs = New(&Config{TagName: "validate"}).Struct(a).(ValidationErrors) + NotEqual(t, errs, nil) + Equal(t, len(errs), 4) + Equal(t, errs["A.B"].Name, "B") + Equal(t, errs["A.C"].Name, "C") + Equal(t, errs["A.D"].Name, "D") + Equal(t, errs["A.E"].Name, "E") +}