[英]Joi.validate îs not a function
嘿伙计们,我正在尝试使用作业构建验证 function
async function validateUser(user) {
const schema = Joi.object({
password: Joi.string().pattern((new RegExp('^[a-zA-Z0-9]{3,30}$'))),
repeat_password: Joi.any().equal(Joi.ref('password')).required().messages({ 'any.only': '{{#label}} does not match' }),
email: Joi.string().email({ minDomainSegments: 2, tlds: { allow: ['com', 'dk']}})
});
try {
const value = await schema.validateAsync((user));
} catch (err) {
console.log(err)
}
}
但是 repeat_password 不能正常工作,
firstname: 'Mette ',
lastname: 'Nielsen',
age: "'35'",
gender: 'female',
about: 'I like to go and watch movies',
email: 'mettenielsen@23245.gaga',
password: 'metteNielsen123',
repeat_password: 'mette',
likes: 'Shopping, going to the mall, cities',
dislikes: 'rap musik, events, poor people'
希望你在你的问题上做得更好,而不是做
async function validateUser(user) {
const schema = Joi.object({
password: Joi.string().pattern((new RegExp('^[a-zA-Z0-9]{3,30}$'))),
repeat_password: Joi.any().equal(Joi.ref('password')).required().messages({
'any.only': '{{#label}} does not match' }),
email: Joi.string().email({ minDomainSegments: 2, tlds: { allow:
['com','dk']}})
});
try {
const value = await schema.validateAsync((user));
} catch (err) {
console.log(err)
}
}
请避免直接验证架构,您可以尝试类似
async function validateUser(user) {
const schema = Joi.object({
password: Joi.string().pattern((new RegExp('^[a-zA-Z0-9]{3,30}$'))),
repeat_password: Joi.any().equal(Joi.ref('password')).required().messages({
'any.only': '{{#label}} does not match' }),
email: Joi.string().email({ minDomainSegments: 2, tlds: { allow:
['com','dk']}})
});
const {error, value} = schema.validate(user)
// Then if the data is correct, the error will be undefined, so you can check like:
if (!error) {
// Everything is okay
} else {
// You can put a message here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.