Skip to content

any.describe() throws an error if the schema contains .allow(NaN) #3094

@RobinTheSprite

Description

@RobinTheSprite

Runtime

node.js

Runtime version

22.13.0

Module version

18.0.2

Last module version without issue

14.3.1

Used with

standalone

Any other relevant information

No response

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

While adding some data validation for my API, I wanted to create a Joi schema that would allow a field to be a number, null, or NaN. The schema Joi.number().allow(null, NaN) seemed a natural fit, and indeed this schema validates numbers as expected.

I also wanted to use the output of the .describe() function to generate some documentation for the API. However, when I called .describe() on the schema I encountered an error.

Below is a toy example I created in a file named test.js that demonstrates the issue. Run node test.js in a project with [email protected] installed to test this:

const Joi = require('joi')

const schema = Joi.number().allow(null, NaN)

console.log(Joi.attempt(1, schema)) // Returns 1 ✔
console.log(Joi.attempt(null, schema)) // Returns null ✔
console.log(Joi.attempt(NaN, schema)) // Returns NaN ✔

console.log(schema.describe()) // Throws an error ✘

What was the result you got?

The last line of the above example code produces this error (I have replaced the full path to my project root with "."):

./node_modules/joi/lib/index.js:242
    throw error;
    ^

Error [ValidationError]: {
  "type": "number",
  "allow": [
    NaN [1]
  ]
}

[1] "allow[0]" does not match any of the allowed types
    at exports.process (./node_modules/joi/lib/errors.js:193:16)
    at internals.entry (./node_modules/joi/lib/validator.js:258:26)
    at exports.entry (./node_modules/joi/lib/validator.js:24:30)
    at internals.Base.validate (./node_modules/joi/lib/base.js:578:26)
    at internals.assert (./node_modules/joi/lib/index.js:224:27)
    at Object.assert (./node_modules/joi/lib/index.js:101:19)
    at internals.validate (./node_modules/joi/lib/manifest.js:474:9)
    at exports.describe (./node_modules/joi/lib/manifest.js:167:15)
    at internals.Base.describe (./node_modules/joi/lib/base.js:62:25)
    at Object.<anonymous> (./test.js:8:20) {
  _original: { type: 'number', allow: [ NaN ] },
  details: [
    {
      message: '"allow[0]" does not match any of the allowed types',
      path: [ 'allow', 0 ],
      type: 'array.includes',
      context: { pos: 0, value: NaN, label: 'allow[0]', key: 0 }
    }
  ]
}

What result did you expect?

I expected the call to .describe() in the above example to output an object describing the schema. Version 14.3.1 is the latest version available to me that does this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug or defect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions