How the Bottom Layers Interact
Trace three kinds of change through a tool that uses Schema, Utils and Resources, and see why the two bottom layers earn the heaviest testing.
Talk about layers is easy to nod along to. It becomes convincing when two of them carry one small feature from end to end. The book does this with an online shop's product catalogue and pricing. Here is the same shape in a bike-share service, so you can watch what moves where without the retail details getting in the way.
A rental has an identifier, a plan, a start time and, once the bike is docked, an end time. The one rule that spans fields is that a rental cannot end before it began. That rule belongs in the Schema layer, next to the data it protects, so every consumer gets it for free.
export const RentalSchema = z.object({
rentalId: z.string().uuid(),
plan: z.enum(["pay-as-you-go", "day-pass", "member"]),
startedAt: z.string().datetime(),
endedAt: z.string().datetime().optional(),
}).refine(
(r) => !r.endedAt || Date.parse(r.endedAt) > Date.parse(r.startedAt),
{ message: "A rental cannot end before it starts" },
);
export type Rental = z.infer<typeof RentalSchema>;
It is worth noticing what the schema does not do. It does not know how much a rental costs, and it does not decide whether a rider may start another one. It states what a well-formed rental is and stops there.
The book's shop example leans on the same idea for numbers that must add up and amounts that must respect a currency's precision. The point is the placement. An invariant written once in Schema is an invariant no tool, endpoint or screen can forget.
Now the fare. Utils depends on Schema, so it can import the Rental type, and it must stay pure. An unfinished rental needs a current time, so that arrives as an argument:
import type { Rental } from "@schema/rentals/rental.schemas";
export const billableMinutes = (r: Rental, now: Date): number => {
const end = r.endedAt ? Date.parse(r.endedAt) : now.getTime();
return Math.ceil((end - Date.parse(r.startedAt)) / 60_000);
};
Notice what is missing. The function does not call new Date(), it does not look up a price list, and it does not log. A per-minute rate and a daily cap would be two more arguments to a second function, not a lookup into configuration or a call to a rates service. In the book, the currency converter works the same way: exchange rates are handed in, never fetched.
Testing this takes almost no effort. Build a rental literal that started at ten o'clock, pass a now of half past, and expect thirty billable minutes. There is no database to seed, no clock to freeze and no mock to configure, because everything the function needs is in its arguments.
The shop edition is bigger than this sketch. Its schema nests smaller schemas inside larger ones and reuses enumerations, and its utilities cover discounts, tax, currency conversion and delivery costs, with a full test suite alongside. The structure is identical to what you have just seen: shape and invariants below, pure calculation above, types flowing upward.
The two bottom layers are small on purpose. Between them they answer two questions, what valid data looks like and what you can calculate from it, and they do so without a database, a network call or a clock. For the concepts behind the example, read about the Schema layer and the Utils layer. The next piece, how the bottom layers interact, shows a tool using both.