2

In my model, I have defined a model schema. One of the property is the name which is a required field and minimum character should be of length 5.

  @property({
    type: 'string',
    required: true,
    jsonSchema: {
      title: 'name',
      minLength: 5,
      maxLength: 64,
    },
  })
  name: string;

But the issue is the error message I get if validation for name fails is (i.e. I pass less than 5 characters),

{
    "error": {
        "statusCode": 422,
        "name": "UnprocessableEntityError",
        "message": "should NOT be shorter than 5 characters"
    }
}

How can I define a custom error message?

Rifa Achrinza
  • 1,555
  • 9
  • 19
prisoner_of_azkaban
  • 700
  • 1
  • 8
  • 26

3 Answers3

4

I found it in loopback4 documentation that you can define customer error message in jsonSchema object itself. Please find below code,

@property({
   name: 'name',
   description: "The product's common name.",
   type: 'string',
   // Specify the JSON validation rules here
   jsonSchema: {
      maxLength: 30,
      minLength: 10,
      errorMessage: 'name must be at least 10 characters and maximum 30 characters',
   },
})

Goto https://loopback.io/doc/en/lb4/Model.html#id-properties and search for section,

Validation Rules

You will get this property to set custom error message. I checked and its working fine!

Thanks!!

Aman Chawla
  • 68
  • 1
  • 7
0

Hello from the LoopBack team

I am able to reproduce your scenario using LoopBack's Todo example app. Here is the full error response I am seeing:

{
  "error": {
    "statusCode": 422,
    "name": "UnprocessableEntityError",
    "message": "The request body is invalid. See error object `details` property for more info.",
    "code": "VALIDATION_FAILED",
    "details": [
      {
        "path": "/name",
        "code": "minLength",
        "message": "should NOT be shorter than 5 characters",
        "info": {
          "limit": 5
        }
      }
    ]
  }
}

As you can see, the details field contains machine-readable information that can be used by clients to render a human-friendly error message:

  • The path field is pointing to the (deep) property that did not pass validation (name).
  • The code field contains the validation rule that was violated (minLength).
  • info.limit provides additional information about the validation rule.

The error message you posted does not look familiar to me. Did you configure a custom error handler in your application (see Handling errors in Sequence)? Are you perhaps using an older version of @loopback/rest? Can you please update your LoopBack dependencies to their latest versions and see if you get more helpful error messages?

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99
0

To customize the error message, there's a section in https://loopback.io/doc/en/lb4/Validation-REST-layer.html#customize-validation-errors related to this.

You can also see a running app in: https://github.com/strongloop/loopback-next/blob/master/examples/validation-app/src/sequence.ts.

Diana
  • 311
  • 1
  • 3