hapijs / hapijs/joi

Is there any elegant way to mix in custom error codes?

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

Description

#### Support plan

* *is this issue currently blocking your project?* (no):
* *is this issue affecting a production system?* (no):

#### Context

* *node version*: 15.6.0
* *module version*: 17.4.2
* *environment* (e.g. node, browser, native): node.js
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*: -

#### How can we help?

I want to add custom error codes. I've done it like this:

```js
//schema
messages({
'string.length': '2000:Phone must be longer than 10 characters '
})

//validator
const validator = schema.validate(data)

if(validator.error) {
const code = Number(validator.error.message.split(':')[0])
const message = validator.error.message.split(':')[1]

throw new ValidationException(code, message)
}
```

But the bad side is that I don't wanna rewrite all the messages, I just need to mix in codes without touching the messages.
Is it possible at least to make something like this:

```js
messages({
'string.length': '2000:{#message}'
})
```

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.