← Kembali ke Daftar Berita

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

Tanggal: 2026-12-02

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

Posted on Dec 2 • Originally published at neuralnair.com

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.

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:

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.

DEV Community — A space to discuss and keep up software development and manage your software career

Built on Forem — the open source software that powers DEV and other inclusive communities.

Made with love and Ruby on Rails. DEV Community © 2016 - 2026.

We're a place where coders share, stay up-to-date and grow their careers.

→ Baca artikel asli