mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-11-16 18:14:02 +01:00
feat(xml): add XML validation to beautification and viewing functions
This commit is contained in:
@@ -1,10 +1,17 @@
|
|||||||
import { InitialValuesType } from './types';
|
import { InitialValuesType } from './types';
|
||||||
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
|
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
|
||||||
|
|
||||||
export function beautifyXml(
|
export function beautifyXml(
|
||||||
input: string,
|
input: string,
|
||||||
_options: InitialValuesType
|
_options: InitialValuesType
|
||||||
): string {
|
): 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 {
|
try {
|
||||||
const parser = new XMLParser();
|
const parser = new XMLParser();
|
||||||
const obj = parser.parse(input);
|
const obj = parser.parse(input);
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
import { InitialValuesType } from './types';
|
import { InitialValuesType } from './types';
|
||||||
import { XMLParser, XMLBuilder } from 'fast-xml-parser';
|
import { XMLParser, XMLBuilder, XMLValidator } from 'fast-xml-parser';
|
||||||
|
|
||||||
export function prettyPrintXml(
|
export function prettyPrintXml(
|
||||||
input: string,
|
input: string,
|
||||||
_options: InitialValuesType
|
_options: InitialValuesType
|
||||||
): string {
|
): 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 {
|
try {
|
||||||
const parser = new XMLParser();
|
const parser = new XMLParser();
|
||||||
const obj = parser.parse(input);
|
const obj = parser.parse(input);
|
||||||
|
|||||||
Reference in New Issue
Block a user