如何使用joi引发自定义错误消息

时间:2020-02-19 09:36:28

标签: typescript joi

我在项目中使用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;
};

0 个答案:

没有答案