AI MOOC
✦ Grow togetherInvite 3 friends, earn 1 month of membership · Up to 12 monthsInvite friends →

Library / My reading notes

Valid JSON is not enough

Reject missing fields, wrong types and oversized results.

Back to course plan: Design a bounded AI text assistant

Chapter 2 / 3 · Read and take notes

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.

  1. 1Parse
  2. 2Validate fields
  3. 3Check evidence
  4. 4Approve delivery

Included material

[{"summary":123,"caveats":[]},
 {"summary":"Template B proceeds to trial.","caveats":[]},
 {"summary":"Efficiency improved 80%.","caveats":[]}]

Work through the steps

  1. Implement the validator and check all three fixtures.
  2. Check evidence for each result that passes validation.
  3. 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.

References and further reading

The lessons are original. The following sources are provided under their respective permissions.

Course

Generative AI for Beginners

Microsoft · en/zh

MIT

License checked. This entry links to the original; the book text is not mirrored.

License checked: 2026-10-16 · Pinned d45da912
Course

Generative AI with JavaScript

Microsoft · en/zh

MIT

License checked. This entry links to the original; the book text is not mirrored.

License checked: 2026-10-16 · Pinned 03e41953
Book

Self-LLM: open LLM deployment guide

Datawhale · en/zh

Apache-2.0

License checked. This entry links to the original; the book text is not mirrored.

License checked: 2026-10-16 · Pinned e0fe14a3