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

Error: Invalid schema content: (email.$_root.alternatives) with Joi 16 #2107

Closed
ghost opened this issue Sep 12, 2019 · 18 comments
Closed

Error: Invalid schema content: (email.$_root.alternatives) with Joi 16 #2107

ghost opened this issue Sep 12, 2019 · 18 comments
Assignees
Labels
non issue Issue is not a problem or requires changes

Comments

@ghost
Copy link

ghost commented Sep 12, 2019

Context

  • node version: 12
  • joi version: 16.0.0
  • environment: node
  • used with: hapi
  • any other relevant information: Using Joi as a payload validator for route

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

I'm using Joi as a validator of payload for route like this:

{
  method: 'POST',
  path: '/path',
  config: {
    auth: false,
    tags: ['api'],
    validate: {
      payload: {
        email: Joi.string()
          .email()
          .allow(null, '')
          .optional(),
        id: Joi.string(),
        firstName: Joi.string(),
        lastName: Joi.string(),
      },
    },
  },
...
}

Which result you had ?

I have this exception in my console on app start:

Error: Invalid schema content: (email.$_root.alternatives)
    at new module.exports (/src/node_modules/@hapi/hapi/node_modules/@hapi/hoek/lib/error.js:21:15)
    at Object.module.exports [as assert] (/src/node_modules/@hapi/hapi/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:50:10)
    at internals.Object.keys (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
    at internals.Object.keys (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
    at internals.Object.keys (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
    at module.exports.internals.Any.root.compile (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/index.js:157:25)
    at Object.exports.compile (/src/node_modules/@hapi/hapi/lib/validation.js:21:49)
    at /src/node_modules/@hapi/hapi/lib/route.js:196:43
    at Array.forEach (<anonymous>)
    at module.exports.internals.Route._setupValidation (/src/node_modules/@hapi/hapi/lib/route.js:194:60)
    at new module.exports.internals.Route (/src/node_modules/@hapi/hapi/lib/route.js:122:14)
    at internals.Server._addRoute (/src/node_modules/@hapi/hapi/lib/server.js:485:23)
    at internals.Server.route (/src/node_modules/@hapi/hapi/lib/server.js:478:22)
    at Object.register (/src/api/index.js:16:12)
    at internals.Server.register (/src/node_modules/@hapi/hapi/lib/server.js:453:35)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async Object.exports.compose (/src/node_modules/@hapi/glue/lib/index.js:46:9)
    at async getServer (/src/getServer.js:16:20) {
    path: 'email.$_root.alternatives'
  }

What did you expect ?

I expect it works as it did with 15 version

@Marsup
Copy link
Collaborator

Marsup commented Sep 12, 2019

Can you try wrapping it into an object like so:

      payload: Joi.object({
        email: Joi.string()
          .email()
          .allow(null, '')
          .optional(),
        id: Joi.string(),
        firstName: Joi.string(),
        lastName: Joi.string(),
      }),

@ghost
Copy link
Author

ghost commented Sep 12, 2019

@Marsup Thank you for hint but no help=((

Error: Invalid schema content: ($_root.alternatives)
...

@Marsup
Copy link
Collaborator

Marsup commented Sep 12, 2019

Can you show the stacktrace with the updated code ?

@ghost
Copy link
Author

ghost commented Sep 12, 2019

sure=)
Here it go:

Error: Invalid schema content: ($_root.alternatives)
    at new module.exports (/src/node_modules/@hapi/hapi/node_modules/@hapi/hoek/lib/error.js:21:15)
    at Object.module.exports [as assert] (/src/node_modules/@hapi/hapi/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:50:10)
    at internals.Object.keys (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
    at internals.Object.keys (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/cast.js:31:29)
    at module.exports.internals.Any.root.compile (/src/node_modules/@hapi/hapi/node_modules/@hapi/joi/lib/index.js:157:25)
    at Object.exports.compile (/src/node_modules/@hapi/hapi/lib/validation.js:21:49)
    at /src/node_modules/@hapi/hapi/lib/route.js:196:43
    at Array.forEach (<anonymous>)
    at module.exports.internals.Route._setupValidation (/src/node_modules/@hapi/hapi/lib/route.js:194:60)
    at new module.exports.internals.Route (/src/node_modules/@hapi/hapi/lib/route.js:122:14)
    at internals.Server._addRoute (/src/node_modules/@hapi/hapi/lib/server.js:485:23)
    at internals.Server.route (/src/node_modules/@hapi/hapi/lib/server.js:478:22)
    at Object.register (/src/api/index.js:16:12)
    at internals.Server.register (/src/node_modules/@hapi/hapi/lib/server.js:453:35)
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async Object.exports.compose (/src/node_modules/@hapi/glue/lib/index.js:46:9)
    at async getServer (/src/getServer.js:16:20) {
    path: '$_root.alternatives'

@Marsup
Copy link
Collaborator

Marsup commented Sep 12, 2019

Are you sure all your modules are up to date ? Which version of hapi are you using ?

@xthule
Copy link

xthule commented Sep 12, 2019

I'm having the exact same issue. I've tried wrapping it in Joi.object() and Joi.object().keys() but keep getting the same issue. I'm using hapi 18.3.2 and have updated all modules. I stop getting the error if I change things like Joi.string() to just 'string', but I don't think it's actually validating anything.

In lib/cast.js at line 50 I found that config is a function, but it seems to expect config to either be isJoi, isRef, a string, number, or boolean.

@bricss
Copy link
Contributor

bricss commented Sep 12, 2019

I have the same issue, nothing is working.
Similar: hapijs/hapi#3970

@ghost
Copy link
Author

ghost commented Sep 12, 2019

Sorry, for late response
I'm using latest hapi 18.3.2

@hueniverse
Copy link
Contributor

The entire validation API has been changed. hapi needs an update to support it. Coming later today.

@xthule
Copy link

xthule commented Sep 12, 2019

We have to wait until "later today"? How dare you make us wait!!! We need it now!!! :)

Thanks for the update.

@hbinduni
Copy link

for quick solution, just downgrade into @hapi/joi@15.1.1

@hueniverse hueniverse self-assigned this Sep 13, 2019
@hueniverse hueniverse added the non issue Issue is not a problem or requires changes label Sep 13, 2019
@zedd45
Copy link

zedd45 commented Sep 13, 2019

#2107 (comment) @hbinduni's solution worked for me, as well.

@opravil-jan
Copy link

When will we be able to use joi 16 with hapi?

@hueniverse
Copy link
Contributor

hapi v18.4.0 supports joi v16. You must wrap the top level schema with Joi.object() for it to use v16 and not the built-in v15.

@Ba7er
Copy link

Ba7er commented Sep 21, 2019

Downgrading worked for me

@Marsup
Copy link
Collaborator

Marsup commented Sep 21, 2019

Come on, @hueniverse just posted the solution above...

@jsconestilo
Copy link

Probado con hapi 18.4 y Joi 161.4
`
// Requerir módulo para validación de datos en el backend
const Joi = require('@hapi/joi')

.....

{
// Los datos enviados a esta ruta deben cumplir con cierto esquema de validación. Ṕara ello se agrega dentro de sus opciones la validación del payload (body request)
// Cada esquema define los criterios que debe cumplir un campo para pasar la validación
method: 'POST',
path: '/create-user',
handler: userController.createUser,
options: {
validate: {
payload: Joi.object({
email: Joi.string().email().required(),
name: Joi.string().required().min(3),
password: Joi.string().required().min(6)
})
}
}
},
`

@hueniverse
Copy link
Contributor

@jsconestilo Support is only available in English.

@hapijs hapijs locked and limited conversation to collaborators Sep 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

No branches or pull requests

9 participants