Joi 数组验证无法转换为数组
Joi Array validation unable to cast to array
我正在使用 Joi
进行架构验证。我有一个对象数组,我想用 Joi
对其进行验证。这是代码:
const joi = require("joi");
function validateUser(user) {
let phone = joi.object().keys({
id: joi.string().required(),
phoneNumber: joi.string().required()
});
const schema = {
firstName: joi.string().required(),
lastName: joi.string().required(),
email: joi
.string()
.email()
.required(),
phoneNumbers: joi.array().items(phone)
};
return joi.validate(user, schema).error;
}
这是我传递给 validateUser
函数的 user
对象。
{
firstName: 'User',
lastName: 'One',
email: 'userone@gmail.com'
phoneNumbers: [
{
id: '03bb22cc-499a-4464-af08-af64d5a52675',
phoneNumber: '13001234567'
},
{
id: '50e32458-756b-4aaa-b3dc-19a2f696ab6c',
phoneNumber: '13031234567'
}
]
}
但是,它向我显示以下错误。
UnhandledPromiseRejectionWarning: ValidationError: user validation failed: phoneNumbers: Cast to Array failed for value "[
{
id: '03bb22cc-499a-4464-af08-af64d5a52675',
phoneNumber: '13001234567'
},
{
id: '50e32458-756b-4aaa-b3dc-19a2f696ab6c',
phoneNumber: '13031234567'
}
]" at path "phoneNumbers"
我不知道这里发生了什么。您能提出解决方案吗?
根据官方文档,Joi
架构是正确的。问题出在我的 Mongoose
架构上。以前是:
const User = mongoose.model(
"user",
new mongoose.Schema(
{
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phoneNumbers: {
type: [String],
default: []
}
},
{ timestamps: true }
)
);
当我把它改成:
时它工作正常
const User = mongoose.model(
"user",
new mongoose.Schema(
{
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phoneNumbers: {
type: [{ id: String, phoneNumber: String}],
default: []
}
},
{ timestamps: true }
)
);
我正在使用 Joi
进行架构验证。我有一个对象数组,我想用 Joi
对其进行验证。这是代码:
const joi = require("joi");
function validateUser(user) {
let phone = joi.object().keys({
id: joi.string().required(),
phoneNumber: joi.string().required()
});
const schema = {
firstName: joi.string().required(),
lastName: joi.string().required(),
email: joi
.string()
.email()
.required(),
phoneNumbers: joi.array().items(phone)
};
return joi.validate(user, schema).error;
}
这是我传递给 validateUser
函数的 user
对象。
{
firstName: 'User',
lastName: 'One',
email: 'userone@gmail.com'
phoneNumbers: [
{
id: '03bb22cc-499a-4464-af08-af64d5a52675',
phoneNumber: '13001234567'
},
{
id: '50e32458-756b-4aaa-b3dc-19a2f696ab6c',
phoneNumber: '13031234567'
}
]
}
但是,它向我显示以下错误。
UnhandledPromiseRejectionWarning: ValidationError: user validation failed: phoneNumbers: Cast to Array failed for value "[
{
id: '03bb22cc-499a-4464-af08-af64d5a52675',
phoneNumber: '13001234567'
},
{
id: '50e32458-756b-4aaa-b3dc-19a2f696ab6c',
phoneNumber: '13031234567'
}
]" at path "phoneNumbers"
我不知道这里发生了什么。您能提出解决方案吗?
Joi
架构是正确的。问题出在我的 Mongoose
架构上。以前是:
const User = mongoose.model(
"user",
new mongoose.Schema(
{
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phoneNumbers: {
type: [String],
default: []
}
},
{ timestamps: true }
)
);
当我把它改成:
时它工作正常const User = mongoose.model(
"user",
new mongoose.Schema(
{
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
email: {
type: String,
required: true
},
phoneNumbers: {
type: [{ id: String, phoneNumber: String}],
default: []
}
},
{ timestamps: true }
)
);