UNPKG

@fin.cx/einvoice

Version:

A TypeScript module for creating, manipulating, and embedding XML data within PDF files specifically tailored for electronic invoice (einvoice) packages.

134 lines (113 loc) 4 kB
import { BaseValidator } from '../base/base.validator.js'; import { InvoiceFormat, ValidationLevel } from '../../interfaces/common.js'; import type { ValidationResult } from '../../interfaces/common.js'; import { FormatDetector } from '../utils/format.detector.js'; // Import specific validators import { EN16931UBLValidator } from '../ubl/en16931.ubl.validator.js'; import { XRechnungValidator } from '../ubl/xrechnung.validator.js'; import { FacturXValidator } from '../cii/facturx/facturx.validator.js'; import { ZUGFeRDValidator } from '../cii/zugferd/zugferd.validator.js'; // The EN16931UBLValidator handles all UBL-based formats with proper business rules // No need for legacy validator implementations here /** * FatturaPA validator implementation * Basic implementation for Italian electronic invoices */ class FatturaPAValidator extends BaseValidator { validate(level: ValidationLevel = ValidationLevel.SYNTAX): ValidationResult { // Reset errors this.errors = []; let valid = true; if (level === ValidationLevel.SYNTAX) { valid = this.validateSchema(); } else if (level === ValidationLevel.SEMANTIC || level === ValidationLevel.BUSINESS) { valid = this.validateSchema() && this.validateBusinessRules(); } return { valid, errors: this.errors, level }; } protected validateSchema(): boolean { // Basic schema validation for FatturaPA if (!this.xml.includes('<FatturaElettronica')) { this.addError('FATT-SCHEMA-1', 'Root element must be FatturaElettronica', '/'); return false; } return true; } protected validateBusinessRules(): boolean { // Basic placeholder implementation - would need more detailed rules // for a real implementation return this.validateSchema(); } } /** * Factory to create the appropriate validator based on the XML format */ export class ValidatorFactory { /** * Creates a validator for the specified XML content * @param xml XML content to validate * @returns Appropriate validator instance */ public static createValidator(xml: string): BaseValidator { try { const format = FormatDetector.detectFormat(xml); switch (format) { case InvoiceFormat.UBL: return new EN16931UBLValidator(xml); case InvoiceFormat.XRECHNUNG: return new XRechnungValidator(xml); case InvoiceFormat.CII: // For now, use Factur-X validator for generic CII return new FacturXValidator(xml); case InvoiceFormat.ZUGFERD: return new ZUGFeRDValidator(xml); case InvoiceFormat.FACTURX: return new FacturXValidator(xml); case InvoiceFormat.FATTURAPA: return new FatturaPAValidator(xml); default: // For unknown formats, provide a generic validator that will // mark the document as invalid but won't throw an exception return new GenericValidator(xml, format); } } catch (error) { // If an error occurs during validator creation, return a generic validator // that will provide meaningful error information instead of throwing console.error(`Error creating validator: ${error}`); return new GenericValidator(xml, 'unknown'); } } } /** * Generic validator for unknown or unsupported formats * Provides meaningful validation errors instead of throwing exceptions */ class GenericValidator extends BaseValidator { private format: string; constructor(xml: string, format: string) { super(xml); this.format = format; this.addError( 'GEN-1', `Unsupported invoice format: ${format}`, '/' ); } validate(level: ValidationLevel = ValidationLevel.SYNTAX): ValidationResult { return { valid: false, errors: this.errors, level }; } protected validateSchema(): boolean { return false; } protected validateBusinessRules(): boolean { return false; } }