Skip to content

Validation

horstoeko edited this page Apr 12, 2024 · 22 revisions

Table of contents

Different mechanisms

There are generally two classes in this library that can validate documents:

  • A pure schema validation against the schema (XSD) by the class ZugferdXsdValidator.
  • A complete validation including business rules by the class ZugferdKositValidator. This class relies on an external tool from the Coordination Office for IT Standards (KoSIT) (German institution of the Federal Republic of Germany) to perform the validation. The validation currently only takes into account validation in accordance with EN16931 and the German CIUS XRechnung. For more details see KoSIT Validator on GitHub. At present, this feature is still to be considered experimental.

Pure XSD Validation

If only a pure validation against the XSD schema is required, then the class ZugferdXsdValidator is completely sufficient. A handling description is available in the form of an example implementation in the examples folder under XsdValidator.php.

Using the class ZugferdXsdValidator is very simple: When a new instance of this class is created, the constructor expects a parameter of the type ZugferdDocument. This makes it possible to check/validate both incoming documents from ZugferdDocumentReader and ZugferdDocumentPdfReader as well as outgoing documents from ZugferdDocumentBuilder:

From ZugferdDocumentReader:

$document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/incoming.xml");

$xsdValidator = new ZugferdXsdValidator($document);
$xsdValidator->validate();

From ZugferdDocumentPdfReader:

$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/incoming.pdf");

$xsdValidator = new ZugferdXsdValidator($document);
$xsdValidator->validate();

From ZugferdDocumentBuilder

$document = ZugferdDocumentBuilder::CreateNew(ZugferdProfiles::PROFILE_EN16931);
$document
    ->setDocumentInformation("471102", "380", \DateTime::createFromFormat("Ymd", "20180305"), "EUR")
    .....

$xsdValidator = new ZugferdXsdValidator($document);
$xsdValidator->validate();

After executing the validate method, further methods are available to determine any validation errors:

Method Description
$xsdValidator->validationPased Returns true or false. True if the validation has been passed successfully
$xsdValidator->validationFailed Returns true or false. True if the validation failed
$xsdValidator->validationErrors Returns an array of strings. Each array element contains a validation error

Validation using the Kosit Validation tool

Important note: At present, this feature is still to be considered experimental.

For a more extensive validation, use the class ZugferdKositValidator. Please note that this class requires a functioning and complete JAVA installation (e.g. openjdk-11-jre-headless for Linux operating systems) to perform the validation correctly, as the external tool (KoSIT-Validator) is a pure JAVA application that is called by the class. A handling description is also available in the form of an example implementation in the examples folder under XsdValidator.php.

As with the ZugferdXsdValidator, this class can receive several different input instances: ZugferdDocumentReader, ZugferdDocumentPdfReader and ZugferdDocumentBuilder. See ZugferdXsdValidator.

By default, the class ZugferdKositValidator uses version 1.5.0 of the KoSIT validator and the validation rule from 15.11.2023. You can also change the version to be used by changing the download URL:

$kositValidator = new ZugferdKositValidator()
$kositValidator->setValidatorDownloadUrl('https://github.com/itplr-kosit/validator/releases/download/v1.4.2/validator-1.4.2-distribution.zip');
$kositValidator->setValidatorScenarioDownloadUrl('https://github.com/itplr-kosit/validator-configuration-xrechnung/releases/download/release-2021-00-31/validator-configuration-xrechnung_3.0.1_2023-11-15.zip');

After changing the version by changing the download URL of the JAVA application, it may also be necessary to change the file name of the JAR file to be executed:

$kositValidator->setValidatorAppJarFilename('validationtool-1.4.2-java8-standalone.jar');