|
| 1 | +import { XMLParser } from 'fast-xml-parser'; |
| 2 | + |
| 3 | +export interface KositValidatorOptions { |
| 4 | + /** |
| 5 | + * The URL of the Kosit validator service. |
| 6 | + * @default 'http://localhost:8081/' |
| 7 | + */ |
| 8 | + endpoint?: string; |
| 9 | +} |
| 10 | + |
| 11 | +export interface KositValidationMessage { |
| 12 | + id: string; |
| 13 | + text: string; |
| 14 | + location: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface KositValidationResult { |
| 18 | + valid: boolean; |
| 19 | + errors: KositValidationMessage[]; |
| 20 | + warnings: KositValidationMessage[]; |
| 21 | + rawXml: string; |
| 22 | +} |
| 23 | + |
| 24 | +export class KositValidator { |
| 25 | + private readonly endpoint: string; |
| 26 | + private readonly parser: XMLParser; |
| 27 | + |
| 28 | + constructor(options?: KositValidatorOptions) { |
| 29 | + this.endpoint = options?.endpoint ?? 'http://localhost:8081/'; |
| 30 | + this.parser = new XMLParser({ |
| 31 | + ignoreAttributes: false, |
| 32 | + attributeNamePrefix: '$', |
| 33 | + parseTagValue: false, |
| 34 | + removeNSPrefix: true, |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Validates a UBL XML document against the Kosit validator service. |
| 40 | + * @param xml The XML document to validate. |
| 41 | + * @returns The validation result including validity status, errors, and warnings. |
| 42 | + */ |
| 43 | + public async validate(xml: string): Promise<KositValidationResult> { |
| 44 | + const response = await fetch(this.endpoint, { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + 'Content-Type': 'application/xml', |
| 48 | + }, |
| 49 | + body: xml, |
| 50 | + }); |
| 51 | + |
| 52 | + if (!response.ok && response.status !== 406) { |
| 53 | + throw new Error( |
| 54 | + `Kosit validator returned HTTP ${response.status}: ${response.statusText}` |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + const rawXml = await response.text(); |
| 59 | + return this.parseReport(rawXml); |
| 60 | + } |
| 61 | + |
| 62 | + private parseReport(rawXml: string): KositValidationResult { |
| 63 | + const parsed = this.parser.parse(rawXml) as Record<string, unknown>; |
| 64 | + |
| 65 | + const report = (parsed['report'] ?? parsed['createReportInput']) as |
| 66 | + | Record<string, unknown> |
| 67 | + | undefined; |
| 68 | + |
| 69 | + if (!report) { |
| 70 | + throw new Error( |
| 71 | + 'Unexpected Kosit validator response: missing report element' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + const assessment = report['assessment'] as |
| 76 | + | Record<string, unknown> |
| 77 | + | undefined; |
| 78 | + |
| 79 | + const valid = assessment |
| 80 | + ? 'accept' in assessment |
| 81 | + : this.inferValidityFromResults(report); |
| 82 | + |
| 83 | + const errors: KositValidationMessage[] = []; |
| 84 | + const warnings: KositValidationMessage[] = []; |
| 85 | + |
| 86 | + this.extractMessages(report, errors, warnings); |
| 87 | + |
| 88 | + return { valid, errors, warnings, rawXml }; |
| 89 | + } |
| 90 | + |
| 91 | + private inferValidityFromResults(report: Record<string, unknown>): boolean { |
| 92 | + const scenarioMatched = report['scenarioMatched'] as |
| 93 | + | Record<string, unknown> |
| 94 | + | undefined; |
| 95 | + |
| 96 | + if (!scenarioMatched) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + const steps = scenarioMatched['validationStepResult']; |
| 101 | + const stepArray = Array.isArray(steps) ? steps : steps ? [steps] : []; |
| 102 | + |
| 103 | + return stepArray.every((step: Record<string, unknown>) => { |
| 104 | + const recommendation = step['recommendation'] as string | undefined; |
| 105 | + return recommendation === 'accept'; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + private extractMessages( |
| 110 | + report: Record<string, unknown>, |
| 111 | + errors: KositValidationMessage[], |
| 112 | + warnings: KositValidationMessage[] |
| 113 | + ): void { |
| 114 | + const scenarioMatched = report['scenarioMatched'] as |
| 115 | + | Record<string, unknown> |
| 116 | + | undefined; |
| 117 | + |
| 118 | + if (!scenarioMatched) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const steps = scenarioMatched['validationStepResult']; |
| 123 | + const stepArray = Array.isArray(steps) ? steps : steps ? [steps] : []; |
| 124 | + |
| 125 | + for (const step of stepArray as Record<string, unknown>[]) { |
| 126 | + this.extractStepMessages(step, errors, warnings); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private extractStepMessages( |
| 131 | + step: Record<string, unknown>, |
| 132 | + errors: KositValidationMessage[], |
| 133 | + warnings: KositValidationMessage[] |
| 134 | + ): void { |
| 135 | + const messages = step['message']; |
| 136 | + const messageArray = Array.isArray(messages) |
| 137 | + ? messages |
| 138 | + : messages |
| 139 | + ? [messages] |
| 140 | + : []; |
| 141 | + |
| 142 | + for (const msg of messageArray as Record<string, unknown>[]) { |
| 143 | + const entry: KositValidationMessage = { |
| 144 | + id: String(msg['$id'] ?? msg['id'] ?? ''), |
| 145 | + text: String(msg['#text'] ?? msg['text'] ?? ''), |
| 146 | + location: String(msg['$location'] ?? msg['location'] ?? ''), |
| 147 | + }; |
| 148 | + |
| 149 | + const level = String( |
| 150 | + msg['$level'] ?? msg['level'] ?? '' |
| 151 | + ).toLowerCase(); |
| 152 | + |
| 153 | + if (level === 'warning') { |
| 154 | + warnings.push(entry); |
| 155 | + } else { |
| 156 | + errors.push(entry); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments