Skip to content

Support plugin-specific route options validation #4074

@jonathansamines

Description

@jonathansamines

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.

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:

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew functionality or improvement

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions