pokeapi-typescript icon indicating copy to clipboard operation
pokeapi-typescript copied to clipboard

Incorrect Validation in listAll Method

Open le-pepe opened this issue 6 months ago • 0 comments

Problem Description

The validation in listAll has two issues:

  1. _isListT incorrectly expects a direct array instead of the API's object-with-results structure
  2. _isT checks for id and name properties, but the API's list endpoint only returns name and url

Current Implementation

protected _isT(data: any): data is T {
    return "id" in data && "name" in data;
}

protected _isListT(data: any): data is NamedApiResourceList<T> {
    return Array.isArray(data) && this._isT(data[0]);
}

API Response Structure

List endpoint (e.g., /pokemon):

{
  "count": 1302,
  "next": "...",
  "previous": null,
  "results": [
    {
      "name": "bulbasaur",
      "url": "https://pokeapi.co/api/v2/pokemon/1/"
    }
  ]
}

Required Changes

  1. Update _isListT to check the correct structure:
protected _isListT(data: any): data is NamedApiResourceList<T> {
    return data && 
           typeof data === 'object' &&
           'results' in data &&
           Array.isArray(data.results) &&
           this._isNamedResource(data.results[0]);
}
  1. Create a new validator for list items:
protected _isNamedResource(data: any): data is { name: string, url: string } {
    return data && 
           typeof data.name === 'string' && 
           typeof data.url === 'string';
}
  1. Update _isT (for single item endpoints):
protected _isT(data: any): data is T {
    return data && 
           typeof data.id === 'number' && 
           typeof data.name === 'string';
}

le-pepe avatar May 26 '25 15:05 le-pepe