Validates document structure against official SII XSD schemas:
use Derafu\Xml\XmlDocument;// Validate from DocumentBagtry { $validatorWorker->validateSchema($bag); echo "XML schema is valid!";} catch (\Exception $e) { echo "Schema validation failed: " . $e->getMessage();}// Validate from XML string$xmlString = file_get_contents('/path/to/dte.xml');try { $validatorWorker->validateSchema($xmlString);} catch (\Exception $e) { echo "Invalid XML structure: " . $e->getMessage();}// Validate from XmlDocument$xmlDocument = new XmlDocument();$xmlDocument->loadXml($xmlString);try { $validatorWorker->validateSchema($xmlDocument);} catch (\Exception $e) { echo "Schema error: " . $e->getMessage();}
Schema validation uses the official SII DTE XSD schema located at resources/schemas/DTE_v10.xsd. Receipts (boletas) are validated through their envelope (EnvioBOLETA) rather than individually.
If a document’s XML is modified after signing, signature validation will fail. Always validate signatures before processing documents from external sources.
For advanced use cases, you can skip validation during document creation:
$bag = $documentComponent->bill( data: $documentData, caf: $caf, certificate: $certificate, options: [ 'normalizer' => [ 'normalize' => false, // Skip normalization and validation ], ]);// Document is created without validation// Use with caution - may result in invalid documents!
Disabling validation can result in invalid documents that SII will reject. Only disable validation if you’re certain your data is already validated and normalized.
For specific document types, different validation strategies are used:
// The ValidatorWorker uses strategies for each document type// ValidatorWorker.php:87$strategy = $this->getStrategy($bag->getTipoDocumento()->getAlias());$strategy->validate($bag);
// If you provide totals, they must be correct'Totales' => [ 'MntNeto' => 100000, 'IVA' => 19000, 'MntTotal' => 120000, // Wrong! Should be 119000]// Better: Let LibreDTE calculate totals// Just provide item details and omit Totales
// The validator needs to know document typeif (!$bag->getTipoDocumento()) { throw new ValidatorException( 'No es posible validar sin un TipoDocumento en la $bag.' );}
Validate data before creating CAF and certificate resources:
// Create bag without CAF/certificate first$bag = new DocumentBag(inputData: $data);// Validate$validatorWorker->validate($bag);// Only if valid, add expensive resources$bag->setCaf($caf);$bag->setCertificate($certificate);
2
Validate Before Sending
Always validate before sending to SII:
$validatorWorker->validate($bag);$validatorWorker->validateSchema($bag);$validatorWorker->validateSignature($bag);// Only then send to SII$trackId = $siiWorker->sendXmlDocument(...);
Use the normalization and sanitization workers before validation. They automatically fix many common data issues like formatting and missing calculated fields.