Files
accounted/lib/invariants/fiscal-year.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

38 lines
1.7 KiB
TypeScript

/**
* Four-digit calendar year, as used for räkenskapsår keys.
*
* ## Why this exists as its own name
*
* The regex is byte-identical to {@link ACCOUNT_NUMBER_RE} in
* `account-number.ts`. That collision is the reason both are named: a reader
* (or a codemod) scanning for `/^\d{4}$/` cannot tell a BAS account from a
* year, and the two have opposite consequences when confused. `lib/api/schemas.ts`
* already carried both meanings under the same literal.
*
* The bound is deliberately loose. Accounted holds historical räkenskapsår from
* SIE imports going back decades and must accept future years for planning, so
* this validates the shape and a sane range, not a business rule. Whether a
* given year has an open fiscal period is a database question.
*/
/** Exactly four digits. Identical to the account-number shape by coincidence, not by meaning. */
export const FISCAL_YEAR_RE = /^\d{4}$/
/** Canonical validation error message (Swedish: user-facing surface). */
export const FISCAL_YEAR_MESSAGE = 'Nyckel måste vara ett fyrsiffrigt år'
/** Lower bound: earlier years are certainly a typo, not an imported räkenskapsår. */
export const FISCAL_YEAR_MIN = 1900
/** Upper bound: generous enough for forward planning, tight enough to catch a mistyped account number. */
export const FISCAL_YEAR_MAX = 2200
/** True when the input is four digits inside the accepted range. */
export function isFiscalYear(raw: string | number | null | undefined): boolean {
if (raw === null || raw === undefined) return false
const s = typeof raw === 'number' ? String(raw) : raw
if (!FISCAL_YEAR_RE.test(s)) return false
const n = parseInt(s, 10)
return n >= FISCAL_YEAR_MIN && n <= FISCAL_YEAR_MAX
}