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

Conditional options contain unknown keys: not #3044

Open
klynchkurzawa opened this issue Jul 1, 2024 · 3 comments
Open

Conditional options contain unknown keys: not #3044

klynchkurzawa opened this issue Jul 1, 2024 · 3 comments
Labels
bug Bug or defect

Comments

@klynchkurzawa
Copy link

Runtime

node.js

Runtime version

18, 20, 22

Module version

17.13.3

Last module version without issue

unknown

Used with

https://joi.dev/tester/

Any other relevant information

Hello,

After attempting to use conditionals in Joi, I believe there is an issue with .conditional() when I am invoking it.
It appears as if when I attempt to invoke it per the docs .conditional(ref: string, options: WhenOptions[]) and what's defined on the interface, it attempts to invoke conditional(ref: Schema, options: WhenSchemaOptions)

Example schema:

Joi.object({
  x: Joi.string().min(1).max(5).required(),
  y: Joi.alternatives().conditional('x', [
    {
    	is: 'foo',
        then: Joi.string().valid('bar').required()
    },
    {
       not: 'foo',
       then: Joi.string().optional(),
    },
  ]).match('one'),
})

Example payload:

{
  x: "foo2",
  y: "bar"
}

Expected behaviour:

Validation Passed

Actual behaviour:

An error is encountered stating that the key of not is unknown.

Error: Options contain unknown keys: not

Possible cause of the error

The conditional method appears to be invoking the incorrect overloaded method at

conditional<ThenSchema, OtherwiseSchema>(ref: Schema, options: WhenSchemaOptions<ThenSchema, OtherwiseSchema>): AlternativesSchema<ThenSchema | OtherwiseSchema>;
when it should be invoking
conditional<ThenSchema, OtherwiseSchema>(ref: string | Reference, options: WhenOptions | WhenOptions[]): AlternativesSchema<ThenSchema | OtherwiseSchema>;
based off of the error that is being returned

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

I am trying to define a joi schema where there are alternatives using conditionals where the parameters are of the following corresponding types:

  1. string
  2. WhenOptions[]

What was the result you got?

I received an error:

Error: Options contain unknown keys: not

What result did you expect?

I expected to be able to define conditional alternatives by referencing an object's attribute using a string instead of a Reference object.

@klynchkurzawa klynchkurzawa added the bug Bug or defect label Jul 1, 2024
@Marsup
Copy link
Collaborator

Marsup commented Jul 3, 2024

Hi,

I can confirm the issue, it looks like it was designed to be only working with is/then, I'm looking for ways to alter the codebase to deal with that, but my time may be limited, so feel free to make a PR if you find a clever way to do it.

@Marsup
Copy link
Collaborator

Marsup commented Jul 3, 2024

By the way, I hope you're not using that exact construct to do a simple if/else, because it's obviously better written as is/then/otherwise in a single step.

@klynchkurzawa
Copy link
Author

klynchkurzawa commented Jul 18, 2024

@Marsup Sorry, been busy the last few weeks and didn't see your reply. I did end up going with the is/then/otherwise approach to work around the issue.

I was using is/not/then for a more complicated structure but gave the above example as I thought it was the most simplistic and highlighted the issue I was seeing the best.

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

No branches or pull requests

2 participants