mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-06 00:44:57 +05:30
feat(xml): add XML validation to beautification and viewing functions
This commit is contained in:
parent
6b2070b39f
commit
d60890b1a5
2 changed files with 16 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue