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

handle pq.StringArray with isUnique #1335

Closed
wants to merge 1 commit into from

Conversation

lukasz0707
Copy link

@lukasz0707 lukasz0707 commented Nov 26, 2024

Fixes Or Enhances

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

@lukasz0707 lukasz0707 requested a review from a team as a code owner November 26, 2024 21:07
@coveralls
Copy link

Coverage Status

coverage: 74.312% (-0.006%) from 74.318%
when pulling 359fe50 on lukasz0707:isUniqueFeature
into 6c3307e on go-playground:master.

Copy link
Member

@zemzale zemzale left a 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
Copy link
Member

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{}) {
Copy link
Member

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

@deankarn
Copy link
Contributor

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

@lukasz0707 lukasz0707 closed this Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants