Rules Hub
Code Ruleset
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)
A malformed ID can fail deep inside a shared transaction after unrelated work has begun.
Objašnjenje (HR)
Neispravan ID može srušiti zajedničku transakciju nakon što je već započeo nepovezan posao.
Good example
| 1 | const rawId = raw.id; |
| 2 | const id = typeof rawId === 'string' && /^\d+$/.test(rawId) ? Number(rawId) : NaN; |
| 3 | if (!Number.isSafeInteger(id) || id <= 0) { |
| 4 | reportInvalidRecord(raw); |
| 5 | continue; |
| 6 | } |
| 7 | await tx.insert(externalIdentities).values({ personId, externalId: id }); |
Explanation (EN)
This example requires a positive decimal string ID within the safe integer range; other schemas need their own validator.
Objašnjenje (HR)
Ovaj primjer zahtijeva pozitivan decimalni ID u obliku stringa, unutar sigurnog raspona cijelih brojeva. Druge sheme trebaju vlastiti validator.
Notes (EN)
Validate external identifiers before conversion and enforce the database/domain range; route invalid records through the batch's explicit exclusion policy before starting its writes.
Exceptions / Tradeoffs (EN)
Number(null) and Number('') become zero, and Number.isInteger permits unsafe integers. Skipping a row is appropriate only for a partial-success contract; atomic imports must fail as a whole.