2

我使用 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”上我没有设置为用户模式。但是如果在那里添加用户模式,它会检查必填字段,所以我不能只修补一些已知字段。

4

1 回答 1

1

如果要确保创建(.post()路由)和更新(.patch()路由)都具有特定架构,请确保仅定义一次架构并在两个路由中引用它,而不是在中内联写入两次.body()DRY 原则)。

let userSchema = joi.object().keys({
  _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(...)
  .body(userSchema)

router.patch(...)
  .body(userSchema)

看起来您实际上定义了这样的模式,存储在变量User中并在 POST 路由中使用:

.body(User, 'The user to create.')

但是您不要在 PATCH 路由中使用架构:

.body(joi.object().description('The data to update the user with.'))

它只确保它req.body是一个对象,但不强制执行任何模式。

于 2016-09-14T13:12:24.487 回答