I'm trying to validate a custom Type with JOI all with conversion.
the base type of my JoiType is a Joi.array()
when I try to
const schema = Joi.object().keys({
attribute: Joi.array().items(Joi.string())
});
const result = schema.validate(exampleOfArray);
I found that result.value
contains the real validated value.
But when I change the type to a custom type, the value is not returned : (result.value === undefined
)
here is my custom type :
const customJoi = Joi.extend((joi: Root) => {
return {
type: 'stringArray',
base: joi.array().items(joi.string()).meta({ baseType: 'array' }),
coerce(value: any, helpers: CustomHelpers) {
if (typeof value !== 'string') {
return value;
}
if (!value) {
return [];
}
return value.replace(/^,+|,+$/mg, '').split(',');
}
};
});
const schema = Joi.object().keys({
attribute: customJoi.stringArray().items(Joi.string())
});
I have found the solution:
the coerce function should return :
return { value: value };
instead of :
return value;