Skip to content

Problem serializing URL classes when stringifying a value #3070

Open
@damusix

Description

@damusix

Runtime

nodejs

Runtime version

22.14 and 20.15

Module version

17.13.3

Last module version without issue

No response

Used with

hapi application

Any other relevant information

When validating something with a class, the JSON validator throws an error whenever a URL instance is present.

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

const Joi = require('joi');

const someSchema = Joi.object({
    type: Joi.string().required(),
}).unknown(false);

const configs = [
    {
        someUrl: new URL('https://some-url.com')
    }
]

Joi.assert(
    configs,
    Joi.array().items(someSchema)
);

What was the result you got?


TypeError: Receiver must be an instance of class URL
    at URL.toJSON (node:internal/url:1136:10)
    at JSON.stringify (<anonymous>)
    at internals.safeStringify (/repos/joibug/node_modules/joi/lib/annotate.js:93:17)
    at exports.error [as annotate] (/repos/joibug/node_modules/joi/lib/annotate.js:70:29)
    at internals.assert (/repos/joibug/node_modules/joi/lib/index.js:236:78)
    at Object.assert (/repos/joibug/node_modules/joi/lib/index.js:102:19)
    at Object.<anonymous> (/repos/joibug/index.js:14:5)
    at Module._compile (node:internal/modules/cjs/loader:1554:14)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32)

Node.js v22.14.0

What result did you expect?

/repos/joibug/node_modules/joi/lib/index.js:243
    throw error;
    ^

Error [ValidationError]: [
  {
    "someUrl": "...",
    "type" [1]: -- missing --
  }
]

[1] "[0].type" is required
    at exports.process (/repos/joibug/node_modules/joi/lib/errors.js:193:16)
    at internals.entry (/repos/joibug/node_modules/joi/lib/validator.js:250:26)
    at exports.entry (/repos/joibug/node_modules/joi/lib/validator.js:27:30)
    at internals.Base.validate (/repos/joibug/node_modules/joi/lib/base.js:548:26)
    at internals.assert (/repos/joibug/node_modules/joi/lib/index.js:225:27)
    at Object.assert (/repos/joibug/node_modules/joi/lib/index.js:102:19)
    at Object.<anonymous> (/repos/joibug/index.js:14:5)
    at Module._compile (node:internal/modules/cjs/loader:1554:14)
    at Object..js (node:internal/modules/cjs/loader:1706:10)
    at Module.load (node:internal/modules/cjs/loader:1289:32) {
  _original: [ { someUrl: '...' } ],
  details: [
    {
      message: '"[0].type" is required',
      path: [ 0, 'type' ],
      type: 'any.required',
      context: { label: '[0].type', key: 'type' }
    }
  ]
}

Node.js v22.14.0

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