本页面为机器翻译。英文版本为原文,可能更准确或更及时。 查看英文版

校验失败

VALIDATION_FAILED 与 HTTP 400 一起返回。您请求中的某个字段或您的查询无效。


含义

API 检查了您的请求,发现至少一个问题。没有执行任何搜索。errors 数组列出了发现的每个问题,因此您可以一次性全部修正。

每一项包含:

  • field:请求字段,例如 eventTypes 或 limit。查询语言中的问题使用字段名 badQuery。
  • issue:易读的文本。措辞可能会改变,所以可以展示给用户,但不要解析它。
  • allowedValues:仅用于有固定取值列表的字段(include、orderBy、order、roles、eventTypes、eventDirections、minImportance、sourceTypes、excludeSourceTypes)。

detail 会说明有多少个字段失败:1 request field is not valid. 或 3 request fields are not valid.

响应示例

有固定取值列表的字段中出现错误取值:

Request

POST
/v3/articles/search
curl -X POST 'https://api.finlight.me/v3/articles/search' \
  -H 'Content-Type: application/json' \
  -H 'X-API-KEY: YOUR_API_KEY' \
  -H 'X-Request-Id: req-123' \
  -d '{ "eventTypes": ["earnigns"] }'

Response (400)

{
  "type": "https://docs.finlight.me/errors/validation-failed",
  "title": "Validation failed",
  "status": 400,
  "detail": "1 request field is not valid.",
  "code": "VALIDATION_FAILED",
  "requestId": "req-123",
  "errors": [
    {
      "field": "eventTypes",
      "issue": "each value in eventTypes must be one of the following values: earnings, guidance, mna, ipo_listing, capital_action, rating_action, management_change, legal, regulatory, bankruptcy_distress, product_partnership, investment_expansion, operational_incident, labor_restructuring, macro, geopolitics",
      "allowedValues": [
        "earnings", "guidance", "mna", "ipo_listing", "capital_action",
        "rating_action", "management_change", "legal", "regulatory",
        "bankruptcy_distress", "product_partnership", "investment_expansion",
        "operational_incident", "labor_restructuring", "macro", "geopolitics"
      ]
    }
  ]
}

请求体 {"limit": 500, "order": "DESC"} 同时存在多个问题:

Response (400)

{
  "type": "https://docs.finlight.me/errors/validation-failed",
  "title": "Validation failed",
  "status": 400,
  "detail": "2 request fields are not valid.",
  "code": "VALIDATION_FAILED",
  "requestId": "req-123",
  "errors": [
    { "field": "order", "issue": "order must be one of the following values: asc, desc", "allowedValues": ["asc", "desc"] },
    { "field": "limit", "issue": "limit must not be greater than 100" }
  ]
}

常见原因

请求fieldissue
不在列表中的取值,例如 "eventTypes": ["earnigns"]eventTypeseach value in eventTypes must be one of the following values: earnings, ...
"sourceTypes": ["wire"]sourceTypeseach value in sourceTypes must be one of the following values: newswire, press_release, financial_media, general_media, aggregator, blog, government
"countries": ["USA"]countrieseach value in countries must be a valid ISO31661 Alpha2 code
"order": "DESC"(取值为小写)orderorder must be one of the following values: asc, desc
"limit": 0limitlimit must not be less than 1
"limit": 500limitlimit must not be greater than 100
"languages": []languageslanguages should not be empty
同时使用 sources 和 optInSourcesoptInSourcesCan't have optInSources and sources defined simultaneously
查询中的未知字段,例如 colour:bluebadQueryUnknown field "colour". Allowed fields: category, companyCount, ... To search for the literal text, quote it: "colour:blue".
GET /v3/companies 既没有 q 也没有 idsq 和 idsEither q or ids has to be given.
GET /v3/companies?q=NVDA&ids=1idsq and ids cannot be combined. Use one of the two.
GET /v3/companies?ids=1,abcidseach value in ids must not be less than 1, each value in ids must be an integer number
GET /v3/companies/abccompanyIdcompanyId has to be a whole number.

其他查询语言问题也使用字段 badQuery,并在 issue 中说明问题。少数查询检查会给出规则名称,而不是请求字段:

查询fieldissue
只有否定词项,例如 -teslapositiveTermRequiredQuery must include at least one non-negative keyword or phrase
完全没有可用的词项missingTermQuery must include at least one valid keyword or phrase
词项过多或嵌套过深complexity例如 Query too large (max 2000 terms)
超过 20,000 个字符maxLengthQuery must be at most 20000 characters long

请参阅查询语法。

有两种情况不会导致这个错误:

  • 未知的请求字段会被丢弃,且不报错。拼写错误的字段名,例如把 eventTypes 写成 eventType,只会导致过滤条件不生效。客户端库会在发送前拒绝未知字段。
  • 超出套餐范围的日期范围会被裁剪到套餐的窗口内,且不报错。请参阅套餐与权益。

不是有效 JSON 的请求体也会得到 400,但采用不带 code 的简单形式。请参阅不是 problem+json 的响应。

如何修正

  1. 阅读 errors[]。每一项都指明了字段和问题。
  2. 对于带 allowedValues 的字段,请从中选择一个取值。取值区分大小写。
  3. 对于 badQuery,请在字段中检查字段名。把包含 : 的自由文本放在双引号中。
  4. 发送修正后的请求。

重试

不要重试。相同的请求会以同样的方式失败。客户端库永远不会重试 400。

未通过此检查的请求仍会消耗 1 个额度,文章路由和公司查询都一样。请参阅速率限制与额度。

在 SDK 中

语言错误
TypeScript、Python、RubyValidationFailedError
.NET、Java、PHPValidationFailedException
Go*finlight.APIError,其中 APIKind == finlight.APIKindValidationFailed
RustError::Api,其中 api_kind() == ApiErrorKind::ValidationFailed

errors[] 中的条目可以通过 errors 获取(字段、问题、允许的取值)。

有些错误永远不会到达 API。各库会自行检查 limit(1 到 100)、空的 languages、未知参数以及公司查询的 q/ids 规则,并抛出本地校验错误(TypeScript 和 Python 中为 FinlightValidationError;Python 中的未知参数为 TypeError)。这些不会产生任何花费。

捕获校验错误

import { ValidationFailedError } from 'finlight-client'

try {
  await client.articles.search({ eventTypes: ['earnigns'] })
} catch (err) {
  if (err instanceof ValidationFailedError) {
    for (const e of err.errors) {
      console.log(e.field, e.issue, e.allowedValues)
    }
  } else {
    throw err
  }
}

在 WebSocket 上

无效的订阅消息会收到代码为 VALIDATION_FAILED 的 connection.error 帧。然后服务器会关闭连接。不要用相同的消息重新连接。客户端库会以校验错误结束数据流。

在 WebSocket 上,问题位于 details[] 中,每一项都带有 field 和 constraints:

connection.error

{
  "event": "connection.error",
  "payloadVersion": 3,
  "data": {
    "code": "VALIDATION_FAILED",
    "message": "Invalid subscription message",
    "details": [
      {
        "field": "sourceTypes",
        "constraints": {
          "isIn": "each value in sourceTypes must be one of the following values: newswire, press_release, financial_media, general_media, aggregator, blog, government"
        },
        "allowedValues": ["newswire", "press_release", "financial_media", "general_media", "aggregator", "blog", "government"]
      }
    ]
  }
}

WebSocket 上的常见原因:

  • 在 v3 订阅中使用了 v2 的键:includeEntities 或 extended 与 "payloadVersion": 3 同时出现。请改用 include。约束文本为 use include with payloadVersion 3。
  • sourceTypes、excludeSourceTypes 或(仅限增强版)include 中有未知取值。
  • 在原始数据流上使用任何 include。原始数据流只投递基本的文章字段。

请参阅订阅和原始数据流。