简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM