Skip to content
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

Open
polRk opened this issue Feb 16, 2022 · 8 comments
Open

uuid4 doesn't works with google uuid.UUID #900

polRk opened this issue Feb 16, 2022 · 8 comments

Comments

@polRk
Copy link

polRk commented Feb 16, 2022

  • [+] I have looked at the documentation here first?
  • [+] I have looked at the examples provided that may showcase my question here?

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:

type getContactRequest struct {
	ID uuid.UUID `uri:"id" binding:"required,uuid4"`
}

If i pass

type getContactRequest struct {
	ID string `uri:"id" binding:"required,uuid4"`
}
``` - It works
@zemzale
Copy link
Member

zemzale commented Feb 16, 2022

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
and https://pkg.go.dev/github.com/go-playground/validator/v10#CustomTypeFunc and https://pkg.go.dev/github.com/go-playground/validator/v10#Validate.RegisterCustomTypeFunc

@polRk
Copy link
Author

polRk commented Feb 16, 2022

Thank you!

@polRk polRk closed this as completed Feb 16, 2022
@polRk
Copy link
Author

polRk commented Feb 19, 2022

It doesn't work

["cb1cdadb-a93c-487b-be62-4963cef4600b"] is not valid value for uuid.UUID
	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
}

@polRk polRk reopened this Feb 19, 2022
@polRk
Copy link
Author

polRk commented Feb 19, 2022

I think, problem with ShouldBindUri, because with ShouldBindJSON works good

@zemzale
Copy link
Member

zemzale commented Feb 19, 2022

That error is not with this library, but https://github.com/gin-gonic/gin so you should take the issue there.

@deankarn
Copy link
Contributor

As an aside I think that we can enhance the uuid validation to check if the type can be cast to a Stringer and if so call that function. Since most, if not all, UUID types have this function.

type Stringer interface {
    String() string
}

I'd be happy to accept a PR for that. Then no custom functions would be required.

@vigo
Copy link

vigo commented Oct 24, 2023

it works with strings only and it works like a charm!

@JoshGlazebrook
Copy link
Contributor

@deankarn ^ I went ahead and did my attempt at adding this in #1189

deankarn pushed a commit that referenced this issue Nov 4, 2023
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants