JSON API指定應該單獨列出屬性/參數上的多個錯誤。處理涉及多個參數的單個錯誤的最佳方法是什麼?例如,假設一個端點接受bid
或fold
參數,但不是兩者(它們是互斥的)。如果提交了兩個參數(GET /endpoint?bid=100.00&fold=muck
),應該是什麼錯誤響應?使用JSON API處理具有多個屬性或參數的錯誤
列出錯誤兩次,每次屬性一次?
{
"errors": [
{
"status": "400",
"source": { "parameter": "bid" },
"detail": "Cannot accept both 'bid' and 'fold' parameters."
},
{
"status": "400",
"source": { "parameter": "fold" },
"detail": "Cannot accept both 'bid' and 'fold' parameters."
}
]
}
結合屬性?
{
"errors": [
{
"status": "400",
"source": { "parameter": ["bid", "fold"] },
"detail": "Cannot accept both 'bid' and 'fold' parameters."
}
]
}
爲整個請求創建一個更高級別的錯誤?
{
"errors": [
{
"status": "400",
"source": { "pointer": "/data" },
"detail": "Cannot accept both 'bid' and 'fold' parameters."
}
]
}
一些其他的方式?
FWIW,在我們的實現中,如果兩個參數衝突,我們只返回一個沒有錯誤的空集。我們選擇了一個有爭議的屬性來指向POST/PATCH,而不是在兩者上都做出錯誤。 – jdubjdub