Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Give deserialized cache/JSON data an explicit shape
Type stored/deserialized data with a defined interface instead of a loose Record with ambiguous either-or fields.
Bad example
| 1 | const data = JSON.parse(result) as Record<string, string | number | undefined>; |
| 2 | // which one is actually stored? unclear and fragile |
| 3 | const id = Number(data.millistreamId ?? data.insref); |
Explanation (EN)
A loose `Record` plus an either-or fallback signals that the write side and read side don't agree on a shape, so reads are guesswork and easy to break.
Objašnjenje (HR)
Labavi `Record` uz ili-ili fallback signalizira da se strana koja pise i strana koja cita ne slazu oko oblika, pa je citanje nagaanje i lako se pokvari.
Good example
| 1 | interface StoredInstrument { |
| 2 | millistreamId: number; |
| 3 | name: string; |
| 4 | } |
| 5 | const data = JSON.parse(result) as StoredInstrument; |
| 6 | const id = data.millistreamId; // single, known field |
Explanation (EN)
An explicit interface documents exactly what is written and read, removing the ambiguity and the defensive fallback. Keep the serializer and deserializer in agreement on this shape.
Objašnjenje (HR)
Eksplicitno sucelje dokumentira tocno sto se pise i cita, uklanjajuci dvosmislenost i obrambeni fallback. Drzi serializer i deserializer usuglasenima oko ovog oblika.
Exceptions / Tradeoffs (EN)
Validate at the boundary with a schema (e.g. zod) when the data truly comes from an untrusted or evolving external source rather than your own writer.
Iznimke / Tradeoffi (HR)
Validiraj na granici pomocu sheme (npr. zod) kada podaci stvarno dolaze iz nepouzdanog ili promjenjivog vanjskog izvora, a ne iz tvog vlastitog pisaca.