nav-osa-core
v0.3.1
Published
Shared TypeScript types, XML parser, XSD validator, and XML builder for the Hungarian NAV Online Invoice System (OSA)
Downloads
168
Maintainers
Readme
nav-osa-core
Shared TypeScript types, XML parser, XSD validator, and XML builder for the Hungarian NAV Online Invoice System (OSA) version 3.0.
Hybrid architecture: parsing is done with libxml2-wasm (libxml2 compiled to WebAssembly), building with fast-xml-builder.
Contents
- Generic XML parser built on
libxml2-wasmconfigured for NAV XML documents - XML builders:
buildInvoiceXmlfor invoice data,buildApiRequestXmlfor API request XML (built onfast-xml-builder) - XSD validation with built-in lazy validator cache (libxml2-wasm)
validateAndExtractFields: validate and extract only specific fields from the DOM without building the full parse objectextractFields: extract specific fields from well-formed XML without XSD validation
Installation
npm install nav-osa-core nav-osa-typesUsage
Types
Types are provided by the separate nav-osa-types package:
import { InvoiceData, TaxNumberType, MonetaryType } from 'nav-osa-types';Parse XML
schemaName is required as the second argument — the XML is validated against that schema before parsing. Validation can be disabled with validate: false:
import { xmlParser, XsdSchemaName } from 'nav-osa-core';
import { InvoiceData } from 'nav-osa-types';
const result = await xmlParser<{ InvoiceData: InvoiceData }>(xmlString, XsdSchemaName.Data);
const resultNoValidation = await xmlParser(xmlString, XsdSchemaName.Data, { validate: false });If validation fails, a detailed XmlValidationError is thrown; a document without a root element also throws XmlValidationError instead of returning an empty object:
import { XmlValidationError } from 'nav-osa-core';
try {
const result = await xmlParser(xmlString, XsdSchemaName.Data);
} catch (err) {
if (err instanceof XmlValidationError) {
console.log('Validation failed:', err.errors);
}
}Validate XML
import { validateXml, ValidationResult, XsdSchemaName } from 'nav-osa-core';
const result: ValidationResult = await validateXml(xmlString, XsdSchemaName.Data);
if (!result.valid) {
console.log('Errors:', result.errors);
}Validators are cached by schema name after the first call.
Validate XML and extract specific fields
When you only need a few fields (e.g. invoiceNumber, invoiceReference) after validation, use validateAndExtractFields — it validates the document and reads only the requested fields directly from the libxml2-wasm DOM via WASM pointer traversal, skipping the full parse object. This minimizes string copies across the WASM↔V8 boundary.
import { validateAndExtractFields, XsdSchemaName } from 'nav-osa-core';
const { fields, errors } = await validateAndExtractFields(
xmlString,
XsdSchemaName.Data,
['invoiceNumber', 'originalInvoiceNumber', 'modifyWithoutMaster', 'modificationIndex']
);
// fields.invoiceNumber === 'ZZZ000009'
// fields.modifyWithoutMaster === 'false'Important: the returned
fieldsare not a typed JSON object — there is no type conversion (no booleans, no numbers, no nested structure). You get a flatRecord<string, string | string[]>of raw tag contents as they appear in the XML:
'false'is the string"false", not the booleanfalse'1'is the string"1", not the number1- The plain
xmlParser(full parse) is the typed path; use that when you need the typedInvoiceDatastructure.
- Repeated fields (e.g.
lineNumberin multi-line invoices) are aggregated intostring[].
Typed extraction with convertValues: true
You can opt into the same tag-name based type conversion that the full parse uses (known boolean/number fields become true/false and numbers, known string fields stay strings):
const { fields } = await validateAndExtractFields(
xmlString,
XsdSchemaName.Data,
['invoiceNumber', 'modifyWithoutMaster', 'modificationIndex'],
{ convertValues: true }
);
// fields.modifyWithoutMaster === false // boolean
// fields.modificationIndex === 1 // number
// fields.invoiceNumber === 'ZZZ000009' // string (STRING_FIELDS)The result type can also be narrowed via the generic parameter: validateAndExtractFields<MyFieldShape>(...).
- By default an invalid document throws
XmlValidationError; pass{ errorMode: 'return' }to get the results with anerrorsarray instead:
const { fields, errors } = await validateAndExtractFields(
xmlString,
XsdSchemaName.Data,
['invoiceNumber'],
{ errorMode: 'return' }
);
if (errors.length > 0) {
console.log('Validation failed:', errors);
}The low-level extractFieldsFast BFS traversal is internal: it walks a raw libxml2-wasm document pointer and is not part of the public API.
Extract fields without XSD validation
extractFields reads only the requested fields from well-formed XML — it does no XSD validation. It is namespace-agnostic and handles entities, CDATA, comments and whitespace via libxml2-wasm, making it a drop-in replacement for regexp-based helpers:
import { extractFields } from 'nav-osa-core';
const { fields, errors } = await extractFields(
xmlString,
['invoiceNumber', 'originalInvoiceNumber'],
);It accepts the same options as validateAndExtractFields (convertValues, maxXmlSize, errorMode) and returns the same { fields, errors } shape. On ill-formed XML it throws XmlValidationError by default; pass { errorMode: 'return' } to get the errors in the errors array instead:
const { fields, errors } = await extractFields(
xmlString,
['invoiceNumber'],
{ errorMode: 'return' },
);
if (errors.length > 0) {
console.log('Well-formedness check failed:', errors);
}Because it skips XsdValidator.validate() entirely, extractFields is ~30-40% faster than validateAndExtractFields when you only need well-formedness, not schema conformance.
Build invoice XML from JSON
Converts an InvoiceData object to XML and validates it against the built-in data.xsd schema. Validation is mandatory and cannot be disabled — the builder never returns XML that the tax authority would reject:
import { buildInvoiceXml } from 'nav-osa-core';
import { InvoiceData } from 'nav-osa-types';
const invoice: InvoiceData = {
invoiceNumber: 'ABC-2025-001',
invoiceIssueDate: '2025-01-15',
completenessIndicator: false,
invoiceMain: {
invoice: {
invoiceHead: {
supplierInfo: { /* ... */ },
invoiceDetail: { /* ... */ },
},
invoiceSummary: { /* ... */ },
},
},
};
const xml = await buildInvoiceXml(invoice);If validation fails, a detailed XmlValidationError is thrown:
import { XmlValidationError } from 'nav-osa-core';
try {
const xml = await buildInvoiceXml(invoice);
} catch (err) {
if (err instanceof XmlValidationError) {
console.log('XSD validation errors:', err.errors);
}
}Build API request XML
Build and validate API request XML (TokenExchangeRequest, QueryInvoiceDigestRequest, etc.) with an ApiRequestType enum member as the root element. Validation is mandatory and cannot be disabled (validate: false exists only on the parsing side). The namespace declarations and the common: prefixing of the header/user subtrees are applied automatically:
import { buildApiRequestXml, ApiRequestType } from 'nav-osa-core';
const xml = await buildApiRequestXml(ApiRequestType.TokenExchangeRequest, {
header: {
requestId: 'RID...',
timestamp: '2025-01-01T00:00:00.000Z',
requestVersion: '3.0',
headerVersion: '1.0',
},
user: {
login: 'user',
passwordHash: {
'@_cryptoType': 'SHA-512',
'#text': 'hash...',
},
taxNumber: '12345678',
requestSignature: {
'@_cryptoType': 'SHA3-512',
'#text': 'sig...',
},
},
software: {
softwareId: '123456789012345678',
softwareName: 'TestApp',
softwareOperation: 'LOCAL_SOFTWARE',
softwareMainVersion: '1.0',
softwareDevName: 'Dev',
softwareDevContact: '[email protected]',
},
});The builder injects xmlns="http://schemas.nav.gov.hu/OSA/3.0/api" and xmlns:common="http://schemas.nav.gov.hu/NTCA/1.0/common" on the root element and prefixes the header and user subtrees — the only common-namespace parts of every OSA API request — with common:, so the output becomes:
<TokenExchangeRequest xmlns="..." xmlns:common="...">
<common:header>
<common:requestId>RID...</common:requestId>
...
</common:header>
<common:user>...</common:user>
<software>...</software>
</TokenExchangeRequest>An unknown request type is rejected with an XmlBuildError before any XML is produced. The enum contains exactly the request roots of invoiceApi.xsd:
TokenExchangeRequest,ManageAnnulmentRequest,ManageInvoiceRequestQueryInvoiceChainDigestRequest,QueryInvoiceCheckRequest,QueryInvoiceDataRequest,QueryInvoiceDigestRequestQueryTaxpayerRequest,QueryTransactionListRequest,QueryTransactionStatusRequest
XSD schemas
The module ships the official NAV XSD files and an enum to reference them:
XsdSchemaName.Common→common.xsd— NTCA Common typesXsdSchemaName.InvoiceBase→invoiceBase.xsd— Base invoice typesXsdSchemaName.Data→data.xsd— Invoice data typesXsdSchemaName.InvoiceApi→invoiceApi.xsd— API request/response types
import { validateXml, buildApiRequestXml, XsdSchemaName, ApiRequestType } from 'nav-osa-core';
// Validate against a named schema
await validateXml(xmlString, XsdSchemaName.Data);
// Build and validate API request
await buildApiRequestXml(ApiRequestType.TokenExchangeRequest, data);Security options
The parser keeps general (DOCTYPE) entity references unexpanded by default (processEntities: false): the XML_PARSE_NOENT flag is off, so internal entity expansion (billion laughs) is not processed and the built-in libxml2 safety limits stay active. The 5 predefined XML entities (& < > " ') and numeric character references (A A) are always decoded, per the XML spec — in both the libxml2-wasm and the fast-xml-parser (xmlParserFxp) paths. If you need general entity resolution for trusted XML, enable it explicitly:
import { xmlParser, XsdSchemaName } from 'nav-osa-core';
const result = await xmlParser(xmlString, XsdSchemaName.Data, { processEntities: true });Warning: Only enable entity processing when parsing XML you fully control. For external or untrusted input, keep the default — XML_PARSE_NOENT is what makes billion-laughs-style entity expansion possible.
Payload size limit
The parser rejects XML payloads larger than 10 MB by default. You can override this:
import { xmlParser, XsdSchemaName } from 'nav-osa-core';
const result = await xmlParser(xmlString, XsdSchemaName.Data, { maxXmlSize: 50 * 1024 * 1024 });Security
- Network access disabled (
NONET) — XML parsing never fetches external resources, preventing XXE (XML External Entity) attacks. - Entity expansion protection — general (DOCTYPE) entity references stay unexpanded by default (
processEntities: false), preventing billion laughs / exponential entity expansion attacks. Only the 5 predefined XML entities and numeric character references are decoded (XML spec). EnableprocessEntities: trueonly for trusted, self-generated XML. - Payload size limit (
maxXmlSize: 10 MB by default) — prevents memory exhaustion from oversized XML inputs. HUGEflag — intentionally not part of the default parse options: libxml2's built-in safety limits (oversized text, entity expansion) stay active. TheHUGEflag is used only when loading the built-in XSD schemas (trusted, shipped with the package).
Support
If you find this package useful, consider supporting the development:
License
Apache-2.0
