-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45dd662
commit e3e0abf
Showing
5 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package validate | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/go-playground/validator/v10" | ||
"github.com/zeiss/pkg/slices" | ||
) | ||
|
||
// Errors is a map of field names to error messages. | ||
type Errors validator.ValidationErrors | ||
|
||
// Field returns the error message for the provided field. | ||
func (e Errors) Field(name string) string { | ||
for _, err := range e { | ||
if err.Field() == name { | ||
return err.Error() | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// TagNameFunc returns the tag name for the provided field. | ||
func TagNameFunc(fld reflect.StructField) string { | ||
name := slices.First(strings.SplitN(fld.Tag.Get("json"), ",", 2)...) | ||
|
||
if name == "-" { | ||
return "" | ||
} | ||
|
||
return name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters