0

I read Node.js + Joi how to display a custom error messages? and am still having trouble.

I have a schema like this:

const create = validator.object().keys({
  app: validator
    .string()
    .required()
    .valid(...ARRAY_OF_VALUES)
    .messages({'any.unknown':'Must pass valid code'})
});

An update of the question above points at https://github.com/sideway/joi/blob/master/API.md#list-of-errors for the valid error types.

I test with a value of invalid! and still see the default error message. I have tried string.unknown, string.invalid, any.invalid to no avail.

Dave Stein
  • 8,653
  • 13
  • 56
  • 104

2 Answers2

0

If you want to display a custom error message regardless of error type, you can use '*' instead of 'any.unknown' as follows:

const create = validator.object().keys({
  app: validator
    .string()
    .required()
    .valid(...ARRAY_OF_VALUES)
    .messages({'*':'Must pass valid code'})
});

For more information, check the description for the messages function here: https://joi.dev/api/?v=17.6.0#anyvalidatevalue-options

node-one
  • 69
  • 1
  • 9
0

Use string.empty

const create = validator.object().keys({
  app: validator
   .string()
   .required()
   .valid(...ARRAY_OF_VALUES)
   .messages({'string.empty':'Must pass valid code'})
});
Adrian
  • 1