Loopback-next: Customizing error messages

Created on 12 Dec 2018  路  12Comments  路  Source: strongloop/loopback-next

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]+$\""

Validation developer-experience help wanted needs doc

Most helpful comment

@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;
        },
      },
    });
  }
}

All 12 comments

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

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

@YaelGit might be relevant to you?

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.

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.

@shadyanwar that's great to hear!

Here is the code converting AJV validation errors to an HttpError instance:

https://github.com/strongloop/loopback-next/blob/27b6accace55761d8db15442e5ca7705bab7d9fc/packages/rest/src/validation/request-body.validator.ts#L128-L137

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

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

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.

@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;
        },
      },
    });
  }
}

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 !

Was this page helpful?
0 / 5 - 0 ratings