protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

Customized error messages on validation failures

Open tflecht opened this issue 6 years ago • 18 comments

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!

tflecht avatar May 09 '18 19:05 tflecht

Hi, is there a way to do this even if this is not yet developed inside protoc-gen-validate.

dharmjit avatar Jan 26 '19 10:01 dharmjit

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?

LDAR2011 avatar Feb 21 '20 12:02 LDAR2011

Hi is this feature available now?

neha1196 avatar Aug 11 '20 19:08 neha1196

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?

akonradi avatar Aug 14 '20 14:08 akonradi

Yeah, I'm definitely interested in this as well. I've been considering 2 approaches:

  1. 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" }];
  }
}
  1. 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.

codyaray avatar Aug 22 '20 04:08 codyaray

we need this feature, pls review the pr #398 and merge

demobin8 avatar Feb 03 '21 02:02 demobin8

Very helpful feature, we need this one. Please review the PR #398

ak89224 avatar Mar 23 '21 08:03 ak89224

Hello, Is the work ongoing on this. This could be ver useful.

dharmjit avatar Jun 21 '21 04:06 dharmjit

Hi ,Is this enhancement can work on some version ?

MikyChow avatar Jul 05 '21 07:07 MikyChow

Need this feature. Any one can help? Thanks

inix avatar Jul 19 '21 09:07 inix

Having this would be really amazing. Please complete this feature. Thanks

HemantNegi avatar Aug 11 '21 17:08 HemantNegi

Please complete this feature. This feature very useful. Thanks.

elricli avatar Oct 14 '21 08:10 elricli

it's good feature.

aveyuan avatar Nov 02 '21 08:11 aveyuan

什么时候能加上这个内容~~期待

wensenz avatar Jan 12 '22 09:01 wensenz

Anyone knows when this will be done?

gsingh-ds avatar Feb 04 '22 10:02 gsingh-ds

This is super useful, one of the most wanted features. Hope it can be merged soon.

mindbergh avatar Feb 28 '22 02:02 mindbergh

This would be very useful.

Shoban05 avatar May 07 '22 19:05 Shoban05

Please merge this feature. It is very useful.

m430 avatar Aug 01 '22 09:08 m430

This one is tricky, there is an ongoing discussion happening between a few of us at buf to make sure this becomes a reality

elliotmjackson avatar Oct 05 '22 14:10 elliotmjackson

Please complete this feature. This feature very useful. Thanks.

shuqingzai avatar Feb 01 '23 15:02 shuqingzai

We need this feature.Please complete this feature.

ZXiangQAQ avatar Feb 13 '23 02:02 ZXiangQAQ

@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.

CAFxX avatar Feb 14 '23 02:02 CAFxX

Please complete this feature. This feature very useful. Thanks.

FarmerChillax avatar Feb 24 '23 09:02 FarmerChillax

现在可以用了吗

StellarGlyph avatar Mar 09 '23 10:03 StellarGlyph

Please complete this feature. This feature very useful. Thanks.

Ystars avatar Mar 10 '23 02:03 Ystars

This would be very useful.

marthjod avatar Apr 12 '23 14:04 marthjod

Any news on this feature? Or is it no longer worth hoping ... ((

shurco avatar Apr 14 '23 15:04 shurco

The lib is already awesome, the only piece missing is this feature...

pauloeduardoferreira avatar Apr 15 '23 20:04 pauloeduardoferreira

I had opened https://github.com/bufbuild/protoc-gen-validate/pull/546 but @elliotmjackson closed it with "won't fix" 🤔

miparnisari avatar May 19 '23 22:05 miparnisari

现在有这个功能了吗?

tan-zhuo avatar Jun 05 '23 01:06 tan-zhuo