feat(core): improve in-memory validation, FatturaPA detection coverage, and published type compatibility
This commit is contained in:
@@ -4,31 +4,35 @@ import type { ValidationResult } from '../../interfaces/common.js';
|
||||
import { CII_NAMESPACES, CIIProfile } from './cii.types.js';
|
||||
import { DOMParser, xpath } from '../../plugins.js';
|
||||
|
||||
const ciiValidatorParser = new DOMParser();
|
||||
const ciiValidatorNamespaces = {
|
||||
rsm: CII_NAMESPACES.RSM,
|
||||
ram: CII_NAMESPACES.RAM,
|
||||
udt: CII_NAMESPACES.UDT,
|
||||
};
|
||||
const ciiValidatorSelect = xpath.useNamespaces(ciiValidatorNamespaces);
|
||||
|
||||
/**
|
||||
* Base validator for CII-based invoice formats
|
||||
*/
|
||||
export abstract class CIIBaseValidator extends BaseValidator {
|
||||
protected doc: Document;
|
||||
protected namespaces: Record<string, string>;
|
||||
protected select: xpath.XPathSelect;
|
||||
protected doc!: Document;
|
||||
protected namespaces!: Record<string, string>;
|
||||
protected select!: xpath.XPathSelect;
|
||||
protected profile: CIIProfile = CIIProfile.EN16931;
|
||||
|
||||
constructor(xml: string) {
|
||||
constructor(xml: string, doc?: Document) {
|
||||
super(xml);
|
||||
|
||||
try {
|
||||
// Parse XML document
|
||||
this.doc = new DOMParser().parseFromString(xml, 'application/xml');
|
||||
// Reuse an existing parsed document when available.
|
||||
this.doc = doc ?? ciiValidatorParser.parseFromString(xml, 'application/xml');
|
||||
|
||||
// Set up namespaces for XPath queries
|
||||
this.namespaces = {
|
||||
rsm: CII_NAMESPACES.RSM,
|
||||
ram: CII_NAMESPACES.RAM,
|
||||
udt: CII_NAMESPACES.UDT
|
||||
};
|
||||
this.namespaces = ciiValidatorNamespaces;
|
||||
|
||||
// Create XPath selector with namespaces
|
||||
this.select = xpath.useNamespaces(this.namespaces);
|
||||
this.select = ciiValidatorSelect;
|
||||
|
||||
// Detect profile
|
||||
this.detectProfile();
|
||||
@@ -139,7 +143,8 @@ export abstract class CIIBaseValidator extends BaseValidator {
|
||||
* @returns Text value or empty string if not found
|
||||
*/
|
||||
protected getText(xpathExpr: string, context?: Node): string {
|
||||
const node = this.select(xpathExpr, context || this.doc)[0];
|
||||
const result = this.select(xpathExpr, context || this.doc);
|
||||
const node = Array.isArray(result) ? result[0] : null;
|
||||
return node ? (node.textContent || '') : '';
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user