-
Notifications
You must be signed in to change notification settings - Fork 578
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
Omitempty? Or some way to not validate default/zero-value? #102
Comments
Hey! Thanks for the report. This is pretty much a generalization of #98, and totally a reasonable addition. If you can, the WKTs carry more semantic weight in describing an optional field, but understandably it makes the data structure more complex. |
@rodaine Thx, but two questions:
|
|
Thanks. |
Hey, just wondering if this might get prioritized (@rodaine )? |
This can be sometimes workarounded with a pattern comparison, I've managed to do it with the rule |
Hello everyone, The problem I see here is that all fields in protobuf v3 are optional but protoc-gen-validate already treats any field that has validation rule as required. However there's already Example: string id = 1 [
(validate.rules).message.required = false,
(validate.rules).string.min_len = 1
]; Can be interepreted like "if field is present then its min length must be greater or equal to 1" string id = 1 [
(validate.rules).message.omitempty = false,
(validate.rules).string.min_len = 1
]; |
Backwards compatibility wise |
@alexykot I think the safest option is to introduce another attribute for this purpose. |
The lack of this feature pretty much means protoc-gen-validate is near-useless to anyone using protobuf v3. Just about anyone building a modern api around protobuf v3 will accept missing fields and default/zero values and treat them as such. Any useful protobuf validation library needs to be able to "validate a field is empty (the default/zero value) or has some property". I'm not as concerned about the specifics yet as I am on just getting the authors on board with this in general. |
100%, I just stumbled upon this. Sorry, but this is really unacceptable behavior for this type of project.
Though I see this issue is now unaswered for a year so I'll move to another project. |
@veqryn @nonpcnpc @alexykot |
@hexdigest thanks but i already wrote my own protoc generator :) with blackjack and ... |
@nonpcnpc I don't see any commits in your forked repo can you please point me to the changes you made? |
I ended up writing a new one from scratch. This one and mwitkow's, or any other pb validators, suffer from the simple fact that they have predetermined list of built-in validators. So I have wrote protoc generator that uses closures instead. It is go-only, but that is all I need. |
Duplicating discussion from #283:
I'm open to suggestions? Something like |
I think |
Has there been any movement on this recently? I would very much like to validate a UUID string field that accepts a UUID or |
Just about every single validation library I've seen in golang has let me opt out of validating a field if it is empty / the zero-value.
For example, here is what several common validation libraries use:
This means:
Validate ID is length 32 and not empty.
If CountryCode is not empty, validate the length is 2.
If Size is not zero, validate the length is greater than 10.
Can we please have some way to specify, on every single validation option and type you have, to ignore the zero-value?
This is the only thing stopping me from making full use of this library.
My use case is that I serve GRPC with the REST Gateway, and I have several proto messages that have both optional (zero value = ok and ignored) and required parameters. For the optional parameters, I'd still like to validate them if they aren't empty.
Proposed format:
The text was updated successfully, but these errors were encountered: