feat(xml): add XML validation to beautification and viewing functions

This commit is contained in:
AshAnand34
2025-07-08 13:10:39 -07:00
parent 6b2070b39f
commit d60890b1a5
2 changed files with 16 additions and 2 deletions

View File

@@ -1,10 +1,17 @@
import { InitialValuesType } from './types';
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
export function beautifyXml(
input: string,
_options: InitialValuesType
): string {
const valid = XMLValidator.validate(input);
if (valid !== true) {
if (typeof valid === 'object' && valid.err) {
return `Invalid XML: ${valid.err.msg} (line ${valid.err.line}, col ${valid.err.col})`;
}
return 'Invalid XML';
}
try {
const parser = new XMLParser();
const obj = parser.parse(input);

View File

@@ -1,10 +1,17 @@
import { InitialValuesType } from './types';
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
export function prettyPrintXml(
input: string,
_options: InitialValuesType
): string {
const valid = XMLValidator.validate(input);
if (valid !== true) {
if (typeof valid === 'object' && valid.err) {
return `Invalid XML: ${valid.err.msg} (line ${valid.err.line}, col ${valid.err.col})`;
}
return 'Invalid XML';
}
try {
const parser = new XMLParser();
const obj = parser.parse(input);