hapijs / hapijs/joi

Set both custom error code and custom message inside custom function

Open
#3,059 0 comments 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

### 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?

```js

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)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.