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 and uuid4_rfc4122 failing with postgres uuids #902

Closed
2 tasks done
FM1337 opened this issue Feb 20, 2022 · 5 comments
Closed
2 tasks done

uuid4 and uuid4_rfc4122 failing with postgres uuids #902

FM1337 opened this issue Feb 20, 2022 · 5 comments

Comments

@FM1337
Copy link

FM1337 commented Feb 20, 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 (v10.10.0)

Issue, Question or Enhancement:

I have two UUIDs here generated by Postgres

ab8dc25f-2e84-4711-afbc-152cf0b9869c
f8a64ef0-232e-4fb8-9949-f972494869a4

I attempted to validate using the uuid4 format rule, but after that didn't work, I tried uuid4_rfc4122, and neither one passed validation

Code sample, to showcase or reproduce:

type JobForm struct {
	Ores                     []string  `form:"ores" json:"ores" validate:"required,min=1,uuid4"`
	OreAmounts               []int     `form:"ore_amounts" json:"ore_amounts" validate:"required,min=1"`
	Participants             []string  `form:"participants" json:"participants" validate:"required,min=1,uuid4_rfc4122"`
}


func ValidateIncomingData(data interface{}) bool {
	validate := validator.New()
	err := validate.Struct(data)

	if err != nil {
                // CODE TO OUTPUT ERROR TO CONSOLE OR RETURN AS RESPONSE HERE
		return false
	}

	return true
}


func main() {
      data := &JobForm{}
     // [CODE TO RECEIVE DATA AND UNMARSHAL IT INTO THE STRUCT HERE]
      ValidateIncomingData(data)
}

I attempted upper casing them for rfc4122 as well, as according to the docs
This validates that a string value contains a valid version 4 UUID. Uppercase UUID values will not pass - use uuid4_rfc4122 instead.

I tried both form-data, x-www-form-urlencoded and in json as the data sent to the server, and even though it successfully decodes each time, it always fails validation on those rules.

(Errors outputted)

Key: 'JobForm.Ores' Error:Field validation for 'Ores' failed on the 'uuid4' tag
Key: 'JobForm.Participants' Error:Field validation for 'Participants' failed on the 'uuid4_rfc4122' tag

I decoded them online to verify their versioning
image
image

I understand that there's already another issue like this open, #900 but that's attempting to use the google.UUID library, whereas I'm simply treating it as a string.

This is the output of the parsed struct I'm trying to validate

&{[ab8dc25f-2e84-4711-afbc-152cf0b9869c] [3000] [f8a64ef0-232e-4fb8-9949-f972494869a4]}

So there's nothing wrong with how I'm receiving the data from the HTTP request, and it appears to be some issues with some of the validation rules (also noticed an issue with datetime but that's for another issue, not this one)

Also according to postgres docs:
The data type uuid stores Universally Unique Identifiers (UUID) as defined by RFC 4122, ISO/IEC 9834-8:2005, and related standards.

The UUIDs were generated using gen_random_uuid()

@FM1337
Copy link
Author

FM1337 commented Feb 20, 2022

I'm dumb. Didn't realize I needed to use dive on it.

@FM1337 FM1337 closed this as completed Feb 20, 2022
@deankarn
Copy link
Contributor

@FM1337 If you look closely at your code you will see you are trying to apply the uuid validation against the type []string which is why it is correctly failing.

If you wish to apply a validation to the inner values of an array/slice you must call dive first

@deankarn
Copy link
Contributor

Ah beat me to it.

@FM1337
Copy link
Author

FM1337 commented Feb 20, 2022

@deankarn yeah sorry about that, walking away to get some lunch helped clear my mind and figure the issue out. Nothing in the README.md though mentioning to use dive for situations like this, I know examples has something but perhaps an update to the README.md to mention it might help?

@deankarn
Copy link
Contributor

Happy to accept a PR to improve the docs.

it may not be in the README but it should be in the godocs and in one of the examples as well.

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

No branches or pull requests

2 participants