-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
uuid4 doesn't works with google uuid.UUID #900
Comments
Hey @polRk The validator works only for string types out of the box. But you can register a CustomTypeFunc. It would look something like this: package main
import (
"fmt"
"reflect"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
)
type getContactRequest struct {
ID uuid.UUID `uri:"id" validate:"required,uuid4"`
}
// use a single instance of Validate, it caches struct info
var validate *validator.Validate
func main() {
validate = validator.New()
// register all sql.Null* types to use the ValidateValuer CustomTypeFunc
validate.RegisterCustomTypeFunc(ValidateUUID, uuid.UUID{})
// build object for validation
x := getContactRequest{ID: uuid.New()}
err := validate.Struct(x)
if err != nil {
fmt.Printf("Err(s):\n%+v\n", err)
}
}
// ValidateUUID implements validator.CustomTypeFunc
func ValidateUUID(field reflect.Value) interface{} {
if valuer, ok := field.Interface().(uuid.UUID); ok {
return valuer.String()
}
return nil
} See more at : https://github.com/go-playground/validator/blob/master/_examples/custom/main.go |
Thank you! |
It doesn't work
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterCustomTypeFunc(ValidateUUID, uuid.UUID{})
v.RegisterCustomTypeFunc(ValidateNullable, uuid.NullUUID{}, nullable.NullString{}, nullable.NullTime{})
} func ValidateUUID(field reflect.Value) interface{} {
if valuer, ok := field.Interface().(uuid.UUID); ok {
return valuer.String()
}
return nil
} func ValidateNullable(field reflect.Value) interface{} {
if valuer, ok := field.Interface().(driver.Valuer); ok {
val, err := valuer.Value()
if err == nil {
return val
}
}
return nil
} |
I think, problem with |
That error is not with this library, but https://github.com/gin-gonic/gin so you should take the issue there. |
As an aside I think that we can enhance the type Stringer interface {
String() string
} I'd be happy to accept a PR for that. Then no custom functions would be required. |
it works with strings only and it works like a charm! |
… implement the Stringer interface. (#1189) ## Fixes Or Enhances This adds the ability to validate UUIDs that have an underlying struct value but also implement the Stringer interface. This should cover most UUID implementations (google's uuid, etc). Implements: #900, specifically #900 (comment). **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
Got an error:
["3dcf8eb3-4489-4777-af93-2ba289316e77"] is not valid value for uuid.UUID
Code sample, to showcase or reproduce:
If i pass
The text was updated successfully, but these errors were encountered: