Two things I got wrong while building my own API
A Zod default that quietly emptied a project's feature list, and a rate limiter that couldn't tell visitors apart. Both were mine, and both are instructive.
On this page
I'm writing this one partly as a reminder to myself. While building the API behind this site I made two mistakes that looked fine until they didn't. Neither was dramatic, but both taught me something I hadn't learned from reading.
The default that erased a list
The admin lets me edit projects, and each project has lists: features, achievements, technologies. To validate updates I used Zod, and for update requests I did the obvious thing: take the "create" schema and call .partial() on it, so every field becomes optional.
It worked in testing. Then I flipped a "featured" toggle on a project, and its feature lists came back empty.
What happened is that in Zod 4, .partial() still applies .default() values. The create schema said "lists default to an empty array", so an update that didn't mention the lists got parsed as "lists: []", and the database was happily updated with that. I'd re-saved the whole project with blank lists, just by changing one checkbox. It was one of the Zoniq project's entries, and I had to put the content back by hand.
The fix was a small helper that strips the defaults before making the schema optional:
export function partialNoDefaults<T extends z.ZodRawShape>(shape: T) {
const bare = Object.fromEntries(
Object.entries(shape).map(([key, field]) =>
[key, field instanceof z.ZodDefault ? field.unwrap() : field]
)
);
return z.object(bare).partial();
}
Every update schema is now built from the raw shape with it, and there's a test that sends a partial update and checks nothing else changed. The lesson isn't really about Zod. It's that "optional" and "has a default" are different ideas, and a partial update is a delicate thing: absent has to mean "leave alone", never "reset".
A rate limiter that saw one visitor
The contact form and login are rate limited, so a bot can't hammer them. Express-rate-limit counts requests per IP address. I deployed it on Render and then, out of habit, went to check that it was really counting the right IP. It wasn't.
Behind a hosting platform, your app doesn't see the visitor's address directly. Requests pass through proxies, and the app only sees the last one. Unless you tell Express how many proxies to trust (trust proxy), req.ip is the proxy's address, which means every visitor shares a single bucket. One person could use up everyone's allowance. Or, if you trust too many, a visitor can send a fake X-Forwarded-For header and pretend to be anyone, which defeats the limit completely.
So I measured. I sent requests with a made-up forwarded address and looked at what the app saw. On Render's free plan the path is: the visitor, then Render's own edge, then an internal balancer, then a local proxy. Three hops. Setting the trust count to 3 makes req.ip the real visitor, and a fake header the visitor adds ends up earlier in the chain, where it's ignored. I confirmed that with the spoofed-header test against the live service, not just by reading the docs.
There's a catch I made a note of. If I ever proxy my API's domain through Cloudflare, that adds a hop, and the number becomes 4. The comment in the config says so, and I re-run the spoof test after any hosting change.
What they have in common
Both bugs passed every test I'd written, because I'd tested the happy path with sensible input. The first needed an update that left something out. The second needed the real network in front of the app. Neither would have shown up on my laptop.
So now I try to ask two questions before I trust something: "what does this do when a field is missing?" and "what does it do when it's not running on my machine?" The answers have been uncomfortable more often than I'd like.
Enjoyed this? Pass it on
Written by Vishwajit Bhosle
I build and lead web and mobile products from Pune, and write about what I run into along the way. If something here helped, or you have a project in mind, say hello.
Follow along
New posts show up in any feed reader the moment they are published: no account, no email, no tracking.
Comments
Anyone can comment, no sign-up. Upvote what helped, reply to anyone. I read all of them and reply where I can.
Loading comments…