Facilitate extension of serialization of values in messages
- Dominant language
- JavaScript
- Stars
- 21.2k
- Forks
- 1.5k
- Avg merge
- 4h 57m
- Merged PRs (30d)
- 14
Description
### Runtime
NodeJS
### Runtime version
22
### Module version
17.13.3
### Used with
Standalone
### Any other relevant information
_No response_
### What problem are you trying to solve?
The default serialization of values using template `{#value}` does not match our expected outcomes. I was looking for a way to improve it (by relying on `require('node:utils').inspect`.
* I've tried using expressions only to switch formatting depending on value (I want to quote strings, but not int, and do this deeply within objects/array). For object, it resulted in `[object Object]`
* I've tried to declare global functions I could use in expression without passing it explicitly everytime.
* I ended up doing `Joi.expression('{#label} expected to be ..., have {inspectValue(#value)}', { inspectValue(value) { return inspect(value) }})` for every overridden message. It works, but requires a lot of boilerplate code for something I'd like to make available for the whole app.
### Do you have a new or modified API suggestion to solve the problem?
- Add a method to declare template functions available for every expressions
- Or add a way to customize Template's `internal.stringify`: https://github.com/hapijs/joi/blob/239ec335a19879f053439e778f0d1b9f57e3d351/lib/template.js#L330-L385
Contributor guide
Assessment
This issue has not been assessed yet.