[ADP-404] 驗證錯誤設計
對於問題類型為 validation-error
的情況,服務器預期會返回多個關於失敗部分的錯誤實例。
必須為驗證錯誤提供錯誤對象資訊數組(在 errors 中,因為預期會有多個)
屬性(類型,格式) 描述 必填 示例 detail(字符串) 當前錯誤實例的詳細描述 是 path(字符串) 如果在 body 中,則為驗證錯誤的 JSON 指針;如果在 header/query 中,則為鍵名;如果在 path 中,則為相對 URI 引用 JSON 指針:'#/key1/key2' 是 in(字符串;枚舉) 'header'、'body'、'query'、'path' 之一,與 OpenAPI 中 https://swagger.io/docs/specification/describing-parameters/ 保持一致 是 details(數組,可選) 如果在路徑中有多個錯誤,需要提供包含 { key, message } 的詳細對象。 否
單個驗證錯誤對象的格式
json
[
{
"in": "[path|query|header|body]",
"path": "#/json/pointer/to/failed/field",
"invalidValue": "Optional invalid client input as a string.",
"details": [
{
"key": "machine.readable.key1",
"message": "First human-readable error message."
},
{
"key": "machine.readable.key2",
"message": "Another human-readable error message."
}
]
}
]
OpenAPI 3.1 範例
yaml
components:
schemas:
ValidationError:
type: object
required:
- in
- path
- detail
properties:
in:
type: string
enum: [path, query, header, body]
path:
type: string
detail:
type: string
invalidValue:
type: string
details:
type: array
items:
type: object
required:
- key
- message
properties:
key:
type: string
message:
type: string
ValidationErrorResponse:
type: object
required:
- type
- title
- status
- errors
properties:
type:
type: string
const: 'validation-error'
title:
type: string
status:
type: integer
enum: [400]
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
paths:
/example:
post:
responses:
'400':
description: Bad Request
content:
application/problem+json:
schema:
$ref: '#/components/schemas/ValidationErrorResponse'