【问题标题】:Joi validation when update (patch) in FoxxFoxx 中更新(补丁)时的 Joi 验证
【发布时间】:2016-07-20 03:12:01
【问题描述】:

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

【问题讨论】:

  • 嗨@de_3,您能否提供一个使用架构的路由示例?我不完全确定发生了什么。
  • 嗨@AlanPlum,我已经更新了我的问题,我已经在那里添加了用户路线。够了吗?

标签: arangodb foxx


【解决方案1】:

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

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 是一个对象,但不强制执行任何架构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多