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

allow() skipping all the validation in when() #2949

Open
terrence-wong opened this issue May 16, 2023 · 0 comments
Open

allow() skipping all the validation in when() #2949

terrence-wong opened this issue May 16, 2023 · 0 comments
Labels
support Questions, discussions, and general support

Comments

@terrence-wong
Copy link

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: any
  • module version with issue: v17.9.1
  • last module version without issue:
  • environment (e.g. node, browser, native): browser
  • used with (e.g. hapi application, another framework, standalone, ...):
  • any other relevant information:

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

This is the schema I am working on. There are two fields: remarks and userList.

remarks can be optional(i.e. empty string or null) if userList is an empty array.
If userList contains some elements, then remarks turns into a required field.

Joi.object({
  remarks: Joi.string().allow("", null),
  userList: Joi.array(),
}).when(
  Joi.object({
    userList: Joi.array().min(1),
  }).unknown(),
  {
    then: Joi.object({
      remarks: Joi.string().required(),
    }),
    otherwise: Joi.object({
      remarks: Joi.any(),
    }),
  }
);

This is the data I am trying to validate:

{ 
  remarks: '',
  userList: [{}]
 }
// OR
{ 
  remarks: null,
  userList: [{}]
 }

I tried in my project as well as the sandbox in the document page(https://joi.dev/tester/), and both doesn't work as expected. It seems if matches with allow(), then the then part will never check.

What was the result you got?

Validation Passed

What result did you expect?

Validation Error: "remarks" is not allowed to be empty
@terrence-wong terrence-wong added the support Questions, discussions, and general support label May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant