Skip to content

Multiple calls to empty() don't chain as expected #3053

Open
@brandonatcv

Description

@brandonatcv

Runtime

node.js

Runtime version

17.x

Module version

Last module version without issue

No response

Used with

standalone

Any other relevant information

I was able to reproduce this issue at https://joi.dev/tester/.

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

When chaining multiple calls to empty(), only the last call is considered. Here's a simple example:

// Schema
Joi.object({
  testA: Joi.string().empty("a").empty("b"),
  testB: Joi.string().empty("a").empty("b")
}

// Data
{
  testA: "a",
  testB: "b"
}

// Expected result
{}

// Actual result
{
  testA: "a"
}

The workaround is to pass an array to .empty(), which works as expected for all values.

// Schema
Joi.object({
  testA: Joi.string().empty(["a", "b"]),
  testB: Joi.string().empty(["a", "b"])
})

// Data
{
  testA: "a",
  testB: "b"
}

// Result
{}

What was the result you got?

Only the last call to .empty() is honored.

What result did you expect?

I expect calls to .empty() to "stack" like other methods. For example, this works fine:

// Schema
Joi.object({
  test1: Joi.string().allow(1).allow(2),
  test2: Joi.string().allow(1).allow(2)
})

// Data
{
  test1: 1,
  test2: 2
}

// Result
{
  test1: 1,
  test2: 2
}

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