Issue
I’m trying to add conidial schema according of the value of the isAdmin field in the request body, here is what I have so far:
import Joi from 'joi';
import entity from './entity';
import put from '../../self-serve/sources/put';
module.exports = Joi.when(Joi.ref('$isAdmin'), {
is: true,
then: Joi.object({
params: {
id: Joi.number().required()
.integer()
.min(1),
},
body: {
...entity,
code: Joi.string().required(),
},
}),
otherwise: Joi.object({
params: {
id: Joi.number().required()
.integer()
.min(1),
},
body: {
...put,
code: Joi.string().required(),
},
}),
});
Inside ‘entity’ and ‘put’ I have other blocks of fields and declarations.
For some reason I get AssertionError [ERR_ASSERTION]: Invalid schema content
error.
What am I missing? Thanks.
Solution
Try this
Joi.object({
params: Joi.object({
id: Joi.number().required().integer().min(1),
}),
body: Joi.object({
code: Joi.string().required(),
}).when(Joi.ref("$isValid"), {
is: Joi.equal(true),
then: Joi.object({
body: Joi.object({
...entity,
}),
}),
otherwise: Joi.object({
body: Joi.object({
...put,
}),
}),
}),
});
Verify the reference given by you is right Joi.ref("$isValid")
.
If you provide right reference, this schema will work as you expect.
Answered By – Saurabh Rahate
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0