joi icon indicating copy to clipboard operation
joi copied to clipboard

TypeError: Method get %TypedArray%.prototype.buffer called on incompatible receiver [object Object]

Open everhardt opened this issue 2 years ago • 2 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v18.12.1
  • module version with issue: v17.7.0
  • last module version without issue: n/a
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

Define a Joi object with a property buffer and validate it with a buffer as the object:

Joi.object().keys({
  a: Joi.object().keys({
    buffer: Joi.string().required(),
  }),
}).validate({
  a: Buffer.from('hi', 'utf8'),
});

What was the result you got?

[..]/node_modules/joi/lib/types/keys.js:103
                const item = value[key];
                                  ^
TypeError: Method get %TypedArray%.prototype.buffer called on incompatible receiver [object Object]
    at get buffer [as buffer] (<anonymous>)
    at Object.validate ([..]/node_modules/joi/lib/types/keys.js:103:35)
    at Object.exports.validate ([..]/node_modules/joi/lib/validator.js:328:26)
    at internals.Base.$_validate ([..]/node_modules/joi/lib/base.js:774:26)
    at Object.validate ([..]/node_modules/joi/lib/types/keys.js:108:45)
    at Object.exports.validate ([..]/node_modules/joi/lib/validator.js:328:26)
    at Object.internals.entry ([..]/node_modules/joi/lib/validator.js:145:28)
    at Object.exports.entry ([..]/node_modules/joi/lib/validator.js:27:30)
    at internals.Base.validate ([..]/node_modules/joi/lib/base.js:548:26)
    at Object.<anonymous> ([..]/test.ts:7:4)

What result did you expect?

No error being thrown

everhardt avatar Nov 22 '22 13:11 everhardt

Buffers actually have a dedicated type for them: https://joi.dev/api/?v=17.8.3#binary Are you trying to do validate things that binary can't?

Marsup avatar Mar 30 '23 18:03 Marsup

No, I'm expecting an object with a property buffer that is of type string, but the unknown thing I try to validate is an object with a property buffer that is an instance of Buffer. Instead of .validate properly returning an object with an error property to indicate that the object to validate is not valid, a TypeError gets thrown, which is risky as .validate normally does not throw.

everhardt avatar Apr 06 '23 06:04 everhardt