A response can arrive with HTTP 200, parse as JSON successfully and still be unsuitable for what I want to display.
A field may be missing. A number may arrive as text. The unit may have changed. Letting every component handle those details independently spreads assumptions throughout the interface.
I prefer to pause at the boundary: what am I willing to accept, and what happens when a value does not meet that contract?
Reading a format does not validate its content
JSON represents different kinds of values. Objects, arrays, null and strings can all be valid parse results without matching my component’s expected structure.
The MDN JSON.parse reference describes that conversion. My application’s contract requires another check afterward.
A temperature card, for example, needs a station, a value and a known unit. A variable name I chose in the frontend cannot establish those facts.
Write a small, explicit contract
This teaching example accepts an already parsed value. It requires a Celsius reading with a nonempty station name and returns only the card’s fields:
function parseReading(value) {
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
throw new TypeError('Expected a reading object');
}
if (typeof value.station !== 'string' || !value.station.trim()) {
throw new TypeError('Missing station');
}
if (!Number.isFinite(value.temperature)) {
throw new TypeError('Expected a finite temperature');
}
if (value.unit !== 'celsius') {
throw new TypeError('Unsupported unit');
}
return {
station: value.station.trim(),
temperature: value.temperature,
unit: 'celsius'
};
}
Rejecting a temperature such as “23” is intentional. If the provider’s contract permits numeric strings, I need an explicit adapter with reviewed conversion rules. Broad coercion should not accidentally make an empty string acceptable.
Number.isFinite checks finite numbers without first coercing the argument. A finite value still does not establish a physically reasonable reading; that needs domain rules.
Defaults tell a story too
Replacing a missing reading with zero can leave a neat card communicating a false fact. “Unavailable” better preserves what I actually know.
Keeping an older reading can be appropriate, but its age should be clear. I would not call it current merely to avoid a gap in the design.
I previously wrote about keeping the clock independent of weather failures. This contract acts one step earlier, keeping an incomplete response from looking like a successful reading.
Boundaries still matter with TypeScript
A type annotation helps during development. It does not transform an external response or verify the bytes that arrived.
A small function or a schema library can serve this role as the contract grows. I want to preserve a clear entry point: validate, produce a known model and let the rest of the interface work with it.
This does not replace server validation or make data safe as HTML. I would still display the station name as text rather than inserting external content through innerHTML.
Test uncomfortable inputs too
I start with a valid reading, zero and negative values. Then I try null, arrays, missing fields, numeric strings, NaN, Infinity and a different unit.
I also check that the function leaves its input unchanged. Normalizing a station name should not unexpectedly modify a response another part of the program retains.
AI can help enumerate variations and draft tests. Accepting, converting or rejecting data still depends on the contract I need to maintain.
I like giving that decision a visible place in the code. An explicit boundary is easier to review than exceptions each screen invented independently.
AN IDEA TO SHARE



