Skip to content

Joi.external() error message #2600

@ShyeChern

Description

@ShyeChern

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

How can we help?

Scenario: When user submit input, I will check is it the user id exist in the database. If not exist it will return a custom error message. The following is my sample code snippet.

const userId = Joi.number()
  .integer()
  .required()
  .external(async (value) => {
    let user = await userModel.selectOne({ id: value });
    if (!user) {
      throw new AppError(AppError.INVALID_USER_ID);
    }
  })
  .error(() => { throw new AppError(AppError.INVALID_USER_ID) });

const schema = Joi.object({
      userId
});

schema.validateAsync(userInput);

Expected output:
"message": "Invalid user id."
Actual output:
"message": "Invalid user id." (userId)

It automatically add the field name into my error message. Is there any way to remove it?

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions