Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
brandonatcv opened this issue Nov 8, 2024 · 1 comment · May be fixed by #3054
Open

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

brandonatcv opened this issue Nov 8, 2024 · 1 comment · May be fixed by #3054
Labels
bug Bug or defect

Comments

@brandonatcv
Copy link

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
}
@brandonatcv brandonatcv added the bug Bug or defect label Nov 8, 2024
@brandonatcv
Copy link
Author

What I've reported as "unexpected" might be the intended behavior based on this test in test/base.js#1227:

describe('empty()')
it('should override any previous empty')

If that's the case, I think an update to the documentation is needed.

@brandonatcv brandonatcv linked a pull request Nov 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant