2016-07-20 62 views
2

我使用arangodb 3.0.2,並在更新/修補程序模式時遇到joi驗證問題。Joi在Foxx中更新(修補程序)時進行驗證

我有這樣

_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. 
`); 

這是我的路線,你可以看到。當我發佈新用戶時,它會驗證用戶架構,所以如果我添加未知字段,它會給我一些錯誤。

但我補丁用戶,它不會驗證用戶架構,因爲在「身體」我沒有設置爲用戶架構。但是,如果添加用戶架構,它會檢查必填字段,所以我不能只修補一些已知字段。

+0

Hi @ de_3,你能提供一個你使用模式的路線的例子嗎?我不完全確定發生了什麼事。 –

+0

嗨@AlanPlum,我已經更新了我的問題,我在那裏添加了用戶路線。這夠了嗎? –

回答

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.') 

但你不使用補丁路由模式:

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

它只確保req.body是一個對象,但不強制任何模式。

相關問題