Latest / The API Podcast with Fexingo: REST, GraphQL, and Modern Web APIs / How APIs Enforce Consistent Data Validation
Transcript
- Lucas: So Luna, you remember that time we were testing that public API and it just silently ignored the extra field we sent? No error, no warning, just... swallowed it. Luna: Oh yeah, that one. Drove me crazy. We thought our request was being accepted, but the response came back with a completely different shape. Lucas: Exactly. That's the kind of thing that happens when an API doesn't validate the data it receives. And honestly, it's more common than you'd think. Today I want to talk about API data validation — why it's critical, how to do it right, and what happens when you skip it. Luna: I'm in. Because I've definitely been on the receiving end of a 400 error that gave me no clue what I did wrong. Lucas: Right, and that's part of the problem. Validation isn't just about rejecting bad data — it's about giving the client useful feedback. But let's start with the basics. What do we mean by validation? It's checking that the data coming in matches what your API expects. That could be the type, the format, the range, even the relationships between fields. Luna: And there are two layers to that, right? Syntactic and semantic. Lucas: Correct. Syntactic validation is about structure. Is this a string? Is this a number? Does it look like an email address? Semantic validation is about meaning — is this email actually allowed to sign up? Is this date in the past? Is this amount within our limits? Both matter, but they're often conflated. Luna: So where do people usually start? I assume with something like JSON Schema. Lucas: That's the go-to. JSON Schema is a declarative language for describing the shape of your data. You define properties, types, required fields, constraints — and then you can validate any JSON payload against it. It's language-agnostic, so your schema can live in one place and be used by different services. Luna: And it pairs well with OpenAPI, right? Because that's how you define your API endpoints. Lucas: Exactly. OpenAPI lets you define your request and response schemas, and many frameworks can automatically validate incoming requests against those schemas. That's a huge win — you write the spec once, and you get validation for free. Luna: But I've heard some people say JSON Schema is overkill or too verbose. What's your take? Lucas: I think it's worth it for any non-trivial API. Yes, there's a learning curve, and some schemas can get long, but the payoff is that your API's contract is explicit and machine-readable. You can also generate documentation, client SDKs, even mock servers from it. The alternative — hand-rolled validation scattered across your controllers — gets messy fast. Luna: That's fair. And what about the tooling? I've heard of Ajv and Zod. Lucas: Ajv is the go-to for JSON Schema validation in JavaScript. It's fast, supports draft-07 and newer, and it's what many frameworks use under the hood. Zod is a bit different — it's a TypeScript-first schema declaration and validation library. You define your schema with Zod's fluent API, and you get type inference for free. It's become super popular with TypeScript developers. Luna: So if I'm building a new API in TypeScript, I might prefer Zod because it's more integrated with my types? Lucas: Exactly. Zod gives you a single source of truth — your schema is both the validation and the type definition. That eliminates a whole class of bugs where your types say one thing and your runtime validation says another. But if you're working across multiple languages or need to share schemas with other teams, JSON Schema is still the portable choice. Luna: That makes sense. Let's talk about what happens when you don't validate. I'm sure there are some horror stories. Lucas: Oh, plenty. One classic example: an API that accepts a numeric field but doesn't check its range. A client sends a negative number for age, and the backend happily stores it. Then downstream systems start behaving weirdly — maybe reports break, or emails go to the wrong people. Luna: And that's a data integrity issue. But there's also a security angle, right? Lucas: Huge security angle. Without validation, you're vulnerable to injection attacks. If you're building a SQL query with user input and you don't validate or sanitize, someone can craft a payload that drops your tables. Or if you're returning error messages that echo the input, you could have a cross-site scripting vector. Luna: So validation is really the first line of defense, not just a nice to have. Lucas: Absolutely. And it's not just about security — it's about reliability. Think about a high-stakes API like a bank's payment endpoint. If you accept a malformed transaction, you could end up with duplicate charges or failed transfers. Banks have to be extremely strict about validation. Luna: They do. Actually, I remember reading about an incident where a bank's API didn't validate the currency code, and a client sent 'US' instead of 'USD'. It caused a massive accounting headache. Lucas: Right, that's the kind of thing that gets people fired. And airlines? I saw a case where an API allowed a negative number of passengers, so a client could book a flight and get a credit instead of a charge. Luna: That's wild. So validation is really about enforcing the contract between you and your clients, and if you do it well, you build trust. Lucas: Exactly. And that's why we talk about this stuff. It's the kind of detail that separates a professional API from a hobby project. And speaking of that, if you're enjoying these conversations and learning something you can actually use, that's exactly the kind of thing we love to hear. Luna: Yeah, and if today's episode gave you something useful, maybe consider a small token of appreciation. It's like buying us a coffee, and it keeps this podcast ad-free. Lucas: Honestly, if today was worth a coffee to you, that's the link — buy me a coffee dot com slash fexingo. No pressure, but listener support is what lets us keep doing this. Luna: And we appreciate every bit. Now, back to validation — I want to get into one of the trickier parts: error messages. How do you balance being helpful without leaking too much? Lucas: Great question. The general principle is: be specific about what's wrong, but vague about your internal implementation. So instead of 'Invalid input at path /data/email', you can say 'The email field must be a valid email address.' That's helpful without revealing your stack. Luna: And you should avoid echoing the input back, right? Because that can lead to stored XSS if you're not careful. Lucas: Exactly. Never echo raw input in error messages. Also, make sure your error responses use a consistent structure — like a code, a message, and maybe a details object. That way clients can programmatically handle errors. Luna: And what about the HTTP status code? I've seen some APIs return 200 with an error flag in the body, which I find infuriating. Lucas: Yeah, that's a common anti-pattern. For validation failures, you should return a 400 Bad Request, or 422 Unprocessable Entity if you want to be more specific about semantic issues. The point is, use the right status code so clients can react correctly without parsing the body. Luna: So we've covered a lot. What's the one takeaway you want listeners to remember? Lucas: I'd say: validation is not an afterthought. It's a core part of API design that affects security, reliability, and developer experience. Start with a schema — JSON Schema or Zod — and enforce it on the server side. Don't rely on client-side validation alone, because that's just a suggestion. Luna: And the next time you see a 400 error, you'll know there's a reason behind it. Lucas: Right. And that reason is often that the API is doing its job. Thanks for listening, folks.