joi icon indicating copy to clipboard operation
joi copied to clipboard

Set both custom error code and custom message inside custom function

Open nolimitdev opened this issue 1 year ago • 0 comments

Runtime

n/a

Runtime version

n/a

Module version

n/a

Used with

No response

Any other relevant information

No response

How can we help?


const myCustomFn = (value, helpers) => {
    // If condition is matched I want to set both code 'my.error.code' and message inside this myCustomFn
    // but helpers.error() does not allow setting message and helpers.message() does not allow setting code other then "custom"
    // and I do not want in many places in code use .custom() with combination .messages() because messages will be repetitive:
    // .custom(myCustomFn).messages({ 'my.error.code' : 'My error message' })
    // I tried to use helpers.prefs.messages['my.error.code'] = 'My error message' but it causes error
    if (.........)
        return helpers.error('my.error.code');

    return value;
};

joi.object({
.........
})
.xor(.......)
.custom(myCustomFn)

nolimitdev avatar Dec 30 '24 09:12 nolimitdev