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 overriddenseverityormessageis applied, and a bespokeUBL-SR/UBL-DTrule's diagnostics take the compiled rule's. The library's ownEINV-diagnostics cannot be disabled or overridden. - An
EINV-XML-EMPTYwarning is dropped whereEINV-XSD-LEXICALorPEPPOL-EN16931-R008reports 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 ​
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 ​
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] });