Expand description
Validate EN 16931 and Peppol BIS Billing 3.0 e-invoices against the official validation artefacts, in pure Rust: no JVM, no C library, no FFI.
A Validator is built once from a rule set: the XML schema it names is loaded and
its official compiled XSLT stylesheets are prepared. It then validates any number of
documents, from any number of threads (Validator is Send + Sync). Each
validation runs, in order:
- the XML check: UTF-8, well-formed, namespace-well-formed, no document type
declaration (
Error::NotXml,Error::Unsupported), no element nested deeper thanValidateOptions::max_depth(Error::Limit); - the XML schema of the rule set, if any (UBL 2.1 for the UBL rule sets), with
einvoice_xsd: every validity error, with its line and column; - every stylesheet of the rule set, with
einvoice_xslt_exec, which executes the official compiled Schematron with the behaviour of the reference engine (Saxon-HE 12.10) and writes its SVRL report.
The Report holds the schema errors, then, for each stylesheet, its raw SVRL
report, its findings (failed asserts and successful reports, with their id, flag,
location, rule context, test and message) and the canonical form of the SVRL. The
verdict is Verdict::Valid when there is no schema error and no finding with flag
fatal.
use einvoice_validator::{Validator, Verdict};
let validator = Validator::cen_ubl()?;
let invoice = br#"<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"/>"#;
let report = validator.validate(invoice)?;
assert_eq!(report.verdict(), Verdict::Invalid);
// The schema wants the mandatory elements; the CEN rules report what is missing.
assert!(!report.schema_errors.is_empty());
assert!(report.findings().any(|f| f.id.as_deref() == Some("BR-01")));
assert_eq!(report.rule_set.version, "1.3.17");§Rule sets and bundles
A rule set is described by a bundle: a manifest and the files it names. The
manifest gives the rule set’s name, title and version, the XML schema to validate
against first, and the stylesheets to run, each with the SHA-256 of its file, which
is checked when the bundle is loaded. RuleSet is the manifest, parsed.
- Built in (
BUILTIN_RULE_SETS,Validator::builtin), embedded unmodified, each behind a feature of its name, on by default (the artefacts are under the EUPL-1.2):cen-ubl, the CEN EN 16931 validation artefacts for UBL, release 1.3.17, with the UBL 2.1 schemas;cen-cii, the same release for UN/CEFACT CII D16B, without an XML schema (the CII D16B schemas are not embedded: documents are checked for well-formedness, then by the stylesheet).Syntax::detecttells from the root element which of the two a document is for. - From a directory (
Validator::from_bundle_dir): the manifest is<dir>/bundle.toml(MANIFEST_FILE), and the paths it gives are relative to<dir>. This is how rule sets whose artefacts cannot be redistributed are used: fetch them into a directory, add the manifest. - From memory (
Validator::from_bundle).
The manifest is TOML, restricted to the subset below; anything else is refused with the line concerned (unknown keys included).
format = 1 # the manifest format, MANIFEST_FORMAT
name = "cen-ubl" # ASCII letters, digits, '-', '.', '_'
title = "CEN EN 16931 UBL"
version = "1.3.17" # the version of the official artefacts
schema = "ubl-2.1" # optional: the XML schema validated first
[[stylesheet]] # one or more, run in this order
name = "EN16931-UBL" # unique in the bundle
path = "ubl/xslt/EN16931-UBL-validation.xslt" # relative, '/'-separated, no '..'
sha256 = "fc4e6cfa8596bba0c2abd19038a98960c6ee9fc1f44566787c115efd4f4aca7c"
# version = "..." # optional: when it differs from the bundle'sAccepted syntax: comments, key = value lines with a bare key, values that are
basic strings (escapes \" and \\ only), literal strings or non-negative integers,
and [[stylesheet]] headers.
§WebAssembly
The crate builds for wasm32-unknown-unknown and needs nothing from the host there:
the built-in rule set is embedded, and a bundle can be given from memory. That target
has no system clock, so the caller gives the one the stylesheets read with
ValidateOptions::clock (for instance Clock::from_unix_millis with
JavaScript’s Date.now()); without it, a validation fails with
Error::Unsupported. The wasm-validate example validates two embedded documents
that way and runs under wasmtime.
§Licences
The code is MIT. The built-in rule sets cen-ubl and cen-cii embed the CEN
stylesheets, under the EUPL-1.2, and einvoice_xsd embeds the OASIS UBL 2.1 schemas, which keep their own
terms (OASIS, UN/CEFACT, W3C). licences() returns the full text of every licence
of what this build embeds, for programs that must show them to their users.
§Errors
A schema error or a failed assert is not an error: it is in the Report. An
Error means no complete verdict could be given: a bundle that cannot be loaded,
a document that is not XML, a construct the engine refuses rather than evaluating it
differently from the reference engine, a document beyond a limit, a dynamic error
raised by a stylesheet, or a bug.
§Limits
The stylesheets walk the document with template rules applied one element level
inside another, so the stack a validation needs grows with the depth of the document.
A document with an element nested deeper than ValidateOptions::max_depth
(DEFAULT_MAX_DEPTH, 256, libxml2’s default) is therefore refused before its schema
is checked, with Error::Limit and the line and column of that element, rather
than overflowing the stack of the thread, which would abort the process. At the
default maximum, a validation with the built-in rule sets or the Peppol bundle runs on
a thread with the default 2 MiB stack of a spawned thread, debug builds included. The
template and function invocations of the stylesheets are bounded too: in nesting
(max_nesting_for_depth of the maximum depth) and in the stack they use
(ValidateOptions::max_stack, DEFAULT_MAX_STACK), so that a stylesheet that
recurses without end fails with Error::Limit as well.
Structs§
- Clock
- The clock of a validation (
ValidateOptions::clock): the implicit timezone and the value offn:current-dateTime(). The clock of an evaluation: the implicit timezone and the value offn:current-dateTime(). Both are inputs: the evaluator never reads a clock itself, and the current date and time stays the same for every evaluation made with the clock, as Saxon reads its clock once per transformation. - Finding
- A failed assert or a successful report of a stylesheet, as the SVRL report states it.
- Licence
- A licence under which part of a crate is distributed: one term of the crate’s
licence expression, what it covers, and its full text, as shipped in the crate’s
LICENSES/directory. - Report
- The result of validating one document: the schema errors, then what each stylesheet of the rule set reported.
- RuleSet
- A rule set, as described by the manifest of its bundle: its name and version, the XML schema to validate against first, and the stylesheets to run, in order, with their checksums.
- Rule
SetVersion - The rule set a report was produced with.
- Schema
Error - One validity error of a document.
- Stylesheet
Entry - One stylesheet of a rule set, as its manifest describes it.
- Stylesheet
Failure - A dynamic error raised by a stylesheet on a document (the reference engine raises it as well), which stopped the stylesheet.
- Stylesheet
Report - What one stylesheet of the rule set reported on a document.
- Validate
Options - Options of one validation.
- Validator
- A rule set ready to validate documents: its schema loaded and its stylesheets
prepared, once. Validating a document only reads it, so one
Validatorserves any number of documents, from any number of threads at once (Send + Sync); nothing of the rule set is copied per document.
Enums§
- Error
- Why a rule set could not be loaded, or a document could not be validated.
- Error
Kind - The kind of an
Error, for callers that only need to classify it (the exit code of a command-line tool, …). - Finding
Kind - What a finding is in the SVRL report.
- Limit
Exceeded - The default maximum element depth of a document (
ValidateOptions::max_depth), and the limitsError::Limitreports. A limit that a document or a transformation exceeded: the input is refused rather than processed with an unbounded call stack. - Schema
Error Kind - The kind of a
ValidationError. The set and the names ofErrorKind::as_strare stable. - Schema
Id - The XML schema a rule set validates documents against before running its stylesheets.
- Severity
- The severity of a finding, from its Schematron
flag, as the official rule sets use it. - Syntax
- The syntax of an e-invoice, as its root element names it.
- Verdict
- The overall verdict on a document.
Constants§
- BUILTIN_
RULE_ SETS - The names of the built-in rule sets of this build (each is a cargo feature of the
same name,
cen-ublandcen-ciion by default). - DEFAULT_
MAX_ DEPTH - The default maximum element depth of a document (
ValidateOptions::max_depth), and the limitsError::Limitreports. The maximum element depth of a parsed document, unless the caller sets another one: the document element is at depth 1, its child elements at depth 2, and so on. - DEFAULT_
MAX_ STACK - The default stack the stylesheets may use (
ValidateOptions::max_stack). The default ofTransformOptions::max_stack: 1.5 MiB, for a thread with the default 2 MiB stack of a spawned thread; 640 KiB on WebAssembly, whose stack is 1 MiB. With it, the official stylesheets run on a documentDEFAULT_MAX_DEPTHdeep, in a debug build too, and the 512 KiB left (384 KiB on WebAssembly) are more than the caller and one level of invocation need. - MANIFEST_
FILE - The file name of the manifest in a bundle directory.
- MANIFEST_
FORMAT - The manifest format this crate reads (the
formatkey). - VERSION
- The version of this crate, as published.
Functions§
- file_
uri - The
file:URI that the reference engine (Saxon-HE) gives a document read from the file atpath(absolute).Nonefor a relative path or one that is not Unicode. - licences
- Every licence of what this build of the crate embeds in a program, with its full
text: the MIT licence of the code (of this crate and of the crates of the project
it depends on, which share that licence), the EUPL-1.2 of the CEN stylesheets when
the feature
cen-ublorcen-ciiis on, then the terms of the UBL 2.1 schemas embedded byeinvoice_xsd(OASIS, UN/CEFACT, W3C). - max_
nesting_ for_ depth - The nesting of template and function invocations the stylesheets are allowed on a
document at most
max_depthdeep (ValidateOptions::max_depth): twice the depth, plus 64. Each level needs less than 3 KiB of stack (debug builds included), which is what a caller raising the maximum depth must give the validating thread. The nesting of invocations that the official stylesheets need on a documentmax_depthdeep, with room to spare: twice the depth, plus 64.