Whisher
Whisher

Reputation: 32726

hapi joi how to validate Optional parameters

How to validate an optional parameter using hapi and joi

path: '/users/{limit?}',

limit is optional but if present should be an integer.

Upvotes: 2

Views: 2917

Answers (1)

Gergo Erdosi
Gergo Erdosi

Reputation: 42058

You can use Joi.number().integer() in the validate section:

server.route({
    method: 'GET',
    path:'/users/{limit?}',
    config: {
        validate: {
            params: {
                limit: Joi.number().integer()
            }
        }
    },
    handler: function (request, reply) {
        reply('ok');
    }
});

Upvotes: 7

Related Questions