-
Notifications
You must be signed in to change notification settings - Fork 29
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
Custom error messages and localization #7
Comments
There are at least two issues which I believe to be pre-requisites: (1) because (2) because the current error representation is somewhat inflexible, and adding a custom message would mean emitting These two issues would require pretty significant changes in both |
I think this and #1 can be combined into a single design. There are two use cases for "custom messages":
(1) would address #63, because you could use this to output the value together with the other errors. (2) could address #1. We would special case the output of the proc macro to not actually produce any errors in this case, and immediately stop all further validation when any of the rules fail. This is fail-fast validation. We could then support using the custom message attribute on both field and struct levels. By using a "Replace" custom message on the struct level, you would have fail-fast validation for the entire struct. This would require reworking all the rules to support two modes:
So that we can fully skip producing an error in case we don't need it. The proc macro will also need to support emitting the field-level validation rules in both fail-fast and error aggregate modes. One limitation is that this would all be encoded in the derived |
i would love to have this feature. Right now we ended up using custom validator to make custom error message. If I want to help is there's anything that I could do? |
I think adding a comment here with a code snippet showing how you'd use this feature would be super helpful! Depending on what exactly you need out of this feature, there may be a simpler "MVP" of #7 (comment) that would not be too difficult to implement. It would consist of:
|
I'm planning to migrate from I'd like to apply i18n on the client side, based on pre-defined error codes that maps to messages with interpolated parameters, like max and min. For example this is how I'm returning error codes to clients: https://github.com/lasantosr/error-info |
I want to avoid adding As for adding support for |
@lasantosr I have it like this, with code, message and path:
And the method is like so:
I hope it satisfies your need. |
That's a first step, the problem is if the i18n message associated to the code is something like: "The value must not exceed {max}" The params are needed in those cases, but I agree that's an overhead not required for every use case, so it can be an opt-in parameter on the struct level macro.
A field might have different validation rules associated, so I would like to have a different code for each. Instead of a generic "username.invalid" I'd like to have "username.invalid_length" and "username.invalid_characters" for example. |
So it can be two extra properties in the
or
Which can be customized via attributes. |
This should be addressed by: |
Totally missed that one! That would be enough, along with this message customization and the custom error codes |
A rule for custom error messages:
Implementation detail: The string version should differentiate between a const string and a format string. It might make sense to generate a temporary function for format strings which does the actual formatting to enforce encapsulation.
Combined with a custom
Context
, it should be possible to use this to localize error messages:But it might make sense to support this directly via a
translate
rule which passes the error message to be further localized - needs some design work.The text was updated successfully, but these errors were encountered: