protoc-gen-validate
protoc-gen-validate copied to clipboard
Customized error messages on validation failures
Is it possible to specify a custom message for validation failures? For example, if a min_length validation fails, I would like to return a reason that doesn't reference 'runes'. Or be able to understand the error message returned is a minimum length violation, and translate that to my own messaging.
I don't see a way to do either; i don't see a way to specify a custom message in the validation option in the proto file, and the error returned is specific to the message, but not (even more) specific to the min_len validation.
Thanks!
Hi, is there a way to do this even if this is not yet developed inside protoc-gen-validate.
Hi, is there a way to do this even if this is not yet developed inside protoc-gen-validate.
How ? Could you show me an example?
Hi is this feature available now?
This feature is not available, but we're happy to accept pull requests. The first step is figuring out how we want to represent this in the validation annotations. Any suggestions?
Yeah, I'm definitely interested in this as well. I've been considering 2 approaches:
- Add it in the annotations like @akonradi mentioned. Since you likely want a different message for each kind of failure, you'll have to add a message per constraint. For example,
message Person {
uint64 id = 1 [(validate.rules).uint64 = {gt: 999, gt_message: "value must be higher than %d"}];
string email = 2 [(validate.rules).string = {email: true, email_message: "value must be a valid email, yo"}];
string name = 3 [(validate.rules).string = {
pattern: "^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$",
max_bytes: 256,
pattern_message: "value may contain only letters, numbers, and the following symbols: ',-_.",
}];
Location home = 4 [(validate.rules).message = {required: true, required_message: "there's no place like home"}];
message Location {
double lat = 1 [(validate.rules).double = { gte: -90, lte: 90, gte_message: "lat must be at least %f degrees", lte_message: "lat can't be more than %d degrees" }];
double lng = 2 [(validate.rules).double = { gte: -180, lte: 180, gte_message: "lng must be at least %f degrees", lte_message: "lng can't be more than %d degrees" }];
}
}
- extract message keys into a message store that can be overridden via a config file passed to protoc-gen-validate at generation time. This is basically the same way you typically handle
i18n
and you can probably even use a similar library, like https://github.com/qor/i18n. We would register a function like
func T(key string, value string, args ...interface{}) string {
return I18n.Default(value).T("en-US", key, args...)
}
and then use it in the templates like
{{ t "string.min_len" "value length must be {{$1}} runes" $r.GetMinLen }}
If this isn't overridden in a translation/config.yaml file, it will output the default like value must be 5 runes
. Otherwise, you can add a config file such as
en-US:
string:
min_len: "value length must be {{$1}} characters"
to show something with "characters" instead of "runes" to the end users.
Note that you may need to support both of these options. The first allows you to customize an error message for a specific field; e.g., this is very useful for string regexes where you don't want to show your end users a regex, but something they'll understand. The second option lets you change the "standard" messages to make sense for your end users.
we need this feature, pls review the pr #398 and merge
Very helpful feature, we need this one. Please review the PR #398
Hello, Is the work ongoing on this. This could be ver useful.
Hi ,Is this enhancement can work on some version ?
Need this feature. Any one can help? Thanks
Having this would be really amazing. Please complete this feature. Thanks
Please complete this feature. This feature very useful. Thanks.
it's good feature.
什么时候能加上这个内容~~期待
Anyone knows when this will be done?
This is super useful, one of the most wanted features. Hope it can be merged soon.
This would be very useful.
Please merge this feature. It is very useful.
This one is tricky, there is an ongoing discussion happening between a few of us at buf to make sure this becomes a reality
Please complete this feature. This feature very useful. Thanks.
We need this feature.Please complete this feature.
@elliotmjackson we also need this, so we may be interested in contributing an implementation (if we can agree reasonably quickly on what needs to be done). I can open a new issue if needed.
Please complete this feature. This feature very useful. Thanks.
现在可以用了吗
Please complete this feature. This feature very useful. Thanks.
This would be very useful.
Any news on this feature? Or is it no longer worth hoping ... ((
The lib is already awesome, the only piece missing is this feature...
I had opened https://github.com/bufbuild/protoc-gen-validate/pull/546 but @elliotmjackson closed it with "won't fix" 🤔
现在有这个功能了吗?