2016-07-20 8 views
2

私はarangodb 3.0.2を使用しており、更新/パッチスキーマのときにjoi検証に問題があります。Foxxでの更新(パッチ)時の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) 

のようなユーザー・スキーマを持っているしかし、私は、ユーザーを更新した場合、および不明なフィールドを追加しても、エラーは発生しません。要求スキーマは検証されないためです。

コレクションに既に存在するフィールドを無視して、update/patchユーザーがスキーマを検証する方法はありますか?ルートの


更新:あなたが見ることができるよう

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"ではユーザースキーマとして設定されていないからです。しかし、そこにユーザスキーマを追加すると、必須のフィールドがチェックされるので、いくつかの既知のフィールドにパッチを当てることはできません。

+0

こんにちは@ de_3、あなたは、スキーマを使用しているルートの例を提供できますか?私は何が起こっているか完全にはわからない。 –

+0

こんにちは@AlanPlum、私は私の質問を更新しました、私はそこにユーザールートを追加しました。それは十分か? –

答えて

1

あなたは、創造(.post()ルート)、更新(.patch()ルート)の両方のために、特定のスキーマを確保したい場合は、(代わりに.body()にインライン二回、それを書くのに一度だけのスキーマを定義して、両方のルートでそれを参照してくださいDRY principle)。

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

しかし、あなたはPATCHルートのスキーマを使用していない:あなたが実際に変数Userに保存されているとPOSTルートで使用される、このようなスキーマを定義したよう

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) 

に見えます:

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

それだけreq.bodyがオブジェクトであることを保証しますが、任意のスキーマを強制しません。

関連する問題