我使用 arangodb 3.0.2 并且在更新/修补架构时遇到 joi 验证问题。
我有这样的用户架构
_key: joi.string(),
name: joi.string().required(),
username: joi.string(),
email: joi.string().required(),
profilePicture: joi.string(),
provider: joi.object().keys({
name: joi.string(),
id: joi.string()
}),
interest: joi.array().items(joi.string()),
level: joi.number().default(0)
当我创建新用户并尝试添加未知字段时,例如状态会抛出错误,
但是如果我更新用户并添加未知字段,它不会引发任何错误。因为它不验证请求模式。
如何在更新/修补用户时验证架构,忽略集合中已经存在的字段?
路线更新:
router.post(function (req, res) {
const user = req.body;
let provider = user.provider.name;
let id = user.provider.id;
let meta;
try {
meta = users.save(user);
} catch (e) {
if (e.isArangoError && e.errorNum === ARANGO_DUPLICATE) {
throw httpError(HTTP_CONFLICT, e.message);
}
throw e;
}
Object.assign(user, meta);
res.status(201);
res.set('location', req.makeAbsolute(
req.reverse('detail', {key: user._key})
));
res.send(user);
}, 'create')
.body(User, 'The user to create.')
.response(201, User, 'The created user.')
.error(HTTP_CONFLICT, 'The user already exists.')
.summary('Create a new user')
.description(dd`
Creates a new user from the request body and
returns the saved document.
`);
router.patch(':key', function (req, res) {
const key = req.pathParams.key;
const patchData = req.body;
let user;
try {
users.update(key, patchData);
user = users.document(key);
} catch (e) {
if (e.isArangoError && e.errorNum === ARANGO_NOT_FOUND) {
throw httpError(HTTP_NOT_FOUND, e.message);
}
if (e.isArangoError && e.errorNum === ARANGO_CONFLICT) {
throw httpError(HTTP_CONFLICT, e.message);
}
throw e;
}
res.send(user);
}, 'update')
.pathParam('key', keySchema)
.body(joi.object().description('The data to update the user with.'))
.response(User, 'The updated user.')
.summary('Update a user')
.description(dd`
Patches a user with the request body and
returns the updated document.
`);
如您所见,这是我的路线。当我发布新用户时,它会验证用户模式,所以如果我添加未知字段,它会给我一些错误。
但是我修补了用户,它不会验证用户模式,因为在“body”上我没有设置为用户模式。但是如果在那里添加用户模式,它会检查必填字段,所以我不能只修补一些已知字段。