我正在尝试验证 POST 请求,其中 POST 请求title可以是带有语言键和值的字符串或对象。例子:
{
title: 'Chicken',
...
}
//OR
{
title: {
en_US: 'Chicken',
de_DE: 'Hähnchen'
}
...
}
Run Code Online (Sandbox Code Playgroud)
和 Joi 我试图像这样验证:
{
title: Joi.any().when('title', {
is: Joi.string(),
then: Joi.string().required(),
otherwise: Joi.object().keys({
en_US: Joi.string().required(),
lt_LT: Joi.string()
}).required()
}),
...
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试验证时出现错误AssertionError [ERR_ASSERTION]: Item cannot come after itself: title(title)
有没有办法使用when相同的字段?
您好,我想知道在保存文档的“状态”方面有什么更好的方法?我能想到的两种方法是使用字符串结尾枚举:
const proposal = new Schema({
state: {
type: String,
enum: ['pending', 'approved', 'denied'],
default: 'pending'
}
});
Run Code Online (Sandbox Code Playgroud)
或使用布尔值:
const proposal = new Schema({
approved: {
type: Boolean,
default: false
},
denied: {
type: Boolean,
default: false
}
});
Run Code Online (Sandbox Code Playgroud)
在性能和安全性方面,哪一个更好?在外面,搜索似乎booleans比string搜索更快。