为什么 Joi 验证在嵌套对象上抛出错误? (可能与名称属性中的点符号有关)

Why Joi validation throwing error on nested object? (potentially related to dot notation in name attribute)

我的 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 和 [=29= 添加了引号]键:

{
  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: ''
}

输入的名称属性是与 Mongoose 模式相关的“author.surname”。我假设引号已经添加到键中的某个地方作为对点符号的反应导致 Joi 抛出错误?我在正确的路线上吗?我怎样才能解决这个问题?非常感谢。

你好,我尝试了这个:

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: ''
}