TypeScript · correctness
prefer-schema-for-api-payload
eslint:prefer-schema-for-api-payload Require Zod (or similar) schema validation on `response.json()` / `JSON.parse()` results before property access.
- Default
- error
- Fix
- none
- Languages
- typescript
Why
External JSON is untrusted at runtime even when its expected TypeScript shape is known statically.
Fix
Parse the payload through a schema or establish a recognized runtime validation guard before reading fields.
Before / after
Executed by this rule’s unit tests.
Before
Do not trust response JSON directly
async function load(response) { const body = await response.json(); return body.id; } After
Validate before property access
async function load(response) { const body = UserSchema.parse(await response.json()); return body.id; } Limits
- Test fixtures, generated clients, local JSON files, and recognized validation guards are excluded.
Message IDs
unparsedJsonAccess