Skip to content

Documentation / @facturometro/einvoice / validateXml

Function: validateXml() ​

validateXml(xml, options?): XmlValidationResult

Defined in: packages/einvoice/src/validate-xml.ts:101

Parses a UBL invoice or credit note and validates it, reporting the parser's structural diagnostics and the validator's issues together.

The document is parsed; its composition is resolved as validate() resolves it (resolveDocumentRuleset()), and the document is validated against exactly that composition, with the diagnostics, so rules that count what the model cannot hold see the parser's reports. The diagnostics then join the result ahead of the validation issues - the structure is checked first, as in the official pipeline - with two adjustments:

  • The composition's configuration of structural ids (UBL-CR-*, UBL-SR-*, UBL-DT-*) applies (CompiledRuleset.applyStructural): a disabled id is dropped, an overridden severity or message is applied, and a bespoke UBL-SR/UBL-DT rule's diagnostics take the compiled rule's. The library's own EINV- diagnostics cannot be disabled or overridden.
  • An EINV-XML-EMPTY warning is dropped where EINV-XSD-LEXICAL or PEPPOL-EN16931-R008 reports the same element, so each empty element is reported once.

issues lists the remaining diagnostics in parser order, then validate()'s issues in its order. ok, fatal, warnings and the counts follow the merged issues, so a fatal diagnostic makes the result not ok even when no rule failed, and one overridden to a warning does not. validate() never copies the diagnostics into its own issues, so each appears once. The diagnostics field keeps them all, unfiltered.

Parameters ​

xml ​

string

The document, as text.

options? ​

ValidateXmlOptions = {}

Which rulesets to run; by default, the composition the document's cbc:CustomizationID resolves to, as for validate().

Returns ​

XmlValidationResult

The merged result, with the raw parser diagnostics.

Throws ​

When the document is not well-formed XML.

Throws ​

When the document element is neither a UBL Invoice nor a UBL CreditNote.

Throws ​

When the ruleset composition does not compile.

Example ​

ts
import { defineRuleset, en16931Ruleset, peppolRuleset, validateXml } from '@facturometro/einvoice';

const result = validateXml(xml);
if (result.ok) {
  result.document.id;
} else {
  result.fatal.map((issue) => `${issue.id} at ${issue.xpath}: ${issue.message}`);
}

const noExtensionsWarning = defineRuleset({ id: 'acme', version: '1.0.0', disable: ['UBL-CR-001'] });
validateXml(xml, { ruleset: [en16931Ruleset, peppolRuleset, noExtensionsWarning] });

MIT licensed. Specification artefacts belong to OpenPEPPOL, CEN and OASIS.