【问题标题】:API validation errors formatAPI 验证错误格式
【发布时间】:2017-08-22 07:51:46
【问题描述】:

1

{
  "errors": {
    "firstName": ["cannot be blank", "must be 10+ characters"],
    "lastName": ["cannot contain symbols"]
  }
}

2

{
  "errors": [
    { "path": "firstName", "message": "cannot be blank" }, 
    { "path": "firstName", "message": "must be 10+ characters" },
    { "path": "lastName", "message": "cannot contain symbols" }
  ]
}

你喜欢什么格式,为什么,它有什么优势?

【问题讨论】:

    标签: api validation error-handling


    【解决方案1】:

    我总是建议采用现有标准。有一个媒体类型application/problem+json 可能很适合这里。它的规范在这里https://datatracker.ietf.org/doc/html/draft-nottingham-http-problem-07,更像是您的第二个示例:为每个验证或其他问题提供一份报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多