joi icon indicating copy to clipboard operation
joi copied to clipboard

Link and When - Cannot combine object with link

Open mattmast opened this issue 3 years ago • 0 comments

Support plan

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

Context

  • node version: v14.15.5
  • 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:

How can we help?

Hello, thank you for your time. I'm evaluating Joi for replacing AJV, so far so good until I needed to try out one of our recursive schemas. We have modules and templates. Modules has children that is a list of resources that can either be a SubModule or a Template. I was hoping for the following schema to work but I get Cannot combine object with link error.

const subModule = Joi.object({
  title: Joi.string().required(),
  resource: Joi.string().valid("module").required(),
  children: Joi.array().items(
    Joi.object({
      resource: Joi.string().valid("template", "module").required(),
    }).when(".resource", {
      switch: [
        {
          is: "module",
          then: Joi.link("#subModule"),
        },
        {
          is: "template",
          then: template,
        },
      ],
    })
  ),
}).id("subModule");

mattmast avatar May 24 '21 15:05 mattmast