Skip to content

Commit e82c80a

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #190 from joeybloggs/v8-development
Merge changes for custom field name extraction
2 parents 344bcd6 + 46f4476 commit e82c80a

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

README.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ It has the following **unique** features:
1515
- Handles type interface by determining it's underlying type prior to validation.
1616
- Handles custom field types such as sql driver Valuer see [Valuer](https://golang.org/src/database/sql/driver/types.go?s=1210:1293#L29)
1717
- Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
18+
- 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
1819

1920
Installation
2021
------------
@@ -201,36 +202,36 @@ func ValidateValuer(field reflect.Value) interface{} {
201202

202203
Benchmarks
203204
------
204-
###### Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go 1.5
205+
###### 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
205206
```go
206207
$ go test -cpu=4 -bench=. -benchmem=true
207208
PASS
208-
BenchmarkFieldSuccess-4 5000000 296 ns/op 16 B/op 1 allocs/op
209+
BenchmarkFieldSuccess-4 5000000 291 ns/op 16 B/op 1 allocs/op
209210
BenchmarkFieldFailure-4 5000000 294 ns/op 16 B/op 1 allocs/op
210-
BenchmarkFieldDiveSuccess-4 500000 2529 ns/op 384 B/op 19 allocs/op
211-
BenchmarkFieldDiveFailure-4 500000 3056 ns/op 768 B/op 23 allocs/op
212-
BenchmarkFieldCustomTypeSuccess-4 3000000 443 ns/op 32 B/op 2 allocs/op
213-
BenchmarkFieldCustomTypeFailure-4 2000000 753 ns/op 384 B/op 4 allocs/op
214-
BenchmarkFieldOrTagSuccess-4 1000000 1334 ns/op 32 B/op 2 allocs/op
215-
BenchmarkFieldOrTagFailure-4 1000000 1172 ns/op 416 B/op 6 allocs/op
216-
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1206 ns/op 80 B/op 5 allocs/op
217-
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1737 ns/op 592 B/op 11 allocs/op
218-
BenchmarkStructPartialSuccess-4 1000000 1367 ns/op 400 B/op 11 allocs/op
219-
BenchmarkStructPartialFailure-4 1000000 1914 ns/op 800 B/op 16 allocs/op
220-
BenchmarkStructExceptSuccess-4 2000000 909 ns/op 368 B/op 9 allocs/op
221-
BenchmarkStructExceptFailure-4 1000000 1350 ns/op 400 B/op 11 allocs/op
222-
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1218 ns/op 128 B/op 6 allocs/op
223-
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1783 ns/op 544 B/op 11 allocs/op
224-
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1806 ns/op 160 B/op 8 allocs/op
225-
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 1000000 2369 ns/op 576 B/op 13 allocs/op
211+
BenchmarkFieldDiveSuccess-4 500000 3498 ns/op 528 B/op 28 allocs/op
212+
BenchmarkFieldDiveFailure-4 300000 4094 ns/op 928 B/op 32 allocs/op
213+
BenchmarkFieldCustomTypeSuccess-4 3000000 460 ns/op 32 B/op 2 allocs/op
214+
BenchmarkFieldCustomTypeFailure-4 2000000 758 ns/op 400 B/op 4 allocs/op
215+
BenchmarkFieldOrTagSuccess-4 1000000 1393 ns/op 32 B/op 2 allocs/op
216+
BenchmarkFieldOrTagFailure-4 1000000 1181 ns/op 432 B/op 6 allocs/op
217+
BenchmarkStructSimpleCustomTypeSuccess-4 1000000 1218 ns/op 80 B/op 5 allocs/op
218+
BenchmarkStructSimpleCustomTypeFailure-4 1000000 1748 ns/op 624 B/op 11 allocs/op
219+
BenchmarkStructPartialSuccess-4 1000000 1392 ns/op 400 B/op 11 allocs/op
220+
BenchmarkStructPartialFailure-4 1000000 1938 ns/op 816 B/op 16 allocs/op
221+
BenchmarkStructExceptSuccess-4 2000000 903 ns/op 368 B/op 9 allocs/op
222+
BenchmarkStructExceptFailure-4 1000000 1381 ns/op 400 B/op 11 allocs/op
223+
BenchmarkStructSimpleCrossFieldSuccess-4 1000000 1215 ns/op 128 B/op 6 allocs/op
224+
BenchmarkStructSimpleCrossFieldFailure-4 1000000 1781 ns/op 560 B/op 11 allocs/op
225+
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4 1000000 1801 ns/op 160 B/op 8 allocs/op
226+
BenchmarkStructSimpleCrossStructCrossFieldFailure-4 1000000 2357 ns/op 592 B/op 13 allocs/op
226227
BenchmarkStructSimpleSuccess-4 1000000 1161 ns/op 48 B/op 3 allocs/op
227-
BenchmarkStructSimpleFailure-4 1000000 1813 ns/op 592 B/op 11 allocs/op
228-
BenchmarkStructSimpleSuccessParallel-4 5000000 353 ns/op 48 B/op 3 allocs/op
229-
BenchmarkStructSimpleFailureParallel-4 2000000 656 ns/op 592 B/op 11 allocs/op
230-
BenchmarkStructComplexSuccess-4 200000 7637 ns/op 432 B/op 27 allocs/op
231-
BenchmarkStructComplexFailure-4 100000 12775 ns/op 3128 B/op 69 allocs/op
232-
BenchmarkStructComplexSuccessParallel-4 1000000 2270 ns/op 432 B/op 27 allocs/op
233-
BenchmarkStructComplexFailureParallel-4 300000 4328 ns/op 3128 B/op 69 allocs/op
228+
BenchmarkStructSimpleFailure-4 1000000 1818 ns/op 624 B/op 11 allocs/op
229+
BenchmarkStructSimpleSuccessParallel-4 5000000 375 ns/op 48 B/op 3 allocs/op
230+
BenchmarkStructSimpleFailureParallel-4 2000000 757 ns/op 624 B/op 11 allocs/op
231+
BenchmarkStructComplexSuccess-4 200000 8053 ns/op 432 B/op 27 allocs/op
232+
BenchmarkStructComplexFailure-4 100000 12634 ns/op 3335 B/op 69 allocs/op
233+
BenchmarkStructComplexSuccessParallel-4 1000000 2718 ns/op 432 B/op 27 allocs/op
234+
BenchmarkStructComplexFailureParallel-4 300000 5086 ns/op 3336 B/op 69 allocs/op
234235
```
235236

236237
How to Contribute

validator.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
404404
numFields := current.NumField()
405405

406406
var fld reflect.StructField
407+
var customName string
407408

408409
for i := 0; i < numFields; i++ {
409410
fld = typ.Field(i)
@@ -421,10 +422,13 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
421422
}
422423
}
423424

424-
customName := fld.Name
425+
customName = fld.Name
425426
if v.fieldNameTag != "" {
426-
name := strings.Split(fld.Tag.Get(v.fieldNameTag), ",")[0]
427-
if name != "" {
427+
428+
name := strings.SplitN(fld.Tag.Get(v.fieldNameTag), ",", 2)[0]
429+
430+
// dash check is for json "-" means don't output in json
431+
if name != "" && name != "-" {
428432
customName = name
429433
}
430434
}

validator_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,15 +4936,24 @@ func TestCustomFieldName(t *testing.T) {
49364936
B string `schema:"b" validate:"required"`
49374937
C string `schema:"c" validate:"required"`
49384938
D []bool `schema:"d" validate:"required"`
4939+
E string `schema:"-" validate:"required"`
49394940
}
49404941

49414942
a := &A{}
49424943

49434944
errs := New(&Config{TagName: "validate", FieldNameTag: "schema"}).Struct(a).(ValidationErrors)
4945+
NotEqual(t, errs, nil)
4946+
Equal(t, len(errs), 4)
49444947
Equal(t, errs["A.B"].Name, "b")
49454948
Equal(t, errs["A.C"].Name, "c")
49464949
Equal(t, errs["A.D"].Name, "d")
4950+
Equal(t, errs["A.E"].Name, "E")
49474951

49484952
errs = New(&Config{TagName: "validate"}).Struct(a).(ValidationErrors)
4953+
NotEqual(t, errs, nil)
4954+
Equal(t, len(errs), 4)
49494955
Equal(t, errs["A.B"].Name, "B")
4956+
Equal(t, errs["A.C"].Name, "C")
4957+
Equal(t, errs["A.D"].Name, "D")
4958+
Equal(t, errs["A.E"].Name, "E")
49504959
}

0 commit comments

Comments
 (0)