-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
[question]: Validation for string-based types in Enum has unexpected results. #600
Comments
@superstas this is an interesting problem you ran into! First, you are very close to the fix. I think something like this should work: s.Enum = []interface{}{string(NameTypeFoo), string(NameTypeBar)} Now for the explanation and what we could do about it. When a request comes in to be validated it actually gets unmarshaled into So on that first pass, you have a type NameType string
const NameTypeFoo NameType = "FOO"
func main() {
// Note: value is `any` otherwise the compiler will see the mismatched types,
// we need to trigger the check at runtime instead.
var value any = "FOO"
fmt.Println(value == NameTypeFoo) // prints false
} https://go.dev/play/p/0pwZcS0Wy01 One possible way to fix this is to check if they are the same type, and if not, try to cast one to the other. In this case since The problem though is that check is a bit hard to do and slow (and may require reflection), so honestly I would rather users create enums using the expected incoming type (in this case |
First of all, thank you, Daniel, for the quick reply and a very detailed explanation.
As for the digging into it.
Since I expect the value of From DX's perspective, something like From the implementation point of view, I don't see a good and reasonable solution for now. Mentioning this possible issue in the Doc/GoDoc is probably the best option for now (until the number of similar problems/questions from users increases). |
Hi there,
I have the following types
main.go
I have two string-type-based constants:
NameTypeFoo
andNameTypeBar
(FOO
andBAR
respectively ).I set
Enum
inTransformSchema
bys.Enum = []interface{}{NameTypeFoo, NameTypeBar}
.After that, I get the correct OAS
spec
where
enum
is correctI expect the request with
FOO
orBAR
to pass the validation.The issue
main_test.go
I get a
422
error with the correctFOO
( orBAR
) value on the request.My question is it a bug or a wrong way to set
Enum
inTransformSchema
?If I set it as strings
s.Enum = []interface{}{"FOO", "BAR"}
everything works as expected.Thank you.
The text was updated successfully, but these errors were encountered: