Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node.get is not a function when validating #2156

Closed
jkurian opened this issue Sep 28, 2019 · 6 comments
Closed

node.get is not a function when validating #2156

jkurian opened this issue Sep 28, 2019 · 6 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@jkurian
Copy link

jkurian commented Sep 28, 2019

Context

Validating an object against my schema throws node.get or node.set is not a function when I expect keys to be stripped instead.

  • node version: 10.16.0 and 12.8.1
  • joi version: 16.1.4
  • environment (node, browser): node
  • used with (hapi, standalone, ...): hapi
  • any other relevant information:

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

I am trying to write a validation that strips out items that do not pass the set validation. See below for more details.

Create the following scenario

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

const A = Joi.object().keys({z: Joi.date().iso().required()})
const B = Joi.object().keys({a: Joi.array().min(1).items(A, Joi.any().strip()).required()});

const schema = Joi.object().keys({data: Joi.object().keys({b: Joi.array().items(B, Joi.any().strip()).min(1).required()}).required()});

const toValidate1 = {data: {b: [{a: []}, {a: [{x: new Date()}]}]}}
const toValidate2 = {data: {b: [{a: []}, {a: [{z: new Date()}]}]}} 
const toValidate3 = {data: {b: [{a: []}, {a: [{x: new Date()}]}, {a: [{z: new Date()}]}]}} 

Joi.assert(toValidate1, schema) // throws node.get is not defined
Joi.assert(toValidate2, schema) // ok
Joi.assert(toValidate3, schema) // throws node.get is not defined

Which result you had ?

Error thrown for cases 1 and 3.

Thrown:
TypeError: node.get is not a function
    at internals.Shadow.set <path>/node_modules/@hapi/joi/lib/validator.js:700:29)
    at Object.internals.finalize (<path>/node_modules/@hapi/joi/lib/validator.js:598:31)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:391:26)
    at internals.Base.$_validate (<path>/node_modules/@hapi/joi/lib/base.js:747:26)
    at Object.validate (<path>/node_modules/@hapi/joi/lib/types/array.js:315:45)
    at Object.internals.rules (<path>/node_modules/@hapi/joi/lib/validator.js:443:33)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:395:22)
    at internals.Base.$_validate (<path>/node_modules/@hapi/joi/lib/base.js:747:26)
    at Object.validate (<path>/node_modules/@hapi/joi/lib/types/keys.js:107:45)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:369:26)

What did you expect ?

Expect the item to be stripped (if my usage is correct) otherwise an error from Joi explaining why the assertion failed.

@hueniverse hueniverse self-assigned this Sep 29, 2019
@hueniverse hueniverse added the bug Bug or defect label Sep 29, 2019
@hueniverse hueniverse added this to the 16.1.5 milestone Sep 29, 2019
@jkurian
Copy link
Author

jkurian commented Sep 30, 2019

I also see the error node.set is not a function with the following schema if this helps:

const Joi = require("@hapi/joi");
const A = Joi.object()
    .keys({
a: Joi.date().timestamp().raw().required(),
}).required()
    .options({ stripUnknown: true });

const schema = Joi.alternatives(
    Joi.array().items(A.required(), Joi.any().strip()),
    A,
);

Joi.attempt([{a: new Date()}, {}, {a: 1}]) //ok 
Joi.attempt([{a:1}]) // throws a proper error
Joi.attempt([{a:1, {}}]) // throws node.set is not a function

The error for case 2 is:

Thrown:
{ ValidationError: "value" does not contain 1 required value(s)
    at Object.exports.process (<path>/node_modules/@hapi/joi/lib/errors.js:183:16)
    at Object.internals.entry (<path>/node_modules/@hapi/joi/lib/validator.js:139:26)
    at Object.exports.entry (<path>/node_modules/@hapi/joi/lib/validator.js:25:30)
    at internals.Base.validate (<path>/node_modules/@hapi/joi/lib/base.js:531:26)
    at Object.internals.assert (<path>/node_modules/@hapi/joi/lib/index.js:210:27);
    at Object.attempt (<path>/node_modules/@hapi/joi/lib/index.js:100:26)
  _original: [ { a: 'a' } ],
  details:
   [ { message: '"value" does not contain 1 required value(s)',
       path: [],
       type: 'array.includesRequiredUnknowns',
       context: [Object] } ] }

error for case 3:

Thrown:
TypeError: node.set is not a function
    at internals.Shadow.set (<path>/node_modules/@hapi/joi/lib/validator.js:631:14)
    at Object.internals.finalize (<path>/node_modules/@hapi/joi/lib/validator.js:510:31)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:253:30)
    at internals.Base.$_validate (<path>/node_modules/@hapi/joi/lib/base.js:747:26)
    at Object.validate (<path>/node_modules/@hapi/joi/lib/types/keys.js:107:45)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:313:26)
    at internals.Base.$_validate (<path>/node_modules/@hapi/joi/lib/base.js:747:26)
    at Object.validate (<path>/node_modules/@hapi/joi/lib/types/array.js:222:50)
    at Object.internals.rules (<path>/node_modules/@hapi/joi/lib/validator.js:382:33)
    at Object.exports.validate (<path>/node_modules/@hapi/joi/lib/validator.js:338:22)

@BolajiOlajide
Copy link
Contributor

@hueniverse Is this something you're working on or should I take a stab at this?

@hueniverse
Copy link
Contributor

I have a fix, but I found another problem and I'd like to fix both at the same time. This is a simple bug. The other one is complicated. If I can't figure it out by tomorrow morning, I'll push a temporary fix.

@BolajiOlajide
Copy link
Contributor

Alright. Let me know if I can help.

@jkurian
Copy link
Author

jkurian commented Oct 1, 2019

Thanks @hueniverse and @BolajiOlajide!

justsml added a commit to justsml/joi that referenced this issue Oct 22, 2019
* 'master' of github.com:hapijs/joi:
  Update API.md
  Improve compile version conflict error message. Closes hapijs#2173
  Closes hapijs#2172
  Fix docs malformed code block ending at section `object.pattern.match`
  End code block
  Fix docs missing code block ending at section `date.less(date)`
  Fix function signature. Fixes hapijs#2170.
  Delete .npmrc
  Delete .editorconfig
  Delete feature_request.md
  Delete bug_report.md
  Delete CONTRIBUTING.md
  16.1.7
  Fix date format validation. Closes hapijs#2168
  16.1.6
  Closes hapijs#2165
  16.1.5
  Clarify error(). Closes hapijs#2158
  Closes hapijs#2161
  Fix handling of shadow values. Closes hapijs#2156
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants