Skip to content

Commit fb68f39

Browse files
author
Dean Karn
authored
Add Access to Field Name from FieldLevel (#284)
1 parent 27158c7 commit fb68f39

File tree

11 files changed

+102
-14
lines changed

11 files changed

+102
-14
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
![Project status](https://img.shields.io/badge/version-9.3.6-green.svg)
4+
![Project status](https://img.shields.io/badge/version-9.4.0-green.svg)
55
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
66
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)
@@ -19,7 +19,7 @@ It has the following **unique** features:
1919
- Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
2020
- 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
2121
- Customizable i18n aware error messages.
22-
- Default validator for the [gin](https://github.com/gin-gonic/gin) web framework; upgrading from v8 to v9 in gin see [here](https://github.com/go-playground/validator/tree/v9/examples/gin-upgrading-overriding)
22+
- Default validator for the [gin](https://github.com/gin-gonic/gin) web framework; upgrading from v8 to v9 in gin see [here](https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding)
2323

2424
Installation
2525
------------
@@ -56,11 +56,11 @@ Please see http://godoc.org/gopkg.in/go-playground/validator.v9 for detailed usa
5656

5757
##### Examples:
5858

59-
- [Simple](https://github.com/go-playground/validator/blob/v9/examples/simple/main.go)
60-
- [Custom Field Types](https://github.com/go-playground/validator/blob/v9/examples/custom/main.go)
61-
- [Struct Level](https://github.com/go-playground/validator/blob/v9/examples/struct-level/main.go)
62-
- [Translations & Custom Errors](https://github.com/go-playground/validator/blob/v9/examples/translations/main.go)
63-
- [Gin upgrade and/or override validator](https://github.com/go-playground/validator/tree/v9/examples/gin-upgrading-overriding)
59+
- [Simple](https://github.com/go-playground/validator/blob/v9/_examples/simple/main.go)
60+
- [Custom Field Types](https://github.com/go-playground/validator/blob/v9/_examples/custom/main.go)
61+
- [Struct Level](https://github.com/go-playground/validator/blob/v9/_examples/struct-level/main.go)
62+
- [Translations & Custom Errors](https://github.com/go-playground/validator/blob/v9/_examples/translations/main.go)
63+
- [Gin upgrade and/or override validator](https://github.com/go-playground/validator/tree/v9/_examples/gin-upgrading-overriding)
6464
- [wash - an example application putting it all together](https://github.com/bluesuncorp/wash)
6565

6666
Benchmarks
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ based on tags.
55
It can also handle Cross-Field and Cross-Struct validation for nested structs
66
and has the ability to dive into arrays and maps of any type.
77
8-
see more examples https://github.com/go-playground/validator/tree/v9/examples
8+
see more examples https://github.com/go-playground/validator/tree/v9/_examples
99
1010
Validation Functions Return Type error
1111

field_level.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ type FieldLevel interface {
1616
// returns current field for validation
1717
Field() reflect.Value
1818

19+
// returns the field's name with the tag
20+
// name takeing precedence over the fields actual name.
21+
FieldName() string
22+
23+
// returns the struct field's name
24+
StructFieldName() string
25+
1926
// returns param for validation against current field
2027
Param() string
2128

@@ -40,12 +47,23 @@ func (v *validate) Field() reflect.Value {
4047
return v.flField
4148
}
4249

50+
// FieldName returns the field's name with the tag
51+
// name takeing precedence over the fields actual name.
52+
func (v *validate) FieldName() string {
53+
return v.cf.altName
54+
}
55+
56+
// StructFieldName returns the struct field's name
57+
func (v *validate) StructFieldName() string {
58+
return v.cf.name
59+
}
60+
4361
// Param returns param for validation against current field
4462
func (v *validate) Param() string {
45-
return v.flParam
63+
return v.ct.param
4664
}
4765

4866
// GetStructFieldOK returns Param returns param for validation against current field
4967
func (v *validate) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
50-
return v.getStructFieldOKInternal(v.slflParent, v.flParam)
68+
return v.getStructFieldOKInternal(v.slflParent, v.ct.param)
5169
}

validator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ type validate struct {
2323
slflParent reflect.Value
2424
slCurrent reflect.Value
2525
flField reflect.Value
26-
flParam string
2726
fldIsPointer bool
27+
cf *cField
28+
ct *cTag
2829

2930
// misc reusable values
3031
misc []byte
@@ -215,7 +216,8 @@ OUTER:
215216
// set Field Level fields
216217
v.slflParent = parent
217218
v.flField = current
218-
v.flParam = ""
219+
v.cf = cf
220+
v.ct = ct
219221

220222
if !v.fldIsPointer && !hasValue(v) {
221223
return
@@ -309,7 +311,8 @@ OUTER:
309311
// set Field Level fields
310312
v.slflParent = parent
311313
v.flField = current
312-
v.flParam = ct.param
314+
v.cf = cf
315+
v.ct = ct
313316

314317
if ct.fn(v) {
315318

@@ -397,7 +400,8 @@ OUTER:
397400
// set Field Level fields
398401
v.slflParent = parent
399402
v.flField = current
400-
v.flParam = ct.param
403+
v.cf = cf
404+
v.ct = ct
401405

402406
// // report error interface functions need these
403407
// v.ns = ns

0 commit comments

Comments
 (0)