Worked Example: A Schema and Utils Pair for Bike-Share Rentals

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.

Schema: the shape, and the rules that travel with it

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.

Utils: calculations that borrow types, never the reverse

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.

What the pairing shows

  • Dependencies point one way. The calculation knows the shape of a rental because it imports the type. The schema knows nothing about fares.
  • Rules and arithmetic live apart. Validity is Schema's business, computation is Utils' business, and neither is duplicated in a handler.
  • Tests need no scaffolding. A rental literal and a fixed date are the entire fixture.
  • Change stays local. A new plan touches the enum, a new pricing rule touches a calculator, and each change has an obvious home.

Where the book's version goes further

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 takeaway

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.

What the book covers

  • A full product schema with nested objects, enumerations and currency-aware refinements
  • Pricing utilities for tax, discounts, bulk rates, currency conversion and shipping
  • A comprehensive test suite for the pricing functions
  • How inferred types connect the schema layer to the utils layer