Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Customized error messages on validation failures #74

Closed
tflecht opened this issue May 9, 2018 · 39 comments
Closed

Customized error messages on validation failures #74

tflecht opened this issue May 9, 2018 · 39 comments
Labels
Enhancement Extend or improve functionality Wont Fix Out of scope of this project

Comments

@tflecht
Copy link

tflecht commented May 9, 2018

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!

@rodaine rodaine added the Enhancement Extend or improve functionality label Nov 1, 2018
@dharmjit
Copy link

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

@LDAR2011
Copy link

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?

@neha1196
Copy link

Hi is this feature available now?

@akonradi
Copy link
Contributor

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?

@codyaray
Copy link
Contributor

codyaray commented Aug 22, 2020

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.

@demobin8
Copy link

demobin8 commented Feb 3, 2021

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

@ak89224
Copy link

ak89224 commented Mar 23, 2021

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

akavel pushed a commit to akavel/protoc-gen-validate that referenced this issue Apr 1, 2021
Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>
akavel pushed a commit to akavel/protoc-gen-validate that referenced this issue Apr 1, 2021
After modifying the harness.proto in an earlier commit, non-Go harnesses
broke and need to be adjusted.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>
akavel pushed a commit to akavel/protoc-gen-validate that referenced this issue Apr 1, 2021
After modifying the harness.proto in an earlier commit, non-Go harnesses
broke and need to be adjusted.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>
akavel pushed a commit to akavel/protoc-gen-validate that referenced this issue Apr 1, 2021
After modifying the harness.proto in an earlier commit, non-Go harnesses
broke and need to be adjusted.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>
akonradi pushed a commit that referenced this issue Apr 1, 2021
* go: add bool arg to Validate (#47)

In generated messages, modify the Validate func to take bool argument.
In a later commit, this will be used to distinguish "fail on first
error" mode from "extensively find as many as possible errors".

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* go: enable multi-error validation (#47)

Modify the templates to allow retrieving multiple errors from a Validate
func, not just stopping on the first one.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* tests: prepare harness for multi-errors (#47)

In test harness executor, add support for languages which can find
multiple validation errors at once.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* go: test multi-error validation (#47)

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* go tests: check if multi-error was returned (#47)

Ensure that a non-nil response from Validate(true) always fulfills the
"multi-error" interface (has AllErrors() method).

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* go tests: compare 1st msg of multi-error (#47)

Compare the first message returned by Validate(true) to the one returned
by Validate(false).

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* go tests: repeated TestResult.Reasons field (#74)

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>

* tests: fix TestResult.Reasons in non-Go tests (#74)

After modifying the harness.proto in an earlier commit, non-Go harnesses
broke and need to be adjusted.

Signed-off-by: Mateusz Czapliński <mateusz.czaplinski@wpengine.com>
@dharmjit
Copy link

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

@MikyChow
Copy link

MikyChow commented Jul 5, 2021

Hi ,Is this enhancement can work on some version ?

@rodaine rodaine added Good First Issue Newcomer-friendly issue Help Wanted Community support requested labels Jul 7, 2021
@inix
Copy link

inix commented Jul 19, 2021

Need this feature. Any one can help?
Thanks

@HemantNegi
Copy link

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

@elricli
Copy link

elricli commented Oct 14, 2021

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

@aveyuan
Copy link

aveyuan commented Nov 2, 2021

it's good feature.

@wensenz
Copy link

wensenz commented Jan 12, 2022

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

@gsingh-ds
Copy link

Anyone knows when this will be done?

@mindbergh
Copy link

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

@elliotmjackson
Copy link
Contributor

elliotmjackson commented Oct 5, 2022

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

@shuqingzai
Copy link

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

@ZXiangQAQ
Copy link

We need this feature.Please complete this feature.

@CAFxX
Copy link

CAFxX commented Feb 14, 2023

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

@FarmerChillax
Copy link

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

@Jasminahs
Copy link

现在可以用了吗

@Ystars
Copy link

Ystars commented Mar 10, 2023

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

@marthjod
Copy link

This would be very useful.

@shurco
Copy link

shurco commented Apr 14, 2023

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

@pauloeduardoferreira
Copy link

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

@miparnisari
Copy link

I had opened #546 but @elliotmjackson closed it with "won't fix" 🤔

@tan-zhuo
Copy link

tan-zhuo commented Jun 5, 2023

现在有这个功能了吗?

1 similar comment
@38888
Copy link

38888 commented Jul 12, 2023

现在有这个功能了吗?

@hyue418
Copy link

hyue418 commented Aug 3, 2023

This issue has been around for 5 years. What is the reason why custom error messages are not supported? This has brought us a lot of trouble and may be the reason why we give it up.

@zhangyanglei
Copy link

i have given up because it

@hyue418
Copy link

hyue418 commented Aug 8, 2023

i have given up because it

Me too. Is there a better alternative?

@zhangyanglei
Copy link

i have given up because it

Me too. Is there a better alternative?

i use protoc-go-inject-tag and struct validator instead

@derekperkins
Copy link

Have you looked at https://github.com/bufbuild/protovalidate?

@zhangyanglei
Copy link

let me try, tks

@elliotmjackson
Copy link
Contributor

elliotmjackson commented Aug 10, 2023

Good news! this is currently available in the protovalidate project.

I'd like to clarify that this feature won't be implemented in protoc-gen-validate, as our focus is now on protovalidate to offer an enhanced and more comprehensive experience within the Protobuf ecosystem.

Given that you can achieve your desired functionality through protovalidate, I will be closing this issue. If you have more questions or suggestions, please don't hesitate to share them with us.

@gonglf1212
Copy link

i need it

@bufbuild bufbuild locked as resolved and limited conversation to collaborators Dec 23, 2023
@rodaine rodaine closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2023
@rodaine rodaine added Wont Fix Out of scope of this project and removed Help Wanted Community support requested Good First Issue Newcomer-friendly issue labels Dec 23, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Enhancement Extend or improve functionality Wont Fix Out of scope of this project
Projects
None yet
Development

No branches or pull requests