Warm-up: checking input
Checking input: you receive unknown. Write a check that it's an object, has a non-empty string identifier, and isn't null or an array. Compare with parseLookupArgs only after your own attempt. Explain why as {id:string} isn't such a check.
The function parseLookupArgs(value) returns { id } or throws an error.
You will practise
- Check data at runtime
- Tell a type cast from validation
The 3 tests
Tests written from the warm-up task. The reference implementation also rejects extra fields and ids longer than 100 characters — the tests don't require that.
returns { id } for a valid object
assert.deepEqual(parseLookupArgs({ id: 'A' }), { id: 'A' });rejects null, arrays and primitive values
for (const value of [null, [], ['A'], 'A', 7, undefined]) {
assert.throws(() => parseLookupArgs(value));
}rejects an empty, non-string or missing identifier
for (const value of [{ id: '' }, { id: 7 }, {}]) {
assert.throws(() => parseLookupArgs(value));
}Related questions
Source: AI & Agents — practice tasks — An exercise written for the study programme, not a confirmed interview task.