Skip to content

How strict should my response decoder be about extra or missing optional fields?

Require the fields your application actually needs for a specific response variant, validate their types, and tolerate unrelated optional additions unless security requires otherwise. Do not apply AISwapLab's strict request-field policy blindly to response decoding. Requests you control and evolving responses have different compatibility needs.

TEST THE CUSTOMER API CONTRACTAISwapLab team · Editorial methodUpdated 2 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: List essentials per variant; Separate recognition from retention; Test additions and omissions.
Workflow diagram: List essentials per variant → Separate recognition from retention → Test additions and omissions. Read the article for evidence and limits.

Strictness belongs at the right boundary

A decoder that rejects every new optional field can break after a harmless contract extension. A decoder that accepts any object can continue with a missing job ID. Document essential invariants rather than choosing universally strict or universally permissive parsing.

What to do next

  1. 01

    List essentials per variant

    For a job response, identify the fields needed to track that job and interpret its state. For a confirmed chat response, identify its own completion fields. Avoid one decoder that assumes every success has the same shape.

  2. 02

    Separate recognition from retention

    Validate essential fields before constructing domain objects. Preserve only the reviewed subset in normal application state, while optionally recording a redacted raw envelope for diagnostics; do not spread arbitrary properties into privileged records.

  3. 03

    Test additions and omissions

    Add an irrelevant optional response property and verify compatibility. Then remove an essential field or change its type and verify a clear decode failure that retains task context instead of continuing with undefined identifiers. Include HTTP 200 with HTML, an empty object or another operation's body: transport success must not bypass the same essential-field checks.

A concrete example

Illustrative example: a job fixture adds a harmless descriptive field, which the decoder ignores. Another HTTP 200 fixture omits the job ID; it is rejected, preserving action context instead of constructing /jobs/undefined or claiming a finished result.

Before you call it ready

  • Optional additions remain compatible.
  • Essential omissions fail clearly.
  • External properties are not blindly merged.

A locally executed, reproducible check

Zod’s ordinary object schema strips an extra key in parsed output, whereas strict mode rejects it.

We executed this exact synthetic fixture on November 16, 2026 with Node v24.16.0 and Zod 3.25.76. It ran locally with no network requests, account data or model generation. The recorded output below is an observation from that environment, not a platform benchmark. Copy the code into a separate CommonJS file to repeat it with the stated dependencies in a suitable local Node environment; compare the mechanism and check your runtime version.

javascript
const {z}=require('zod');
const input={status:'complete',future_metadata:'synthetic'};
const schema=z.object({status:z.string()});
console.log('normal parsed:',JSON.stringify(schema.parse(input)));
console.log('strict accepted:',schema.strict().safeParse(input).success);
console.log('original key retained:',Object.hasOwn(input,'future_metadata'));

Recorded output and interpretation

This records the installed Zod 3 behavior, not a recommendation to accept arbitrary fields or a platform schema change. Required fields, authorization, error unions and documented statuses still need their own validation. Parsed output and the original input are different objects.

A matching local output establishes only the illustrated library or database behavior under these inputs. It does not certify a production integration. Preserve the approved action, authorization boundaries and current AISwapLab request contract when applying the idea.

text
normal parsed: {"status":"complete"}
strict accepted: false
original key retained: true

When to stop or choose another method

Stop relying on a static type assertion as proof that a network response contains the essential fields your next action requires.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation