-
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
handle pq.StringArray with isUnique #1335
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are few issues with the PR:
- We wont add support for custom types from libraries, because that just opens the flood gates to add types from every library under the sun
- There are no tests to cover this change
- And please open a issue before you make a PR to actually see if your idea is valid for a change
@@ -8,6 +8,7 @@ require ( | |||
github.com/go-playground/locales v0.14.1 | |||
github.com/go-playground/universal-translator v0.18.1 | |||
github.com/leodido/go-urn v1.4.0 | |||
github.com/lib/pq v1.10.9 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no plans to support external libraries, and their custom types, so this is a no-go.
@@ -326,6 +327,11 @@ func isUnique(fl FieldLevel) bool { | |||
|
|||
switch field.Kind() { | |||
case reflect.Slice, reflect.Array: | |||
// If it's a pq.StringArray, convert it to a []string for easier handling | |||
if field.Type() == reflect.TypeOf(pq.StringArray{}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are not planning to support custom types from all kinds of libraries this is not going to work.
BUT it's possible to get the same kind of functionality by using the ConvertTo
API to check if that field can be converted to a string slice https://pkg.go.dev/reflect#Value.CanConvert
Everything @zemzale stated plus this is already supported today registering a custom type function, here is an example using the std libraries SQL types https://github.com/go-playground/validator/blob/master/_examples/custom/main.go#L26 |
Fixes Or Enhances
Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers