【发布时间】:2020-04-03 02:16:12
【问题描述】:
我正在尝试使用 JOI 验证自定义类型,并进行转换。
我的 JoiType 的基本类型是 Joi.array()
当我尝试时
const schema = Joi.object().keys({
attribute: Joi.array().items(Joi.string())
});
const result = schema.validate(exampleOfArray);
我发现result.value 包含真正的验证值。
但是当我将类型更改为自定义类型时,没有返回值 : (result.value === undefined)
这是我的自定义类型:
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())
});
【问题讨论】:
标签: javascript node.js hapijs joi hapi