joi
joi copied to clipboard
Conditional options contain unknown keys: not
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 https://github.com/hapijs/joi/blob/ed25e95c299190e9aace62569d4b8306b2d8e614/lib/index.d.ts#L1940 when it should be invoking https://github.com/hapijs/joi/blob/ed25e95c299190e9aace62569d4b8306b2d8e614/lib/index.d.ts#L1939 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:
-
string
-
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.