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).
Do not paper over a missing required field with an empty-string default
If an output field is mandatory, validate it at the input boundary or skip the record rather than emitting an empty value.
Bad example
| 1 | function toEntry(article: ArticlePayload): SitemapEntry { |
| 2 | return { |
| 3 | loc: article.url ?? '', // empty <loc> is invalid output |
| 4 | // ... |
| 5 | }; |
| 6 | } |
Explanation (EN)
url is optional on the payload, so a missing url silently becomes an empty loc, producing structurally invalid output that can poison the whole document.
Objašnjenje (HR)
url je opcionalan u payloadu, pa nedostajući url tiho postaje prazan loc, čime se proizvodi strukturno neispravan izlaz koji može pokvariti cijeli dokument.
Good example
| 1 | function toEntry(article: ArticlePayload): SitemapEntry | null { |
| 2 | if (!article.url) { |
| 3 | return null; // skip records that can't produce a valid entry |
| 4 | } |
| 5 | return { loc: article.url /* ... */ }; |
| 6 | } |
| 7 |
|
| 8 | // or make it required at the boundary: |
| 9 | // class ArticlePayload { @IsUrl() url!: string; } |
Explanation (EN)
Requiring the field in the DTO (or skipping records that lack it) guarantees every emitted entry is valid.
Objašnjenje (HR)
Zahtijevanje polja u DTO-u (ili preskakanje zapisa koji ga nemaju) jamči da je svaki proizvedeni zapis ispravan.