Skip to content

[Bug]: “field() callback cannot validate array item structure without length error” #245

@gn753

Description

@gn753

Expected Behavior

The structure of each item in the array should be verifiable using the field("...", (val) => ...) callback, even if the array length varies in the actual service response.

The test should pass regardless of the array length.

Actual Behavior

  • The server actually returns 3 items in the data array, but itdoc assumes the "expected array length=1", causing an error like Expected response body[data] to have length 1 but got 3.
  • It's not possible to validate the structure of each item in the array using a callback.

Steps to Reproduce

Server Response Example:

{
  "success": true,
  "data": [
    { "id": "plan-1", "tierCode": "BASIC", },
    { "id": "plan-2", "tierCode": "BASIC", },
    { "id": "plan-3", "tierCode": "PREMIUM", }
  ],
  "count": 3,
  "meta": {
    "retrievedAt": "2025-09-25T04:28:33.350Z",
    "source": "plan_list_query"
  }
}

Test Code ① – Static Array (Passes):

.body({
  success: field("Success status", true),
  data: field("Plan list", [
    { id: field("Plan ID", "plan-1"), tierCode: field("Tier code", "BASIC"),},
    { id: field("Plan ID", "plan-2"), tierCode: field("Tier code", "BASIC"),},
    { id: field("Plan ID", "plan-3"), tierCode: field("Tier code", "PREMIUM"),},
  ]),
  count: field("Plan count", 3),
  meta: field("Meta", val => val),
});

✅ The test passes when all array items are defined statically.

Test Code ② – Single Sample + Callback (Fails):

.body({
  success: field("Success status", true),
  data: field("Plan list", [
    {
      id: field("Plan ID", (val: any) => {
        if (typeof val !== 'string') throw new Error('id must be a string');
        return val;
      }),
      tierCode: field("Tier code", (val: any) => {
        if (!['BASIC', 'PREMIUM'].includes(val)) throw new Error('Disallowed code');
        return val;
      }),
      // ... other fields follow the same pattern
    }
  ]),
  count: field("Plan count", val => val),
  meta: field("Meta", val => val),
});

itdoc Version

0.4.1

Additional Information

No response

Metadata

Metadata

Assignees

Labels

bugbugbugbug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions