Skip to content

[Feature] Add runtime API response schema validation to prevent silent data corruption #82

@numbers-official

Description

@numbers-official

Summary

The asset-service.ts functions (fetchAsset, hasNftProduct, fetchAssetMetadata) directly destructure API responses without any runtime validation. If the Numbers Protocol API changes field names, response structure, or data types, the component silently renders undefined values with no error signal — making debugging extremely difficult.

Affected Files

  • src/asset/asset-service.ts (lines 21-66, 80-83, 101-111)

Current Behavior

// asset-service.ts line 21-23 — direct destructuring with no validation
const {
  response: { data },
} = await response.json();

// Lines 43-63 — direct field access, any missing field silently becomes undefined
const assetModel: AssetModel = {
  creator: data.assetCreator,           // undefined if renamed to asset_creator
  headline: data.headline,              // undefined if removed
  initialTransaction: data.initial_transaction,
  thumbnailUrl: data.thumnail_url,      // Note: already has a typo (thumnail)
  // ...
};

If data.assetCreator is renamed to data.asset_creator on the backend, the modal silently shows empty creator info with no console warning or error event.

Proposed Implementation

Add lightweight runtime type guards without adding external dependencies:

// asset-validators.ts
interface RawAssetResponse {
  response: {
    data: {
      assetCreator?: string;
      headline?: string;
      initial_transaction?: string;
      // ... all expected fields
    };
  };
}

function isValidAssetResponse(json: unknown): json is RawAssetResponse {
  if (!json || typeof json !== 'object') return false;
  const obj = json as Record<string, unknown>;
  if (!obj.response || typeof obj.response !== 'object') return false;
  const resp = obj.response as Record<string, unknown>;
  return resp.data !== undefined && typeof resp.data === 'object';
}

// Usage in fetchAsset():
const json = await response.json();
if (!isValidAssetResponse(json)) {
  console.error('Unexpected API response structure:', Object.keys(json));
  return;
}
const { response: { data } } = json;

Expected Impact

  1. Immediate error detection when API responses change shape
  2. Actionable console warnings identifying exactly which fields are missing
  3. Prevent silent UI corruption — show error state instead of blank fields
  4. Support API versioning — gracefully handle v1→v2 migration
  5. Complement existing proposals — works with [Feature] Emit custom error events for host application observability #80 (error events) and [Feature] Add error state and timeout handling for asset data loading failures #69 (error UI states) to create a complete error handling pipeline: validate → emit event → show UI state

Relationship to Other Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions