hapijs / hapijs/joi

Support passing custom template functions

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

Description

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

#### Context

* *node version*: 12-14
* *module version*: 17
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi app
* *any other relevant information*:

#### What problem are you trying to solve?

Support custom functions in joi templates. I can see that the package `formula` is used and is passed an internal set of functions here: https://github.com/sideway/joi/blob/master/lib/template.js#L384

I think it would be great if we could supply some custom functions to allow string manipulations like `lower` and `slice`. This would allow re-use of the `label` property

```js
joi
.string()
.label('First name')
.max(50)
.required()
.options({
messages: {
'string.empty': 'Enter your {{lower(#label)}}',
'string.max': '{{#label}} must be {{#limit}} characters or fewer'
}
})
```

#### Do you have a new or modified API suggestion to solve the problem?
Not sure. Maybe by creating a new customized instance of the joi module like for extensions?

Or maybe as part of the call to `options`
```js
joi
.string()
.label('First name')
.max(50)
.required()
.options({
messages: {
'string.empty': 'Enter your {{lower(#label)}}',
'string.max': '{{#label}} must be {{#limit}} characters or fewer'
},
functions: {
slice: (str, start, end) => str.slice(start, end),
lower: (str) => str.toLowerCase(),
...
}
})
```

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.