Skip to content

check()

Validate an XML invoice against the official XSD, and optionally the EN 16931 Schematron business rules.

Signature

ts
function check(options: {
  xml: string | Buffer | XMLDocument
  flavor?: string
  level?: string
  schematron?: boolean
}): Promise<{
  valid: boolean
  errors: any[]
  flavor: string
  level: string
  schematronValid?: boolean
  schematronErrors?: SchematronError[]
}>

Options

OptionTypeDefaultDescription
xmlstring | Buffer | XMLDocumentRequired. The invoice XML.
flavorstringautodetectfacturx, orderx or zugferd.
levelstringautodetectSchema level (e.g. en16931).
schematronbooleanfalseAlso run Schematron business-rule validation. Factur-X only — throws otherwise.

Returns

FieldTypeDescription
validbooleanXSD result — or XSD and Schematron when schematron: true.
errorsany[]XSD validation errors (empty when structurally valid).
flavorstringDetected (or supplied) flavor.
levelstringDetected (or supplied) level.
schematronValidboolean?Schematron result on its own (only when schematron: true).
schematronErrorsSchematronError[]?Business-rule failures (only when schematron: true). See SchematronError.

When schematron: true, valid is true only if both the XSD and the Schematron pass.

Examples

ts
import { readFile } from 'node:fs/promises'
import { check } from '@stafyniaksacha/facturx'

const xml = await readFile('Facture_FR_EN16931.xml')

// Structure only (XSD)
const { valid, errors } = await check({ xml })
if (!valid)
  errors.forEach(e => console.error(e.message))

// Full EN 16931 validation (Factur-X)
const result = await check({ xml, schematron: true })
console.log(result.valid) // XSD && Schematron
console.log(result.schematronValid) // Schematron alone
for (const e of result.schematronErrors ?? [])
  console.log(e.id, e.message)

Schematron is Factur-X only

schematron: true for a non-facturx flavor throws. See Validation.

See also

Released under the MIT License.