Skip to content

Re-instate .lazy(..) to support Mutually Recursive Schemas? #2611

Open
@lukehesluke

Description

@lukehesluke

Support plan

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

Context

  • node version: 14.15.0
  • module version: 17.4.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...):standalone
  • any other relevant information:

What problem are you trying to solve?

I would like to use JOI for the validation logic for a set of structural types which include some mutually recursive references.

A limited example just to demonstrate the point:

const X = Joi.object({ a: Y });
const Y = Joi.object({ b: X });

This obviously won't work as Y will be undefined in the first line. This also wouldn't be achievable using .link(..) as .link(..) (as far as I can tell) only works for referencing things within the one schema's boundaries itself. i.e. I initially tried:

const X = Joi.object({ a: Joi.link('#Y') }).id('X');
const Y = Joi.object({ b: Joi.link('#X') }).id('Y');

But, upon, trying to validate something, received the error Error: "a" contains link reference "ref:local:Y" which is outside of schema boundaries. This agrees with the docs here.

In v15, there was a function .lazy(..) which supports this approach. It would allow:

const X = Joi.object({ a: Joi.lazy(() => Y) });
const Y = Joi.object({ b: X });

.lazy(..) was deleted in v16 (release notes)

To help give more context, I'm writing something that generates JOI validation schemas out of schema.org. schema.org models have many mutually recursive references e.g. https://schema.org/Enumeration references https://schema.org/Class (in supersededBy), which references https://schema.org/Enumeration again, etc etc.

Do you have a new or modified API suggestion to solve the problem?

Re-introducing .lazy(..) as it was in v15 (https://joi.dev/api/?v=15.1.1#lazyfn-options---inherits-from-any).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions