Joi's ref documentation suggests that a Reference created using Joi.ref
can refer to a value from the context object. Joi's any.when documentation suggests that the condition
parameter accepts a Reference. However, I can't get the following simple usage to work:
import Joi from "joi";
const schema = Joi.object({
year: Joi.number().when(Joi.ref(`$flag`), {
is: Joi.boolean().truthy,
then: Joi.number().min(2000),
}),
});
const value = {
year: 1999,
};
const context = {
flag: true
};
const result = schema.validate(value, { context });
This code results in a passing validation. However, I would expect it to fail. What am I missing?
You need to use
boolean().valid(true)
so only the boolean valuetrue
passes!boolean().truthy()
accepts a list of additional values to be considered valid booleans. E.g.boolean().truthy('YES', 'Y')
. Details in the Documentationjoi
version: 17.2.1