Reject missing fields, wrong types and oversized results.
Understand the method
JSON.parse proves syntax, not suitability. {"summary":123} is valid JSON but violates the string contract. Validate object type, allowed fields, required fields and bounds. Reject empty summaries or object values inside the caveats array rather than guessing intent.
Even structurally valid output may contain a fabricated “80% improvement.” A schema controls shape, not factual support. Require source evidence and use human review or a dedicated evaluation process. Here we check against the supplied text manually.
- 1Parse
- 2Validate fields
- 3Check evidence
- 4Approve delivery
Included material
[{"summary":123,"caveats":[]},
{"summary":"Template B proceeds to trial.","caveats":[]},
{"summary":"Efficiency improved 80%.","caveats":[]}]Work through the steps
- Implement the validator and check all three fixtures.
- Check evidence for each result that passes validation.
- Keep format errors distinct from claims awaiting factual review.
Worked example
function validSummary(x) {
return x !== null && typeof x === "object" && !Array.isArray(x) &&
Object.keys(x).every(k => ["summary", "caveats"].includes(k)) &&
typeof x.summary === "string" && x.summary.trim().length > 0 &&
x.summary.length <= 300 && Array.isArray(x.caveats) &&
x.caveats.length <= 5 && x.caveats.every(s =>
typeof s === "string" && s.trim().length > 0 && s.length <= 200);
}Your exercise
Add fixtures with a string caveats value and a whitespace-only summary. Explain why the third supplied fixture is structurally valid but not ready to deliver.
Check before delivery
- Invalid structure is rejected without a success message.
- You can explain why validation does not establish factual truth.
Finish the exercise, then view the reference
Fixture one fails structure. Two passes and matches the source. Three passes structure but the 80% claim is unsupported. Both added error fixtures should return false.
An original course with teaching exercises. Reading records, practice outcomes and independent certification are distinct.