Support plugin-specific route options validation
- Dominant language
- JavaScript
- Stars
- 14.8k
- Forks
- 1.4k
- Avg merge
- 22d 3h
- Merged PRs (30d)
- 1
Description
#### Support plan
* *which support plan is this issue covered by?* (e.g. Community, Core, Plus, or Enterprise): Community
* *is this issue currently blocking your project?* (yes/no): No
* *is this issue affecting a production system?* (yes/no): No
#### Context
* *node version*: 12 LTS
* *module version*: 19.1.1
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi application
* *any other relevant information*: none
#### What problem are you trying to solve?
When authoring a plugin that can be configured "per route", it is often necessary to validate those options (ideally at boot time) so that no incorrect configuration is provided to the plugin.
```js
server.route({
method: 'GET',
path: '/',
options: {
plugins: {
pluginNS: {} // validate these options are correct at boot time
}
}
});
```
It is possible to manually perform this validation, but ideally a more declarative approach can be used.
#### Validating at boot time:
```js
server.ext('onPreStart', () => {
// Prepare plugin options
const routes = server.table();
for (const route of routes) {
if ('pluginNS' in route.settings.plugins) {
route.settings.plugins.pluginNS = Joi.attempt(route.settings.plugins.pluginNS, schema);
}
}
});
```
Reference: https://github.com/kanongil/nipo/blob/master/lib/index.js#L404-L414
#### Validating at runtime:
```js
options = Joi.attempt(request.route.settings.plugins.pluginNS, schema);
````
Reference: https://github.com/nlf/blankie/blob/master/lib/index.js#L271
#### Do you have a new or modified API suggestion to solve the problem?
I don't have a particular approach or design in mind. Any help here would be appreciated.
Contributor guide
Assessment
This issue has not been assessed yet.