Urgent.News

What's breaking now, across thousands of outlets.

Tech

Generating a Zod schema from an API response sample, and what a sample can’t tell you

Disclosure: I built the converter used in this post. The Zod code below is useful without it. TypeScript types disappear at runtime. const user: User = await res.json() checks nothing. Zod validates data where it enters your app, so a first-draft schema from a sample response saves typing. Here is a sample response: { "id" : 4182 , "email" : "ada@example.com" , "createdAt" :…

The article discusses generating Zod schemas from API response samples and the limitations of relying solely on samples. The author built a converter for this purpose, stating that TypeScript types disappear at runtime, making validation essential. A sample response is provided, which is then used to generate a Zod schema.

However, the article highlights what a single sample cannot tell you. There are two order schemas in the sample, each with different keys, leading to a union type when creating the final schema. To merge these, you must handle them manually. The sample also shows that the `avatarUrl` field can be either a string or null, but the nullable type is only inferred from the sample.

Additionally, the sample confirms that `email` and `createdAt` are plain strings, but a real API might return different formats. Furthermore, empty arrays are represented as `z.array(z.null())` in the generated schema, which would reject actual items. To avoid this, you should add stricter checks based on API documentation.

The article concludes with an example of how to use the generated schema when data enters your application, such as in the `loadUser` function, which safely parses the response using the `UserSchema`. The parsed result is then checked to ensure it's valid and returned as the expected `User` type.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

European Law for .NET Developers: What the GDPR Means for Your Code

Hey lovely readers, The GDPR has been around since 2018, and most of us have heard of it. But when I ask developers what it actually means for the code they write, they often don't know.

  • GDPR protects EU personal data in code, including names, email addresses, and IP addresses.
  • Special categories of data, like health and genetic data, have stricter processing rules.
  • Violations of GDPR can result in fines up to 20 million euros or 4% of global revenue.

Pocket rockhound buddy that works anywhere

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 1: Touch Grass What I Built I love to explore nature, and while I'm familiar with the minerals, fossils, and stones around my…

  • Offline tool identifies rocks, fossils, and minerals
  • Guides users through physical tests like scratching and weighing
  • Generates specimen label with hardness and origin

Server-Sent Events (SSE) Backpressure Breaks: Memory Leaks and Socket Starvation in High-Throughput LLM Pipelines

Streaming LLM completions over Server-Sent Events (SSE) looks trivial in sandbox demos, but unhandled backpressure under real-world network conditions can destroy backend application servers.

  • Server-Sent Events (SSE) struggle with backpressure handling
  • Memory leaks and crashes occur due to unmanaged token generation
  • Production-ready backpressure-aware SSE pipeline proposed

Proved, Certified, Swept, Sampled

A claim in a paper can be backed by very different things. It can have a written proof. It can have a theorem the Lean kernel has checked.

  • Claims can be backed up by written proofs, verified theorems, certificates, or sampling
  • Sampling ten million configurations failing to break a claim is less reliable than other methods

More from Saturday 10 October →