Validation
A document is validated against the official OASIS schemas bundled with the package - the same
ones real UBL consumers (Peppol Access Points, tax authorities) validate against: the XSD for
XML (XmlSchemaValidator) and the model-based JSON schemas of the OASIS note for JSON and YAML
(JsonSchemaValidator). There are two interfaces because what they validate is different: a
string of XML, or the decoded array of a JSON or YAML.
While Reading a Document
The simplest way is the validate option of Deserializer, for any of the three formats:
$quotation = $deserializer->deserialize($content, 'json', ['validate' => true]);
Validation is opt-in: without the option nothing is validated. A JSON or YAML is validated as
fromArray() is going to read it (after the normalizer, before the extension codecs).
Validating XML
use Derafu\Ubl\Service\XmlSchemaValidator;
$validator = new XmlSchemaValidator();
$validator->validate($xml); // Throws Derafu\Ubl\Exception\ValidationException if invalid.
Nothing is returned on success: a document either validates, or the call throws.
Which XSD file to validate against is resolved automatically from the XML’s own root element,
the same way Deserializer resolves a target class for xml (see Self-Describing Content in
Formats). The UBL version is the default one, or the second argument:
$validator->validate($xml, '2.2');
Validating JSON and YAML
use Derafu\Ubl\Service\JsonSchemaValidator;
$validator = new JsonSchemaValidator();
$validator->validate($data); // The decoded JSON or YAML: the apex and the document.
$validator->validate($data, '2.3'); // Of another UBL version.
The document type is resolved from the property of the data that names it (the only one that
does not start with _).
It needs opis/json-schema, which is only suggested: the package works without it, until the
validation of a JSON or YAML is requested, which throws a ValidationException that says what to
install.
OASIS has published the JSON schemas for UBL 2.1, 2.2 and 2.3 only. Validating the JSON or YAML of
2.4 or 2.5 throws a ValidationException saying so: there is no official schema, and this package
does not write one of its own.
The official JSON example of OrderResponse is not valid against the official schemas: the note
writes <cac:Item/> as {"_": ""} and the schema does not accept it. What this package writes
for it, {}, does validate.
Handling Validation Errors
A failed validation throws Derafu\Ubl\Exception\ValidationException. For XML it exposes the real
list of LibXMLError objects libxml reported, not only a flat message:
use Derafu\Ubl\Exception\ValidationException;
try {
$validator->validate($xml);
} catch (ValidationException $e) {
foreach ($e->getErrors() as $error) {
printf("Line %d: %s\n", $error->line, trim($error->message));
}
}
For JSON and YAML it is a list of strings, each one the path of the error in the document and its
message (/Invoice: The required properties (IssueDate) are missing).
A malformed XML string, or a well-formed one whose root element matches no known UBL document of
the version, also throws ValidationException - with getErrors() empty in both cases, since
neither one ever reaches schema validation itself: the document type could not be resolved to
begin with.