In the last lesson, every note came back from askForJson() as a record that matched the schema, and checkRecord() still flagged two. Its date check compares preferredDate with today as text, which works only for a real date written as YYYY-MM-DD, and nothing checks that yet. Your schema also does only half the job: the JSON Schema in record-schema.js describes the record for the request, and no code checks the record that comes back against it.
In this lesson, you’ll write the record’s rules once, as a Zod schema. You’ll turn it into the JSON Schema the request sends, and you’ll use the same schema to check every answer before your intake code touches it. Start by installing the library that does both jobs.
Install Zod and Write the Record’s Schema
Your current RECORD_SCHEMA is a plain object that the provider reads. Your own code can’t ask it whether a record fits. Zod is a schema library from npm. You describe the data once in JavaScript, and Zod both checks values against that description and converts it to JSON Schema.
Zod is written with TypeScript in mind, but it checks values at runtime, so it works the same in the plain JavaScript files of this course. Types alone can’t do this job, because TypeScript checks your code before it runs, and a model’s answer only exists at runtime. The post JavaScript vs TypeScript compares the two if you’re curious. In your project folder, install Zod:
$ npm install zod
added 1 package, and audited 2 packages in 822ms
1 package is looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm adds Zod to the dependencies in your package.json, and the installation for this lesson used version 4.6.7. If you’d like a refresher on what npm install does to a project, the From Script Tags to a Vite Project course walks through it.
Now replace the contents of record-schema.js with a Zod schema for the same record:
record-schema.js
import { z } from "zod";
const textOrNull = z.string().nullable();
export const RECORD_SCHEMA = z.object({
kind: z.enum(["booking", "not_a_booking"]),
species: textOrNull,
petName: z.string().max(40).nullable(),
problem: textOrNull,
urgency: z.enum(["routine", "soon", "urgent"]).nullable(),
preferredDate: z.iso.date().nullable(),
preferredTime: textOrNull,
});
Each line reads like the JSON Schema it replaces. z.object() lists the record’s seven keys, z.enum() allows only the listed strings, and .nullable() adds null as an allowed value. A key in z.object() is required unless you mark it optional, so every record still has all seven keys.
Two rules are new. The clinic’s booking system has room for 40 characters in a pet’s name, so .max(40) limits petName. And z.iso.date() accepts only a real calendar date written as YYYY-MM-DD: four digits, then two for the month and two for the day.
That second rule is what the date check in checkRecord() needed. Comparing dates as text works only when both are written that way, and a date with a one-digit month sorts in the wrong place:
> "2026-2-01" < "2026-12-01"
false
February 1 comes before December 1, but as text, the 2 after the first hyphen sorts after the 1 of 12. So a date in the wrong format could pass the date check even when the date has already gone by. With z.iso.date() in the schema, a record with that date never reaches checkRecord().
See Which Rules Reach the Provider
You’ll first use the schema in the request. z.toJSONSchema() converts a Zod schema to JSON Schema, so you can check what Zod writes before you send it. Create print-schema.js, which prints the required keys and the pet name’s part of the converted schema:
print-schema.js
import { z } from "zod";
import { RECORD_SCHEMA } from "./record-schema.js";
const jsonSchema = z.toJSONSchema(RECORD_SCHEMA);
console.log(jsonSchema.required);
console.log(jsonSchema.properties.petName);