Skip to content
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

Customizing error messages #2146

Closed
jangeorgiev opened this issue Dec 12, 2018 · 12 comments
Closed

Customizing error messages #2146

jangeorgiev opened this issue Dec 12, 2018 · 12 comments
Labels
developer-experience Issues affecting ease of use and overall experience of LB users help wanted needs doc Validation

Comments

@jangeorgiev
Copy link

Feature proposal

I have noticed that currently there are no way of customizing error messages returned from AJV in LB4.
I have found that there is implementation of handling error message already - AJV-Errors.
I'll be very happy to be able to customize error messages, because some of them are extremely ugly.

Example:

See code here

The code above in case of violation of patter will return
"should match pattern "^[a-z]+$""

@shadyanwar
Copy link

Related to #1867

@dhmlau
Copy link
Member

dhmlau commented Dec 12, 2018

@jangeorgiev , is #1867 that @shadyanwar mentioned above something you're looking for? If yes, I'd like to continue the discussion over there instead. Thanks.

@dhmlau dhmlau self-assigned this Dec 13, 2018
@jangeorgiev
Copy link
Author

No, it's not related to custom errors that are returned from HttpErrors.UnprocessableEntity().
I talk about customizing error messages of predefined validation rules see all rules here

@TomerSalton
Copy link

@YaelGit might be relevant to you?

@bajtos bajtos added the developer-experience Issues affecting ease of use and overall experience of LB users label Jan 25, 2019
@bajtos
Copy link
Member

bajtos commented Jan 25, 2019

Based on the issue description and the discussion, I see two incremental steps we can make:

  1. Change our validation layer to use ajv-errors to produce the error message, instead of relying on the built-in ajv messages.

  2. Create an extensibility point allowing LB4 applications to further customize the error messages.

  3. Probably out of scope of this issue, just something to keep in consideration: localize error messages to a different langauge based on server ENV variables and/or request headers.

Does it look like a reasonable plan to you?

I don't think this will become a priority for us in the next 3-6 months, so it's up to you to contribute these improvements. I am happy to help you along the way if you decide so.

See https://loopback.io/doc/en/contrib/code-contrib.html to get started.

@dhmlau dhmlau removed their assignment Apr 2, 2019
@shadyanwar
Copy link

Change our validation layer to use ajv-errors to produce the error message, instead of relying on the built-in ajv messages.

@bajtos I hope to find time next weekend to work on this. It would be nice if you tell me where to start.

@bajtos
Copy link
Member

bajtos commented Jun 25, 2019

@AaronJan
Copy link
Contributor

Hi @bajtos, I made a PR (#3746 ), advice needed!

@dhmlau
Copy link
Member

dhmlau commented Oct 24, 2019

@jangeorgiev, is this issue good to close? Thanks.

@jangeorgiev
Copy link
Author

Hey @dhmlau, thanks for asking.

I updated my project to last version and I tried to use custom error messages as described in AJV Documentation, but it didn't worked.
Should I install some additional packages (i.e. "ajv-i18n") ?
Actually, could you or @AaronJan please show me an example how to do it in your way ?

Thanks in advance.

@AaronJan
Copy link
Contributor

@jangeorgiev 😄 No problem!

You have to install ajv-i18n first, then put this code in your Application:

const localize = require('ajv-i18n');

export class MyApplication {
  constructor() {
  // ...

  this.bind(RestBindings.REQUEST_BODY_PARSER_OPTIONS).to({
      validation: {
        ajvErrorTransformer: (errors: AJV.ErrorObject[]): AJV.ErrorObject[] => {
          // `.zh()` is for Chinese, for other languages check here: https://github.com/epoberezkin/ajv-i18n#supported-locales
          localize.zh(errors);

          // Because the translation function is mutable, you have to return the original object.
          return errors;
        },
      },
    });
  }
}

@jangeorgiev
Copy link
Author

Great! The example works perfect!

Still I have no idea how to integrate ajv-errors, but it's not big deal for me, I'll just write work-around thanks to ajvErrorTransformer.

Thanks for hard work guys !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer-experience Issues affecting ease of use and overall experience of LB users help wanted needs doc Validation
Projects
None yet
Development

No branches or pull requests

6 participants