Validation failed
VALIDATION_FAILED は HTTP 400 とともに返ります。リクエストのフィールド、またはクエリが有効ではありません。
意味
API がリクエストをチェックし、少なくとも 1 つの問題を見つけました。検索は行われていません。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
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" }
]
}
よくある原因
| リクエスト | field | issue |
|---|---|---|
リストにない値。例: "eventTypes": ["earnigns"] | eventTypes | each value in eventTypes must be one of the following values: earnings, ... |
"sourceTypes": ["wire"] | sourceTypes | each value in sourceTypes must be one of the following values: newswire, press_release, financial_media, general_media, aggregator, blog, government |
"countries": ["USA"] | countries | each value in countries must be a valid ISO31661 Alpha2 code |
"order": "DESC"(値は小文字) | order | order must be one of the following values: asc, desc |
"limit": 0 | limit | limit must not be less than 1 |
"limit": 500 | limit | limit must not be greater than 100 |
"languages": [] | languages | languages should not be empty |
sources と optInSources の併用 | optInSources | Can't have optInSources and sources defined simultaneously |
クエリ内の未知のフィールド。例: colour:blue | badQuery | Unknown field "colour". Allowed fields: category, companyCount, ... To search for the literal text, quote it: "colour:blue". |
q も ids もない GET /v3/companies | q と ids | Either q or ids has to be given. |
GET /v3/companies?q=NVDA&ids=1 | ids | q and ids cannot be combined. Use one of the two. |
GET /v3/companies?ids=1,abc | ids | each value in ids must not be less than 1, each value in ids must be an integer number |
GET /v3/companies/abc | companyId | companyId has to be a whole number. |
その他のクエリ言語の問題もフィールド badQuery を使い、issue で問題を説明します。一部のクエリのチェックは、リクエストフィールドではなくルールの名前を示します:
| クエリ | field | issue |
|---|---|---|
否定の語だけ。例: -tesla | positiveTermRequired | Query must include at least one non-negative keyword or phrase |
| 使える語がまったくない | missingTerm | Query must include at least one valid keyword or phrase |
| 語が多すぎる、またはネストが深すぎる | complexity | 例: Query too large (max 2000 terms) |
| 20,000 文字を超える | maxLength | Query must be at most 20000 characters long |
クエリ構文を参照してください。
次の 2 つはこのエラーの原因になりません:
- 未知のリクエストフィールドは、エラーなしで捨てられます。
eventTypesの代わりにeventTypeのようにフィールド名を間違えると、フィルターが適用されないだけです。クライアントライブラリは、送信前に未知のフィールドを拒否します。 - プランの範囲外の日付範囲は、エラーなしでプランの範囲に切り詰められます。プランと利用権限を参照してください。
有効な JSON ではないリクエストボディも 400 になりますが、code のない、より単純な形式です。problem+json ではないレスポンスを参照してください。
直し方
errors[]を読みます。各エントリがフィールドと問題を示します。allowedValuesのあるフィールドでは、それらの値から 1 つ選びます。値は大文字小文字を区別します。badQueryでは、フィールドでフィールド名を確認してください。:を含む自由テキストはダブルクォートで囲みます。- 直したリクエストを送ります。
リトライ
リトライしないでください。同じリクエストは同じように失敗します。クライアントライブラリは 400 をリトライしません。
このチェックで失敗したリクエストも、記事のルートでも企業のルックアップでも、1 クレジットかかります。レート制限とクレジットを参照してください。
SDK での扱い
| 言語 | エラー |
|---|---|
| TypeScript, Python, Ruby | ValidationFailedError |
| .NET, Java, PHP | ValidationFailedException |
| Go | APIKind == finlight.APIKindValidationFailed の *finlight.APIError |
| Rust | api_kind() == ApiErrorKind::ValidationFailed の Error::Api |
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 のキー:
"payloadVersion": 3と一緒にincludeEntities、extendedを指定した。代わりにincludeを使ってください。制約のテキストはuse include with payloadVersion 3です。 sourceTypes、excludeSourceTypes、または(エンリッチのみ)includeの未知の値。- Raw ストリームでの
include。Raw は記事の基本フィールドだけを配信します。