Skip to content

defaults in alternatives should work recursively #2590

@nlundquist

Description

@nlundquist

Support plan

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

Context

  • node version: 14
  • module version with issue: 17.4.x
  • last module version without issue: n/a
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

Carrying on from the work in #2535, it's reasonable that the defaults in following case should work:

const schema = Joi.alternatives([
    Joi.alternatives(
        Joi.object({ foo: Joi.string().default('bar') }).unknown(),
        Joi.object({ baz: Joi.boolean().default(false) }).unknown()
    ).match('all'),
    Joi.object({ lol: Joi.boolean().default(true) }).unknown()
]).match('all');

However my solution for #2535 didn't handle the case of recursive sets of object alternatives being present in the matches set.

What was the result you got?

schema.validate({})
// {}

What result did you expect?

schema.validate({})
// { foo: 'bar', baz: false, lol: true }

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions