Skip to content

Documentation / einvoice-ts / rule

Variable: rule ​

const rule: RuleFactory

Defined in: packages/validator/src/engine/rule.ts:386

Builds a validation rule.

rule(id, definition) looks the id up in the generated spec table and takes severity, message, family, business terms and documentation URL from there; an unknown id throws when the module defining the rule loads, never at validation time. rule.custom(id, definition) takes the metadata inline and refuses ids of the specification and the library's reserved EINV- prefix.

The at form anchors the rule at a DocumentPaths key; the context form takes a selector over the document view.

Example ​

ts
import { rule } from '@einvoice-ts/validator';

const br01 = rule('BR-01', {
  at: 'document',
  test: (document, ctx) =>
    ctx.normSpace(document.customizationId) !== '' || { at: ['customizationId'] },
});

const poNumber = rule.custom('ACME-R-001', {
  at: 'document',
  message: 'A purchase order reference is required.',
  severity: 'fatal',
  test: (document) => document.orderReference?.id !== undefined,
});

Throws ​

rule() when the id is not in the spec table; rule.custom() when it is, or when it starts with EINV-.

MIT licensed. Specification material belongs to OpenPeppol, CEN and OASIS (see NOTICE). Peppol is a trademark of OpenPeppol AISBL; this project is not affiliated with it.