我在项目中使用joiful
软件包。在此primitiveArray
函数中,我必须为必填字段传递自定义错误消息,但是它抛出了urlImage
之类的joi错误消息。
primitiveArray = (type: 'string' | 'number' | 'urlImage', required: boolean = false): jfArrayReturnType => {
let jfFunction: jfArrayReturnType;
switch (type) {
case 'urlImage':
jfFunction = jf.array().items((joi) =>
joi.string()
.error(this.sendValidationError({
// REQUIRED ERROR TYPE: any.required
[JoiValidationErrorType.REQUIRED]: 'sssssss',
[JoiValidationErrorType.REGEX]: 'Array should match \'image\' pattern',
[JoiValidationErrorType.STRING]: 'Array should contain only string'
} as JoiValidationErrorMessageType))
.trim());
break;
default:
jfFunction = jf.array().items((joi) =>
joi.string()
.error(this.sendValidationError({
[JoiValidationErrorType.STRING]: 'Array should contain only string'
} as JoiValidationErrorMessageType))
.trim());
}
if (required) {
return this.required(jfFunction);
}
return jfFunction;
};