Reputation: 19986
I as trying to create a Joi schema using Joi.alternatives.try(). This is the schema I have tried.
Joi.alternatives().try(Joi.object({
type: Joi.number().required().label('Error1!!')
}), Joi.object({
reason: Joi.string().required().label('Error2!!')
})).label('Error!!')
This is the object that I have used.
{ reason: 2 }
I was expecting the error as Error2!!
or something containing this the string Error2!!
. But im getting error as
Validation Error: "Error!!" does not match any of the allowed types
This error comes from the parent node.
How can I make the error specific to the object? i,e, an error that comes from the alternative object node rather that the parent object.
You can use this platform to validate schema online.
Update: Here is the sample schema that I used.
employee_retired = Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
personaldetails: Joi.alternatives().conditional('type', {
is: 2, then: Joi.array().items(Joi.object({
address: Joi.string().required()
.min(1)
.max(100),
salary: Joi.string().required()
.min(0)
.max(500),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
}).required()).max(50).required(),
otherwise: Joi.forbidden(),
}),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number()
.required()
.min(0)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
});
empolyee_working = Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number().required()
.min(1)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
})
const employee = Joi.alternatives().try(employee_retired, empolyee_working);
Upvotes: 4
Views: 3136
Reputation: 3321
You may use object.or
for this behavior:
Joi.object({
type: Joi.number().label('Error1!!'),
reason: Joi.string().label('Error2!!')
}).or('type', 'reason').label('Error!!')
Tests:
{}
// Validation Error: "Error!!" must contain at least one of [Error1!!, Error2!!]
{ reason: 2 }
// Validation Error: "Error2!!" must be a string
{ type: "a" } // note that due to default `convert` behavior, `{ type: "2" }` would pass
// Validation Error: "Error1!!" must be a number
{ a: "b" }
// Validation Error: "a" is not allowed. "Error!!" must contain at least one of [Error1!!, Error2!!]
Indeed it's a bit more verbose, but follows the same logic:
personaldetails
and contactnumbers
varySo something along the following lines should provide you with a precise validation error message (I haven't tested all cases though). I just "merged" both employee declarations, and the two varying cases personaldetails
and contactnumbers
are not declared required
anymore but are specified in the final or
.
Joi.object({
type: Joi.number().required().valid(2, 3, 7),
reason: Joi.string().required()
.min(1)
.max(100),
firstname: Joi.string()
.required(),
lastname: Joi.string()
.required()
.min(1)
.max(255),
personaldetails: Joi.alternatives().conditional('type', {
is: 2, then: Joi.array().items(Joi.object({
address: Joi.string().required()
.min(1)
.max(100),
salary: Joi.string().required()
.min(0)
.max(500),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
})).max(50).required(),
// N.B.: no more .required() on the next line, .or() will handle it conditionally
}).required()).max(50),
otherwise: Joi.forbidden(),
}),
contactnumbers: Joi.array().items(Joi.object({
mobile: Joi.string().required()
.min(0)
.max(15),
home: Joi.string()
.required()
.min(1)
.max(15),
// N.B.: no more .required() on the next line, .or() will handle it conditionally
})).max(50),
monthlysavings: Joi.alternatives().conditional('type', {
is: 3,
then: Joi.number()
.required()
.min(0)
.max(50000),
otherwise: Joi.forbidden(),
}),
isapproved: Joi.boolean().required(),
}).or('personaldetails', 'contactnumbers').label('OR failure')
Upvotes: 2