Urgent.News

What's breaking now, across thousands of outlets.

Tech

Your hand-written JSON Schema is lying to your API. Mine was.

I once maintained a JSON Schema by hand for a payment webhook payload. For months it was fine. Then the provider added a field, my schema said additionalProperties: false , and a valid payload started failing validation at 2am. The schema wasn't wrong when I wrote it. It was wrong the moment reality changed and I didn't. Hand-written schemas drift That's the core problem with writing JSON Schema…

A JSON Schema written manually for a payment webhook payload once functioned well, but issues arose when the provider added a new field. The schema had been set to disallow additional properties with `additionalProperties: false`, resulting in validation failures at an odd hour. The schema itself was not inherently wrong at the time of creation, but it became incorrect once reality changed and the developer did not update it accordingly.

Hand-written schemas are prone to drifting out of sync with the actual data, as they are static snapshots reflecting the data structure at the moment they were written. Because the schema and the payloads reside in separate repositories, manual updates are often neglected. To address this issue, it is recommended to adopt a sample-first approach to schema creation.

By starting with real sample responses and automatically generating a draft 2020-12 schema with marked required fields and a plain-English explanation of validation rules, the schema can be more accurately reflected the actual data. Regularly tightening the schema based on practical knowledge of optional fields, string enums, and data types (such as distinguishing integers from floating-point numbers) can help maintain its relevance.

Reading the schema aloud can also aid in its ongoing maintenance. Additionally, integrating schema validation into CI pipelines and utilizing structured output contracts with language models can enhance the overall schema management process. However, it's important to note that automated schema generation cannot infer business rules or deduce nuanced constraints from limited samples.

Manual reviews are still necessary to ensure accuracy. Furthermore, the generated schemas adhere to the draft 2020-12 specification, so compatibility adjustments may be required if working with draft-04 tools. To begin, it's advisable to use the most complex and field-rich JSON response available and feed it into a schema generator.

If the resulting schema accurately reflects the data structure and identifies any discrepancies in test coverage, the workflow should be implemented for better schema management.

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

Cyberattack Hits Spanish Train Operator User Data

Spain’s largest train operator reported a cyberattack that compromised some usernames and email addresses, but the company said there’s “no evidence” that financial information was leaked. Spanish outlet El Mundo said the attack shared traces of AI, with the incident taking place amid warnings of intensifying Russian hybrid threats…

JavaScript vs Java: They're Not the Same! ☕⚡

JavaScript vs Java: They're Not the Same! ☕⚡ As a beginner, I thought Java and JavaScript were the same language because of their names. They aren't—they're completely different. What is Java? ☕ Java is a programming language mainly used for backend development, Android apps, and enterprise software.

More from Saturday 26 September →