Support passing custom template functions
- 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
Assessment
This issue has not been assessed yet.