Skip to main content

SKY-T105 · Quality, structure and performance

Unvalidated JSON type assertion

Medium

JSON.parse() and a fetch response's .json() return whatever the text contained. Asserting the result with as Settings only tells the compiler what to assume; nothing checks it. SKY-T105 reports such an assertion when no validation sits in between.

Languages
TypeScript
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A settings file edited by hand, or an API that changed its response, is accepted as if it matched the type. The mistake surfaces later as undefined in the middle of the program, or as a value from outside that was trusted without a check.

What Skylos flags

src/settings.ts · TypeScriptReported on line 4
type Settings = { theme: "light" | "dark"; fontSize: number }; export function loadSettings(raw: string): Settings {  return JSON.parse(raw) as Settings;}

What Skylos leaves alone

src/settings.ts · TypeScript
type Settings = { theme: "light" | "dark"; fontSize: number }; function isSettings(value: unknown): value is Settings {  const v = value as Partial<Record<keyof Settings, unknown>>;  const themeOk = v.theme === "light" || v.theme === "dark";  return themeOk && typeof v.fontSize === "number";} export function loadSettings(raw: string): Settings {  const data: unknown = JSON.parse(raw);  if (!isSettings(data)) {    throw new Error("settings file is not valid");  }  return data;}

Why Skylos leaves it alone

The parsed value is kept as unknown and only becomes Settings after the type guard has checked it. The one assertion left is to a type that still contains unknown, which is not a claim about the data.

More languages

TypeScript

Flagged

src/api/users.ts · TypeScriptReported on line 5
type User = { id: string; email: string }; export async function fetchUser(id: string): Promise<User> {  const res = await fetch(`/api/users/${id}`);  return (await res.json()) as User;}

Not flagged

src/api/users.ts · TypeScript
type User = { id: string; email: string }; export async function fetchUser(id: string): Promise<unknown> {  const res = await fetch(`/api/users/${id}`);  return await res.json();}

Why Skylos leaves it alone

The response is returned as unknown, so every caller has to check it before using it as a User.

How Skylos decides

  1. Runs with --quality on .ts, .tsx, .mts and .cts files. Declaration files, generated files and files that do not parse are skipped.
  2. Finds an assertion (x as T or <T>x) to a precise type: not as const, and with no any, unknown, object or {} inside it.
  3. Its operand, looking through parentheses, await, ! and satisfies, must be either a call to the global JSON.parse with one argument that is not a fixed string, or an awaited .json() call with no arguments on a response that comes straight from await fetch(...) or from a const assigned await fetch(...).
  4. Gives up when JSON, JSON.parse, fetch or Response.prototype.json is shadowed or reassigned in the file, or when the file is too large to analyse completely.
  5. Reports MEDIUM on the line where the assertion starts.

How to fix it

Keep the parsed value as unknown and check it with a type guard or a schema library such as Zod before using it as the type.

src/settings.ts · TypeScript · the fix, against the flagged file
type Settings = { theme: "light" | "dark"; fontSize: number }; Added: function isSettings(value: unknown): value is Settings {Added:   const v = value as Partial<Record<keyof Settings, unknown>>;Added:   const themeOk = v.theme === "light" || v.theme === "dark";Added:   return themeOk && typeof v.fontSize === "number";Added: }Added:  export function loadSettings(raw: string): Settings {Removed:   return JSON.parse(raw) as Settings;Added:   const data: unknown = JSON.parse(raw);Added:   if (!isSettings(data)) {Added:     throw new Error("settings file is not valid");Added:   }Added:   return data;}

When it can be wrong

  • JSON that the program wrote itself a moment earlier, for example a value read back from its own cache, is reported although it cannot have another shape.
  • It misses a .json() call on a response that came from a wrapper such as axios or a helper function, and an assertion done in a separate step (const data = JSON.parse(raw); return data as T).

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line:

    TypeScript
    return JSON.parse(raw) as Settings; // skylos: ignore[SKY-T105] written by saveSettings
  • [tool.skylos] ignore = ["SKY-T105"] in pyproject.toml turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.