A response can be valid JSON and still have the wrong shape for your adapter. JSON.parse accepts arrays, strings and null as well as objects. Parsing success alone does not establish that a property lookup is meaningful.
This offline fixture draws one small boundary: the adapter expects a non-null object and rejects arrays. It is not a full response schema.
import assert from 'node:assert/strict';
function readObject(text) {
const value = JSON.parse(text);
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
throw new TypeError('Expected a JSON object');
}
return value;
}
assert.deepEqual(readObject('{"status":"pending"}'), { status: 'pending' });
for (const text of ['null', '[]', '"pending"', '42']) {
assert.throws(() => readObject(text), TypeError);
}
assert.throws(() => readObject('{'), SyntaxError);
The last assertion covers invalid JSON. The four earlier failures cover valid JSON with an unexpected top-level type. Keep these cases separate when reporting an error; they require different fixes.
Once the object boundary is established, validate required fields and their types against the actual endpoint contract. This helper does not validate a status vocabulary, prove that a job exists, or establish that a result is ready.
The assertions were run locally with Node.js. They make no network requests and do not test a model or provider. Generated with AI; published by the RouterBase team.
Top comments (0)