A summarization tool needs a small list of document IDs and a maximum output length. It does not need the model to choose a tenant, an administrative role, or a different operation.
This exercise validates that narrow contract using Node.js 22 or newer. It reads no documents and calls no model. Everything runs in memory with synthetic input, so it is suitable for an authorized local test environment.
1. Define the accepted shape
Save the code as tool-arguments.mjs. The input is raw JSON text limited to 2,048 UTF-8 bytes. The parsed object must contain exactly three fields, one supported operation, one to five distinct document IDs, and an integer output limit from 100 to 1,000 characters.
import assert from 'node:assert/strict';
function validateArguments(raw) {
assert(typeof raw === 'string' && Buffer.byteLength(raw, 'utf8') <= 2048, 'input too large or not text');
const input = JSON.parse(raw);
assert(input && typeof input === 'object' && !Array.isArray(input), 'object required');
assert.deepEqual(Object.keys(input).sort(), ['documentIds', 'maxCharacters', 'operation']);
assert.equal(input.operation, 'summarize', 'operation not allowed');
assert(Array.isArray(input.documentIds) && input.documentIds.length >= 1 && input.documentIds.length <= 5, 'invalid document count');
assert(input.documentIds.every((id) => typeof id === 'string' && /^[a-z0-9-]{1,40}$/.test(id)), 'invalid document ID');
assert.equal(new Set(input.documentIds).size, input.documentIds.length, 'duplicate IDs');
assert(Number.isSafeInteger(input.maxCharacters) && input.maxCharacters >= 100 && input.maxCharacters <= 1000, 'invalid output limit');
return { operation: input.operation, documentIds: [...input.documentIds], maxCharacters: input.maxCharacters };
}
const valid = { operation: 'summarize', documentIds: ['doc-1', 'doc-2'], maxCharacters: 500 };
assert.deepEqual(validateArguments(JSON.stringify(valid)), valid);
for (const patch of [
{ operation: 'delete' }, { tenantId: 'another-tenant' },
{ documentIds: [] }, { documentIds: ['doc-1', 'doc-1'] },
{ documentIds: ['../private'] }, { documentIds: 'doc-1' },
{ maxCharacters: '500' }, { maxCharacters: 99 },
{ maxCharacters: 1001 }, { maxCharacters: 100.5 },
]) assert.throws(() => validateArguments(JSON.stringify({ ...valid, ...patch })));
for (const raw of ['null', '[]', '{broken', '{"__proto__":{},"operation":"summarize","documentIds":["doc-1"],"maxCharacters":500}']) {
assert.throws(() => validateArguments(raw));
}
assert.throws(() => validateArguments(' '.repeat(2049)), /input too large/);
assert.throws(() => validateArguments(null), /not text/);
for (const maxCharacters of [100, 1000]) {
assert.equal(validateArguments(JSON.stringify({ ...valid, maxCharacters })).maxCharacters, maxCharacters);
}
console.log('PASS: bounded valid arguments accepted; malformed and unauthorized argument shapes rejected.');
2. Run valid and invalid cases
node tool-arguments.mjs
Expect one PASS: line. The boundary values of 100 and 1,000 are accepted, while values outside the range and numeric strings are rejected. An extra tenant field fails even if all other arguments are valid.
The word “unauthorized” in the result refers to disallowed argument shapes. The code does not authorize access to the document IDs themselves. That decision belongs to the authenticated retrieval layer.
3. Confirm the tests protect the operation allowlist
In a disposable copy, remove the assert.equal(input.operation, 'summarize', ...) line. Re-run the script. The invalid-operation case must fail with Missing expected exception. Restore the line afterward.
This catches a regression that would otherwise accept a well-formed request for an unsupported action. Similar tests should accompany any new operation before it is enabled.
4. Check the next boundary too
JSON.parse retains the last value of a duplicate property. This exercise validates that parsed representation and passes only the resulting object onward. If your interface requires rejecting duplicate JSON keys, use a parser contract that explicitly supports that rule; do not validate with one interpretation and execute the raw text with another.
The output limit is a requested bound, not enforcement of the eventual model response length. The tool implementation must constrain or reject oversized output as appropriate. The argument validation guide describes how parsing, semantic validation, and authorization fit together.
