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).
Reject invalid records individually instead of aborting the whole batch
Validate coerced external values before they can fail deep inside a shared transaction.
Bad example
| 1 | const id = Number(raw.id); |
| 2 | // used later inside a transaction that processes hundreds of records |
| 3 | await tx.insert(externalIdentities).values({ personId, drpAuthorId: id }); // id may be NaN |
Explanation (EN)
If raw.id is anything unexpected, Number() silently produces NaN instead of throwing, and the failure only surfaces when the database rejects the NaN much later — aborting the entire transaction and losing every other record processed alongside it.
Objašnjenje (HR)
Ako je raw.id bilo što neočekivano, Number() tiho proizvede NaN umjesto da baci grešku, a problem se pojavi tek kad baza odbije NaN mnogo kasnije — pri čemu se prekida cijela transakcija i gube se svi ostali zapisi obrađeni uz njega.
Good example
| 1 | const id = Number(raw.id); |
| 2 | if (!Number.isInteger(id)) { |
| 3 | report.push({ text: `record ${raw.id} -> excluded: invalid id` }); |
| 4 | continue; |
| 5 | } |
| 6 | await tx.insert(externalIdentities).values({ personId, drpAuthorId: id }); |
Explanation (EN)
Validate the coerced value right after conversion and route a bad record into the same exclusion/report mechanism already used for other partial-data cases, so one bad record can't take the whole batch down with it.
Objašnjenje (HR)
Provjeri pretvorenu vrijednost odmah nakon konverzije i preusmjeri loš zapis u isti mehanizam isključivanja/izvještaja koji se već koristi za druge slučajeve nepotpunih podataka, tako da jedan loš zapis ne može srušiti cijeli batch.