否定Joi的内置email()验证程序有什么方法?
类似于此伪代码:
username: Joi.string().not.Joi.email()
OR
username: Joi.string().Joi.email().invert()
我能够使其与以下Regex一起使用:
const emailRegEx = RegExp('^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$');
const schema2 = Joi.object({
username: Joi.string().regex(emailRegEx, { invert: true })
})
不确定这是否是最好的方法,但是您可以尝试以下方法:
const schema = Joi.object().keys({
username: Joi.alternatives().when(
Joi.string().email(),
{
then: Joi.forbidden().error(new Error('must not be an email')),
otherwise: Joi.string().required()
}
)
});
schema.validate({ username: 'whatever' }); // error: null, value: { username: 'whatever' }
schema.validate({ username: '[email protected]' }); // error: Error: must not be an email