Arangodb Foxx中更新(补丁)时的Joi验证
我使用Arangodb3.0.2,在更新/修补模式时,joi验证有问题 我有这样的用户模式Arangodb Foxx中更新(补丁)时的Joi验证,arangodb,foxx,Arangodb,Foxx,我使用Arangodb3.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.strin
_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”上我并没有设置为用户模式。但若在那个里添加用户模式,它将检查必填字段,所以我不能只修补一些已知字段 如果要确保创建(
.post()
route)和更新(.patch()
route)都有特定的架构,请确保只定义一次架构,并在两个路由中引用它,而不是在.body()
中内联写入两次
看起来您实际上定义了这样一个模式,存储在变量User
中,并在POST路由中使用:
.body(User, 'The user to create.')
但在修补程序路径中不使用架构:
.body(joi.object().description('The data to update the user with.'))
它只确保
req.body
是一个对象,但不强制任何模式。Hi@de_3,您能提供一个使用模式的路由示例吗?我不完全确定发生了什么。嗨@AlanPlum,我更新了我的问题,我在那里添加了用户路线。够了吗?
.body(joi.object().description('The data to update the user with.'))