hapijs / joi

The most powerful data validation library for JS
Other
20.9k stars 1.51k forks source link

got an error in nextjs 13.4.4:TypeError: Cannot read properties of undefined (reading '$_root') #2952

Open CrazyXiaoSong opened 1 year ago

CrazyXiaoSong commented 1 year ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

fix error

const some = 'properly formatted code example';

What was the result you got?

TypeError: Cannot read properties of undefined (reading '$_root')

What result did you expect?

no error

CrazyXiaoSong commented 1 year ago

oh I found the cause of the error! cause error: const {validate} = Joi.object({ username: Joi.string().required() }) const result =validate({ username: '666' }) fix error: const temp = Joi.object({ username: Joi.string().required() }) const result = temp.validate({ username: '666' }) I don't know if this is a bug or not。

robogeek commented 2 hours ago

I'm getting this same error and have it differently than what you describe.

Stack trace:

   TypeError [Error]: Cannot read properties of undefined (reading '$_root')
        at internals.tracer (/home/david/Projects/openadr/openadr-3-ts-types/package/node_modules/joi/lib/validator.js:258:16)
        at internals.entry (/home/david/Projects/openadr/openadr-3-ts-types/package/node_modules/joi/lib/validator.js:234:43)
        at exports.entry (/home/david/Projects/openadr/openadr-3-ts-types/package/node_modules/joi/lib/validator.js:28:30)
        at validate (/home/david/Projects/openadr/openadr-3-ts-types/package/node_modules/joi/lib/base.js:549:26)
        at validateParams (file:///home/david/Projects/openadr/openadr-3-client/node/dist/components/common.js:3:30)
        at Module.searchAllReports (file:///home/david/Projects/openadr/openadr-3-client/node/dist/components/reports.js:7:27)
        at OADR3Client.searchAllReports (file:///home/david/Projects/openadr/openadr-3-client/node/dist/client.js:252:24)
        at TestContext.<anonymous> (file:///home/david/Projects/openadr/openadr-3-client/node/test/test.reports.js:104:43)

At lib/base.js line 549, there's a method for the Base class, validate. Its "schema" argument is undefined causing the error in lib/validator.js at line 258.

The "schema" argument is coming from the caller. Joi could be doing defensive coding at that method, detect the undefined schema, and throw an informative error. As it is, we're left with confusion over what's going on.