Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 368
Tas Long Avatar asked Jan 01 '23 04:01

Tas Long


1 Answers

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) })

like image 68
Nits Avatar answered Jan 14 '23 13:01

Nits