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).
Optional reference IDs must be omitted when absent and positive when present
Foreign-key style IDs should not accept 0 or null as a meaningful value unless the API explicitly defines that behavior.
Bad example
| 1 | const parseNonNegativeInteger = (value: unknown) => { |
| 2 | if (value == null) return null; |
| 3 | if (typeof value !== 'number' || !Number.isInteger(value) || value < 0) return undefined; |
| 4 | return value; |
| 5 | }; |
| 6 |
|
| 7 | payload.dividendId = parseNonNegativeInteger(body.dividendId); |
| 8 | payload.transactionId = parseNonNegativeInteger(body.transactionId); |
Explanation (EN)
Allowing 0 for reference IDs weakens the contract and creates an unnecessary extra state. Optional IDs should usually be omitted when absent.
Objašnjenje (HR)
Dozvoljavanje 0 za reference ID-je slabi contract i stvara nepotrebno dodatno stanje. Optional ID-jeve obicno treba izostaviti kada nisu prisutni.
Good example
| 1 | const parseOptionalPositiveInteger = (value: unknown) => { |
| 2 | if (value == null) return { value: undefined }; |
| 3 | if (typeof value !== 'number' || !Number.isInteger(value) || value <= 0) return { error: true }; |
| 4 | return { value }; |
| 5 | }; |
| 6 |
|
| 7 | const parsedDividendId = parseOptionalPositiveInteger(body.dividendId); |
| 8 | if ('error' in parsedDividendId) return res.status(400).json({ message: 'dividendId must be a positive integer greater than 0 when provided.' }); |
| 9 |
|
| 10 | const payload = { |
| 11 | ...(parsedDividendId.value !== undefined ? { dividendId: parsedDividendId.value } : {}), |
| 12 | }; |
Explanation (EN)
Absent values are omitted, and present values must be valid positive IDs. The payload contract is tighter and easier to reason about.
Objašnjenje (HR)
Nepostojece vrijednosti se izostavljaju, a prisutne moraju biti valjani pozitivni ID-jevi. Payload contract je strozi i laksi za razumjeti.
Notes (EN)
Use this especially for fields like `portfolioId`, `transactionId`, `dividendId`, `instrumentId`, and similar relation IDs.
Bilješke (HR)
Koristi ovo posebno za polja kao sto su `portfolioId`, `transactionId`, `dividendId`, `instrumentId` i slicne relation ID-jeve.
Exceptions / Tradeoffs (EN)
Only allow 0 or null if the backend contract explicitly documents them as valid domain values with different meaning from omission.
Iznimke / Tradeoffi (HR)
Dopusti 0 ili null samo ako backend contract izricito dokumentira da su to valjane domenske vrijednosti s drugacijim znacenjem od izostavljanja.