Comments (14)
@asaskevich sure, I'll try and do it over the weekend 😃
from govalidator.
We wanted the same thing, we ended up using reflect
to get the validation subject struct fields, extract their json
tags to get the field name we want to return in responses, something like this:
func ValidationError(ctx echo.Context, s interface{}, err error) error {
switch err.(type) {
case govalidator.Errors:
// Use reflect to get the raw struct element
typ := reflect.TypeOf(s).Elem()
if typ.Kind() != reflect.Struct {
return UnexpectedError(ctx, errors.New("validation subject is not a struct"))
}
// This is will contain the errors we return back to user
errs := map[string]string{}
// Errors found by the validator
errsByField := govalidator.ErrorsByField(err.(govalidator.Errors))
// Loop over our struct fields
for i := 0; i < typ.NumField(); i++ {
// Get the field
f := typ.Field(i)
// Do we have an error for the field
e, ok := errsByField[f.Name]
if ok {
// Try and get the `json` struct tag
name := strings.Split(f.Tag.Get("json"), ",")[0]
// If the name is - we should ignore the field
if name == "-" {
continue
}
// If the name is not blank we add it our error map
if name != "" {
errs[name] = e
continue
}
// Finall if all else has failed just add the raw field name to the
// error map
errs[f.Name] = e
}
}
// Return the validation error
mapper := &Errors{Message: "Validation Error", Errors: errs}
return ErrorHandler(422, mapper, ctx)
}
// If the error type is not a govalidator.Errors, return a 500
return UnexpectedError(
ctx,
fmt.Errorf("Expected govalidator.Errors, got %s", reflect.TypeOf(err)))
}
from govalidator.
@krak3n could you put your example into pull request with sample of usage and small note in docs please?
from govalidator.
+1!!!!
from govalidator.
If the intent is make the messages more user friendly I think that would be better use a field for these only purpose.
from govalidator.
Hmmm i see your point and in some ways it is more flexible. But also the the json
tag is what the user will practically see for any restful JSON applications, which I believe will be a alot of use cases.
Could we potentially have some way of specifying a custom tag field?
from govalidator.
Looking forward to this PR.
Is it just going to use the json:"value"
as it's field name, or perhaps different approach will be taken?
Thanks.
from govalidator.
@paroxp I didn't get to it last weekend but hopefully this weekend, it's just going to be documentation how how you could use reflection to get the json
, xml
or what ever other struct
tag to get the field name you want to use for the error value.
from govalidator.
do we have updates or solution?
from govalidator.
What about nested field names?
If we validate the following, we get Name: invalid_alphanum does not validate as alphanum
. However, we cannot identify which failed when nested fields contain the same name.
type Resource struct {
Name string `json:"name" valid:"alphanum"`
Version string `json:"version" valid:"required"`
Env struct {
Name string `json:"name" valid:"alphanum"`
Value string `json:"value" valid:"required"`
}
}
from govalidator.
@retr0h it should return something like Env.Name: invalid_alphanum does not validate as alphanum
or Env.name ...
?
from govalidator.
^ Yes, that would be the expected behavior, however is not the case currently
from govalidator.
from govalidator.
Hello guys!
I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back
Link to my repo: create issue there and we'll discuss it.
from govalidator.
Related Issues (20)
- Validation of a pointer to a variable of type bool now checks the value of the variable, not the pointer HOT 1
- Need to check if a string is word or not
- Need to check if a string is Decimal or not
- Need to check if a string is contained in a csv file or not
- Validate time duration strings
- How to do struct validation for slice of strings
- need to validate ipv4:port string
- IsRequestURI fails for non-absolute URLs
- Custom function validation doesn't work
- How to do custom type conversion
- IsRequestURI fails for IPV6 URLs with interface
- Is this project abandoned? Is there an active fork of this repo? HOT 1
- fatal error: concurrent map writes
- Are there any plans for an updated version recently? HOT 1
- Add filters to transform data after validation
- Choice of tags breaks semantic versioning HOT 2
- `IsHash()` is highly inefficient
- IsURL is accepting "http//" as legitimate URL prefix HOT 2
- Module path is not versioned in go.mod
- Add yyyy-mm-dd validation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from govalidator.