提交表單時,我的 joi 驗證拋出錯誤。錯誤訊息如下:
堆疊:錯誤:module.exports.citationJoi 中不允許“author.surname”
據我所知,我的 joiSchema 沒問題:
const nestedScehma = Joi.object().keys({
surname: Joi.string().trim().required(),
firstname: Joi.string().trim().required()
})
module.exports.citationJoi = (req, res, next) => {
const memberSchema = Joi.object().keys({
quote: Joi.string().trim().required(),
author: nestedScehma,
page: Joi.string().allow(null, '').trim(),
year: Joi.string().allow(null, '').trim(),
title: Joi.string().allow(null, '').trim(),
publisher: Joi.string().allow(null, '').trim(),
place: Joi.string().allow(null, '').trim()
})
const { error } = memberSchema.validate(req.body);
if(error) {
const msg = error.details.map(el => el.message).join(",");
throw new AppError(msg, 400);
} else {
next();
}
}
這與我的貓鼬模式有關:
const quoteScheme = new Schema({
quote: {
type: String,
required: true
},
author: {
surname: {
type: String,
required: true
},
firstname: {
type: String,
required: true
}
},
page: {
type: String,
required: false
},
year: {
type: String,
required: false
},
title: {
type: String,
required: false
},
publisher: {
type: String,
required: false
},
place: {
type: String,
required: false
}
})
我已經在這里呆了一段時間了,唯一的奇怪(至少在我的新手心中)是在 req.body 中添加了對 author.surname 和 author.firstname 鍵的參考:
{
quote: "Ozzy Osbourne's advice on how rock stars should treat young bands is wholesome as heck",
'author.surname': 'tosser',
'author.firstname': 'Oscar',
page: '',
year: '',
title: '',
publisher: '',
place: ''
}
輸入的 name 屬性是與 Mongoose 模式相關的“author.surname”。我認為在某處將引號添加到鍵中作為對點符號的反應導致 Joi 拋出錯誤?我在正確的路線上嗎?我怎樣才能解決這個問題?非常感謝。
uj5u.com熱心網友回復:
你好,我通過嘗試這個得到了這個作業:
module.exports.citationJoi = (req, res, next) => {
const memberSchema = Joi.object({
quote: Joi.string().trim().required(),
"author.surname": Joi.string().trim().required(),
"author.firstname": Joi.string().trim().required(),
page: Joi.string().allow(null, "").trim(),
year: Joi.string().allow(null, "").trim(),
title: Joi.string().allow(null, "").trim(),
publisher: Joi.string().allow(null, "").trim(),
place: Joi.string().allow(null, "").trim(),
})
const {
error
} = memberSchema.validate(req.body);
if (error) {
const msg = error.details.map(el => el.message).join(",");
throw new AppError(msg, 400);
} else {
next();
}
}
如果這是傳入的有效負載,它應該可以作業:
{
quote: "Ozzy Osbourne's advice on how rock stars should treat young bands is wholesome as heck",
'author.surname': 'tosser',
'author.firstname': 'Oscar',
page: '',
year: '',
title: '',
publisher: '',
place: ''
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/456277.html
標籤:javascript 节点.js 表示 猫鼬 乔伊
上一篇:將嵌套JSON檔案轉換和存盤為MongoDB檔案的最佳實踐是什么?
下一篇:在Django中參考靜態檔案
