feat(validation): add validators
This commit is contained in:
66
ts/index.ts
66
ts/index.ts
@ -9,7 +9,28 @@ import { XInvoiceDecoder } from './formats/xinvoice.decoder.js';
|
||||
import { DecoderFactory } from './formats/decoder.factory.js';
|
||||
import { BaseDecoder } from './formats/base.decoder.js';
|
||||
|
||||
// Export interfaces
|
||||
// Import validator classes
|
||||
import { ValidatorFactory } from './formats/validator.factory.js';
|
||||
import { BaseValidator } from './formats/base.validator.js';
|
||||
import { FacturXValidator } from './formats/facturx.validator.js';
|
||||
import { UBLValidator } from './formats/ubl.validator.js';
|
||||
|
||||
// Export specific interfaces for easier use
|
||||
export type {
|
||||
IXInvoice,
|
||||
IParty,
|
||||
IAddress,
|
||||
IContact,
|
||||
IInvoiceItem,
|
||||
ValidationError,
|
||||
ValidationResult,
|
||||
ValidationLevel,
|
||||
InvoiceFormat,
|
||||
XInvoiceOptions,
|
||||
IValidator
|
||||
} from './interfaces.js';
|
||||
|
||||
// Export interfaces (legacy support)
|
||||
export { interfaces };
|
||||
|
||||
// Export main class
|
||||
@ -30,6 +51,47 @@ export {
|
||||
XInvoiceDecoder
|
||||
};
|
||||
|
||||
// Export validator classes
|
||||
export const Validators = {
|
||||
ValidatorFactory,
|
||||
BaseValidator,
|
||||
FacturXValidator,
|
||||
UBLValidator
|
||||
};
|
||||
|
||||
// For backward compatibility
|
||||
export { FacturXEncoder as ZugferdXmlEncoder };
|
||||
export { FacturXDecoder as ZUGFeRDXmlDecoder };
|
||||
export { FacturXDecoder as ZUGFeRDXmlDecoder };
|
||||
|
||||
/**
|
||||
* Validates an XML string against the appropriate format rules
|
||||
* @param xml XML content to validate
|
||||
* @param level Validation level (syntax, semantic, business)
|
||||
* @returns ValidationResult with the result of validation
|
||||
*/
|
||||
export function validateXml(
|
||||
xml: string,
|
||||
level: interfaces.ValidationLevel = interfaces.ValidationLevel.SYNTAX
|
||||
): interfaces.ValidationResult {
|
||||
try {
|
||||
const validator = ValidatorFactory.createValidator(xml);
|
||||
return validator.validate(level);
|
||||
} catch (error) {
|
||||
return {
|
||||
valid: false,
|
||||
errors: [{
|
||||
code: 'VAL-ERROR',
|
||||
message: `Validation error: ${error.message}`
|
||||
}],
|
||||
level
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new XInvoice instance
|
||||
* @returns A new XInvoice instance
|
||||
*/
|
||||
export function createXInvoice(): XInvoice {
|
||||
return new XInvoice();
|
||||
}
|
Reference in New Issue
Block a user