How do I override empty rule with messages in JOI 17?
Asked Answered
M

2

1

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.

Meridethmeridian answered 5/2, 2021 at 20:57 Comment(2)
https://mcmap.net/q/244623/-how-to-set-custom-message-for-regex-in-joi, using this answer, you can try finding out what error is being thrown. Then overriding it using .messages().Diminuendo
You can use any.onlyElizebethelizondo
H
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

Hamlani answered 16/6, 2022 at 23:29 Comment(0)
P
0

Use string.empty

const create = validator.object().keys({
  app: validator
   .string()
   .required()
   .valid(...ARRAY_OF_VALUES)
   .messages({'string.empty':'Must pass valid code'})
});
Pastoralist answered 15/2, 2023 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.