// SPDX-License-Identifier: EUPL-1.2 // Structural check against the UBL 2.1 schema for the 42 complex types listed in // ubl-xsd-table.ts: allowed child elements, their sequence order, required children and // maximum occurrence, plus the lexical form of decimal, date and boolean leaf values. // This is not a complete XML Schema validation: types outside the table (reachable only // through elements that EN 16931 does not use) are not descended into. import { parseDec, parseIsoDate } from "./decimal"; import type { RuleContext, RuleRow } from "./types"; import { UBL_XSD_ELEMENT_TYPES, UBL_XSD_TYPES } from "./ubl-xsd-table"; import type { XmlNode } from "./xml"; export const STRUCTURE_RULES: readonly RuleRow[] = [ ["RS-UBL-02", "fatal", "The element is not allowed at this position by the UBL 2.1 schema."], ["RS-UBL-03", "fatal", "The element is out of sequence. The UBL 2.1 schema fixes the order of child elements."], ["RS-UBL-04", "fatal", "A child element required by the UBL 2.1 schema is missing."], ["RS-UBL-05", "fatal", "The value does not match its UBL 2.1 data type (decimal, date or boolean)."], ["RS-UBL-06", "fatal", "The element may occur only once at this position according to the UBL 2.1 schema."] ]; interface ChildSpec { readonly name: string; readonly position: number; readonly required: boolean; readonly unbounded: boolean; } function expand(reference: string): string { if (reference.startsWith("a:")) return `cac:${reference.slice(2)}`; if (reference.startsWith("b:")) return `cbc:${reference.slice(2)}`; return `ext:${reference.slice(2)}`; } function buildTypes(): Map { const types = new Map(); for (const line of UBL_XSD_TYPES) { const separator = line.indexOf("="); const children = line .slice(separator + 1) .split(",") .map((token, position): ChildSpec => { const unbounded = token.endsWith("*"); const withoutStar = unbounded ? token.slice(0, -1) : token; const required = withoutStar.endsWith("!"); return { name: expand(required ? withoutStar.slice(0, -1) : withoutStar), position, required, unbounded }; }); types.set(line.slice(0, separator), children); } return types; } const TYPES = buildTypes(); const TYPE_LOOKUP = new Map>( [...TYPES].map(([typeName, children]) => [typeName, new Map(children.map((child) => [child.name, child]))]) ); const ELEMENT_TYPE = new Map( UBL_XSD_ELEMENT_TYPES.split(" ").map((pair) => { const separator = pair.indexOf(">"); return [`cac:${pair.slice(0, separator)}`, pair.slice(separator + 1)]; }) ); /** Number of schema types in the table; tests compare it with releases.ts. */ export const STRUCTURE_TABLE_SIZE = TYPES.size; const BOOLEAN_PATTERN = /^[ \t\r\n]*(?:true|false|1|0)[ \t\r\n]*$/; const DECIMAL_SUFFIXES = ["Amount", "Quantity", "Percent", "Numeric", "Measure", "Rate"] as const; type LeafKind = "decimal" | "date" | "boolean"; function leafKind(local: string): LeafKind | null { if (local.endsWith("Date")) return "date"; if (local.endsWith("Indicator")) return "boolean"; return DECIMAL_SUFFIXES.some((suffix) => local.endsWith(suffix)) ? "decimal" : null; } function validLeaf(kind: LeafKind, text: string): boolean { if (kind === "decimal") return parseDec(text) !== null; if (kind === "date") return parseIsoDate(text) !== null; return BOOLEAN_PATTERN.test(text); } export function runStructureChecks(context: RuleContext): void { const { root, emit, isCreditNote } = context; const visit = (node: XmlNode, typeName: string): void => { const lookup = TYPE_LOOKUP.get(typeName); const children = TYPES.get(typeName); if (!lookup || !children) return; const counts = new Map(); let highest = -1; let highestName = ""; for (const child of node.children) { const spec = lookup.get(child.q); if (!spec) { emit("RS-UBL-02", child, `<${child.q}> inside <${node.q}>`); continue; } if (spec.position < highest) { emit("RS-UBL-03", child, `<${child.q}> must come before <${highestName}>`); } else { highest = spec.position; highestName = child.q; } const seen = (counts.get(child.q) ?? 0) + 1; counts.set(child.q, seen); if (seen === 2 && !spec.unbounded) emit("RS-UBL-06", child, `<${child.q}> inside <${node.q}>`); const childType = ELEMENT_TYPE.get(child.q); if (childType !== undefined) visit(child, childType); } for (const spec of children) { if (spec.required && !counts.has(spec.name)) emit("RS-UBL-04", node, `<${spec.name}> inside <${node.q}>`); } }; visit(root, isCreditNote ? "CreditNoteType" : "InvoiceType"); for (const node of context.index.elements) { if (!node.q.startsWith("cbc:") || node.children.length > 0) continue; const kind = leafKind(node.local); if (kind !== null && !validLeaf(kind, node.text)) emit("RS-UBL-05", node, `<${node.q}> is not a valid ${kind}`); } }