Validation failed

VALIDATION_FAILED comes with HTTP 400. A field of your request, or your query, is not valid.


What it means

The API checked your request and found at least one problem. Nothing was searched. The errors array lists every problem it found, so you can fix them all at once.

Each entry has:

  • field: the request field, for example eventTypes or limit. A problem in the query language uses the field name badQuery.
  • issue: a readable text. The wording can change, so show it to people but do not parse it.
  • allowedValues: only for fields with a fixed list of values (include, orderBy, order, roles, eventTypes, eventDirections, minImportance, sourceTypes, excludeSourceTypes).

The detail says how many fields failed: 1 request field is not valid. or 3 request fields are not valid.

Example response

A wrong value in a field with a fixed list:

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"
      ]
    }
  ]
}

Several problems at once, for the body {"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" }
  ]
}

Common causes

Requestfieldissue
A value that is not in the list, for example "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" (values are lower case)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 and optInSources togetheroptInSourcesCan't have optInSources and sources defined simultaneously
An unknown field in the query, for example colour:bluebadQueryUnknown field "colour". Allowed fields: category, companyCount, ... To search for the literal text, quote it: "colour:blue".
GET /v3/companies without q and without idsq and 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.

Other query language problems also use the field badQuery and explain the problem in issue. A few query checks name the rule instead of a request field:

Queryfieldissue
Only negative terms, for example -teslapositiveTermRequiredQuery must include at least one non-negative keyword or phrase
No usable term at allmissingTermQuery must include at least one valid keyword or phrase
Too many terms or too deeply nestedcomplexityfor example Query too large (max 2000 terms)
More than 20,000 charactersmaxLengthQuery must be at most 20000 characters long

See Query syntax.

Two things do not cause this error:

  • Unknown request fields are dropped without an error. A misspelled field name, such as eventType instead of eventTypes, means the filter is simply not applied. The client libraries reject unknown fields before they send anything.
  • A date range outside your plan is cut to your plan's window without an error. See Plans & entitlements.

A request body that is not valid JSON also gets a 400, but in a simpler form without code. See Responses that are not problem+json.

How to fix

  1. Read errors[]. Each entry names the field and the problem.
  2. For a field with allowedValues, pick one of those values. Values are case-sensitive.
  3. For badQuery, check the field names in Fields. Put free text that contains a : in double quotes.
  4. Send the corrected request.

Retry

Do not retry. The same request fails the same way. The client libraries never retry a 400.

A request that fails this check still costs 1 credit, on the article routes and on the company lookup alike. See Rate limits & credits.

In the SDKs

LanguageError
TypeScript, Python, RubyValidationFailedError
.NET, Java, PHPValidationFailedException
Go*finlight.APIError with APIKind == finlight.APIKindValidationFailed
RustError::Api with api_kind() == ApiErrorKind::ValidationFailed

The entries of errors[] are available as errors (field, issue, allowed values).

Some mistakes never reach the API. The libraries check limit (1 to 100), empty languages, unknown arguments and the q/ids rule of the company lookup themselves and raise a local validation error (FinlightValidationError in TypeScript and Python; an unknown argument in Python is a TypeError). Those cost nothing.

Catch a validation error

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
  }
}

On WebSocket

A subscribe message that is not valid gets a connection.error frame with the code VALIDATION_FAILED. Then the server closes the connection. Do not reconnect with the same message. The client libraries end the stream with a validation error.

On the WebSocket the problems are in details[], each with field and 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"]
      }
    ]
  }
}

Common causes on the WebSocket:

  • A v2 key in a v3 subscribe: includeEntities or extended together with "payloadVersion": 3. Use include instead. The constraint text is use include with payloadVersion 3.
  • An unknown value in sourceTypes, excludeSourceTypes or (enriched only) include.
  • Any include on the raw stream. Raw delivers only the basic article fields.

See Subscribe and Raw stream.