Pozostałe reguły
Poza regułami NIP, kwot i dat, ksefnik zawiera 7 dodatkowych reguł walidacji.
required-fields
Dział zatytułowany „required-fields”Sprawdza obecność wymaganych pól na fakturze.
import { requiredFieldsRule } from '@ksefnik/core'Wymagane pola: invoiceNumber, sellerNIP, grossAmount, issueDate.
Każde pole jest sprawdzane osobno — reguła zwraca osobny ValidationResult dla każdego brakującego pola:
// Faktura bez numeru i NIP:// results: [// { valid: false, rule: 'required-fields', field: 'invoiceNumber', message: 'Missing required field: invoiceNumber' },// { valid: false, rule: 'required-fields', field: 'sellerNIP', message: 'Missing required field: sellerNIP' },// { valid: true, rule: 'required-fields', field: 'grossAmount' },// { valid: true, rule: 'required-fields', field: 'issueDate' },// ]invoice-number-format
Dział zatytułowany „invoice-number-format”Sprawdza długość numeru faktury.
import { invoiceNumberFormatRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
invoiceNumber | 1-256 znaków |
| Numer | Wynik |
|---|---|
FV/2026/03/001 | valid |
1 | valid |
| (pusty string) | invalid |
| (257+ znaków) | invalid |
duplicate-check
Dział zatytułowany „duplicate-check”Sprawdza, czy w zbiorze faktur nie ma duplikatów (ta sama para invoiceNumber + sellerNIP).
import { duplicateCheckRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
invoiceNumber + sellerNIP | unikalna kombinacja w context.allInvoices |
import { validateInvoices, duplicateCheckRule } from '@ksefnik/core'
const invoices = [ { invoiceNumber: 'FV/001', sellerNIP: '5213456789', /* ... */ }, { invoiceNumber: 'FV/001', sellerNIP: '5213456789', /* ... */ }, // duplikat!]
const reports = validateInvoices(invoices, [duplicateCheckRule])// reports[1].results[0].message: "Duplicate invoice: FV/001 from 5213456789"currency-valid
Dział zatytułowany „currency-valid”Sprawdza, czy waluta faktury to PLN.
import { currencyValidRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
currency | === 'PLN' |
| Waluta | Wynik |
|---|---|
PLN | valid |
EUR | invalid |
USD | invalid |
quantity-positive
Dział zatytułowany „quantity-positive”Sprawdza, czy ilość w pozycjach faktury jest dodatnia.
import { quantityPositiveRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
lineItems[].quantity | > 0 (jeśli podane) |
Reguła sprawdza każdą pozycję osobno i zwraca osobny wynik dla każdej:
// lineItems: [{ quantity: 5, ... }, { quantity: 0, ... }]// results: [// { valid: true, rule: 'quantity-positive', field: 'lineItems[1].quantity' },// { valid: false, rule: 'quantity-positive', field: 'lineItems[2].quantity',// message: 'Quantity must be positive: 0' },// ]description-length
Dział zatytułowany „description-length”Sprawdza, czy opisy pozycji nie przekraczają 256 znaków.
import { descriptionLengthRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
lineItems[].description | <= 256 znaków |
ksef-reference-format
Dział zatytułowany „ksef-reference-format”Sprawdza format referencji KSeF, jeśli jest podana.
import { ksefReferenceFormatRule } from '@ksefnik/core'| Pole | Warunek |
|---|---|
ksefReference | pasuje do /^\d+-\d{8}-[A-Z0-9]+$/ (jeśli podane) |
| Referencja | Wynik |
|---|---|
1234567890-20260301-ABCDEF123456 | valid |
123-20260301-ABC | valid |
ABC-20260301-123 | invalid (cyfry na początku) |
| (brak) | valid (pomijane) |
Użycie wszystkich reguł
Dział zatytułowany „Użycie wszystkich reguł”import { validateInvoices, allRules } from '@ksefnik/core'
const reports = validateInvoices(invoices, allRules)
for (const report of reports) { const errors = report.results.filter(r => !r.valid) if (errors.length > 0) { console.log(`${report.invoice.invoiceNumber}: ${errors.map(e => e.rule).join(', ')}`) }}