MongoDB 模式中基于对等的验证

Peer Based Validation in MongoDB Schema

我正在使用 MongoDB 架构来验证插入数据库的数据。我希望有类似于 Joi 中的 Joi.when() 的基于对等的验证。 数据如下所示:

first:"Teddy",
last:"Bear"

只有当 first 不为空时,我如何使 last requiredmaxLength:10 ?我需要在 MongoDB jsonSchema:

中与此等效的内容
last: Joi.string().when('first', { is: null, then: Joi.required().max(10), otherwise:Joi.forbidden() }),

你不能。在 json 架构的 draft-07 a year ago. Mongo uses 7 years old draft-04 中引入了条件验证 if-then-else。