hapi icon indicating copy to clipboard operation
hapi copied to clipboard

validation fails when using Hapi.types.Object() at the root

Open pgte opened this issue 13 years ago • 3 comments

When using Hapi.types.Object() at the root of a validation specification, a request fails with the following trace:

TypeError: Object function (value, obj, key, errors, keyPath) {

        if (typeof value !== 'object') {
            errors.add('the value of ' + key + ' must be an object', keyPath);
            return false;
        }

        return self._traverse(value, self._config, errors, keyPath);
    } has no method 'validate'
    at validateKeyConfig (/Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/joi/lib/index.js:77:47)
    at processConfig (/Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/joi/lib/index.js:56:13)
    at Object.exports.validate (/Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/joi/lib/index.js:111:5)
    at exports.payload (/Users/pedroteixeira/projects/test/node_modules/hapi/lib/validation.js:25:21)
    at /Users/pedroteixeira/projects/test/node_modules/hapi/lib/request.js:300:13
    at iterate (/Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/async/lib/async.js:131:13)
    at /Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/async/lib/async.js:142:25
    at /Users/pedroteixeira/projects/test/node_modules/hapi/node_modules/hoek/lib/index.js:582:22
    at process._tickDomainCallback (node.js:459:13)

Simple test case:

var Hapi        = require('hapi');
var server = Hapi.createServer('0.0.0.0', 8080);

var types = Hapi.types;

var query = types.Object({
    param: types.String().required()
  }, true);

query.___test = true;

var validate = {
  query: query
};

server.route({
  method: 'GET',
  path: '/test',
  config: {
    validate: validate,
    handler: handler
  }
});

server.start();

function handler(req) {
  req.reply('HAY');
}

Start the server and then query it:

$ curl http://localhost:8080/test?param=true

I have pinned it down to the fact that hapi clones the route config, loosing the joi.types.Object type at the root.

pgte avatar Aug 02 '13 11:08 pgte

I can confirm this and am working on a fix. I believe it will be a fix in the hoek module.

geek avatar Aug 02 '13 21:08 geek

Can someone explain the second argument (true) to types.Object and also query.__test = true?

I was able to get this working by just unwrapping query from types.Object:

var Hapi = require('hapi');
var server = Hapi.createServer('0.0.0.0', 8080);

var types = Hapi.types;

var query = { 
  param: types.String().required()
};

var validate = {
  query: query
};

server.route({
  method: 'GET',
  path: '/test',
  config: {
    validate: validate,
    handler: handler
  }
});

server.start();

function handler(req) {
  req.reply('HAY');
}

As an aside, I ran into very similar, if not the same issue, when trying to set validate.payload to a Joi schema defined in a separate node module. I ended up unwrapping schemas from types.Object(), similar to above, and also having to run npm dedup in order to enforce the same Joi instance being returned when required().

papajuans avatar Sep 12 '13 14:09 papajuans

Thanks for the feedback, I am not seeing anything related to a second parameter for Object. The only parameter used is the configuration object.

geek avatar Sep 12 '13 15:09 geek