-
Notifications
You must be signed in to change notification settings - Fork 581
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
Sharing validation rules in a project #405
Comments
We also would very much like something to improve the status quo for extending well-known types. For example, we are using ksuid instead of UUIDs - currently we can put a regexp for every ksuid field, but it would be great to have an extension point for In the Go ecosystem, we can manually implement the Validate() interface if the field is an message type, but many of our use cases are for scalar types. |
Any update on this? This would be highly desirable. In our proto files we have repetitive scalars (validate.rules).string.pattern regex checks for safe chars (^[0-9a-zA-Z.\-/() |
I put together an example of what this API could look like in protobuf using stricter typing. changes that would need to be made to extend google.protobuf.EnumValueOptions {
optional FieldRules custom_rules = 1071;
}
message FieldRules {
...
//add new oneof value for custom wellknown
google.protobuf.Any custom_well_known = 23;
}
}
example of what it would look like to define custom well know rules message CustomRules {
enum Rule {
SEMVER = 0[
(validate.custom_rules).string = {
pattern: "^(\\d)+\\.(\\d)+\\.(\\d)+$"
min_len: 3
}
];
LONG_ID = 1[
(validate.custom_rules).string = {
min_len: 100
}
];
}
repeated Rule rule = 1;
}
what the usage would look like from the message type message ExampleUsage {
string some_field = 1 [
(validate.rules).custom_well_known = {
[type.googleapis.com/my.packge.v1.CustomRules]:{
rule: SEMVER
rule: LONG_ID
}
}
];
} Using the |
Thank you for highlighting the importance of sharing validation rules for reusing custom validation rules within a project. This is an area of interest for us as we continue to enhance the capabilities of bufbuild/protovalidate#51. Your input is greatly appreciated, and it aligns with our goals for future developments. As a result, I'll be closing this issue while keeping your suggestion in mind. Should you have more suggestions or questions down the line, don't hesitate to get in touch. Your involvement is invaluable! |
Another way to think about this might be how to allow custom "well-known rules".
Primarily I'm interested in sharing regex patterns in a larger project without needing to take the route of upstreaming all well-known types.
I know this gets at the heart of protobuf's lack of string constants (or constants of any kind).
Here's the sketch of one approach (that would require some changes to how validators work). I've thought this through for java. Generally, you can think of this approach as delegating field validation to another validator.
Provide a family of rules (e.g.
(validate.rules).TYPE.like = "fully.qualified.Message.field_name"
that takes a string. The string references a specific message field defined elsewhere in the project that has PGV rules applied to it.In the generated java code we now have 2 generated validators one for the prototypical message and another for the message annotated with
(validate.rules).TYPE.like
. Assuming we generate individual methods for each field, thelike
rule validator can delegate checking.Example:
At this point, in the java generated code at least, we have
CommonValidator
and aRequestValidator
. If we enhance the*Validator
code generation to allow individual field validation methods,RequestValidator
can validateid
by callingCommonValidator.validateId
(or whatever the per-field methods get called).Performance considerations:
Usability considerations:
(validate.rules).string.like = "acme.common.Common.id"
Could instead be expressed aslike = {message: "acme.common.Common", field: "id"}
to more closely match theMessageDescriptor
+FieldDescriptor
.protoc -I
import path to ensure that both validators are generated. This is a usability optimization though since if the common validator isn't on the protoc path, the generated code will fail to compile when trying to reference the common validator.The text was updated successfully, but these errors were encountered: