Tas Long
Tas Long

Reputation: 99

What is causing the error "Cannot set uncompiled validation rules without configuring a validator" in NodeJS?

Here's what I have so far.

'use strict';

const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi")

const server = new Hapi.Server({ host: "0.0.0.0", port: 80 });

server.route({
    method: "POST",
    path: "/board",
    options: {
        validate: {
            payload: {
                name: Joi.object({
                    name: Joi.string().min(1).max(15)
                })
            }
        }
    },
    handler: async (request, h) => {
        // do stuff
    }
});

server.start();

This produces the error

Error: Cannot set uncompiled validation rules without configuring a validator
    at new module.exports (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hoek\lib\error.js:23:19)
    at Object.module.exports [as assert] (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hoek\lib\assert.js:20:11)
    at Object.exports.compile (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\validation.js:48:10)
    at module.exports.internals.Route._setupValidation (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\route.js:197:43)
    at new module.exports.internals.Route (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\route.js:122:14)
    at internals.Server._addRoute (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\server.js:498:23)
    at internals.Server.route (C:\Users\Fukatsumu\Desktop\projects\Textboard\node_modules\@hapi\hapi\lib\server.js:491:22)
    at Object.<anonymous> (C:\Users\Fukatsumu\Desktop\projects\Textboard\index.js:37:8)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

I expected this to validate the request, but instead it's producing an error message that there are very few details on how to fix.

Upvotes: 7

Views: 7871

Answers (2)

tk120404
tk120404

Reputation: 3073

On upgrading to Latest hapi, check your code for route validate and response.schema settings and if you are passing values that must be compiled (see above for the lack of Joi.object() as a typical case), either wrap your schema with Joi.object() or call server.validator(Joi)

Upvotes: 3

Nits
Nits

Reputation: 550

It should be

'use strict';

const Hapi = require("@hapi/hapi");
const Joi = require("@hapi/joi")

const server = new Hapi.Server({ host: "0.0.0.0", port: 80 });

server.route({
    method: "POST",
    path: "/board",
    options: {
        validate: {
            payload: Joi.object({
                    name: Joi.string().min(1).max(15)
                })
        }
    },
    handler: async (request, h) => {
        // do stuff
    }
});

server.start();

payload: Joi.object({ name: Joi.string().min(1).max(15) })

Upvotes: 13

Related Questions