The Website reports a validation error, but the code does not

398 Views Asked by At

I am using the fast-xml-parser, and it trying to write a few test cases, I get a failure on some plain text from the Javascript code. However, access the site's online webpage to try the validator, returns an error.

My XML check

import * as XmlLib from 'fast-xml-parser';
class XmlWrapper {
    public static IsXML(XML: string): boolean | never {
        try {
            const XmlParser: XmlLib.XMLParser = new XmlLib.XMLParser({ allowBooleanAttributes: true });
            const _Result = XmlParser.parse(XML);

            // Just using the validate directly has the same issue
            // const _Result = XmlLib.XMLValidator.validate(XML, { allowBooleanAttributes: true });
            }
        } catch (Exception) {
            console.log(Exception);
            throw new Error('Bad XML');
        }
        return true;
    }
}

My test cases are fairly simple:

import { XmlWrapper } from './xml-wrapper';

describe('XmlWrapper', () => {
    it('should be defined', () => {
        expect(new XmlWrapper()).toBeDefined();
    });

    it.each([
        ['undefined', undefined],
        ['null', null],
        ['empty string', ''],
        ['ABC', 'ABC'],
        ['just <?xml starting tag', '<?xml'],
    ])('should throw an exception when getting %s which is an invalid value.', (_Text, Value) => {
        expect(() => {
            XmlWrapper.IsXML(Value);
        }).toThrowError('Bad XML');
    });

All the tests pass correctly, except the ABC field.

FAIL  src/library/wrappers/xml-wrapper.spec.ts (7.492 s)
XmlWrapper
    √ should throw an exception when getting undefined which is an invalid value. (7 ms)
    √ should throw an exception when getting null which is an invalid value. (3 ms)
    √ should throw an exception when getting empty string which is an invalid value. (3 ms)
    × should throw an exception when getting ABC which is an invalid value. (4 ms)
    √ should throw an exception when getting just <?xml starting tag which is an invalid value. (5 ms)

  ● XmlWrapper › should throw an exception when getting ABC which is an invalid value.
  expect(received).toThrowError(expected)

  Expected substring: "Bad XML"

  Received function did not throw

However, using the fast-xml-parser webpage with just ABC on the left, and then Validate, works. Fast XML Parser web page with just ABC works

1

There are 1 best solutions below

0
On BEST ANSWER

I was able to detect the empty object for the XML validation / parsing by adding a check on the objects keys.

import * as XmlLib from 'fast-xml-parser';
class XmlWrapper {
    public static IsXML(XML: string): boolean | never {
        try {
            const XmlParser: XmlLib.XMLParser = new XmlLib.XMLParser({ allowBooleanAttributes: true });
            const Result = XmlParser.parse(XML);

            if (Object.keys(Result).length) { // Empty structure
                return Result;
            }
            throw new Error('Bad XML');
        } catch (Exception) {
            console.log(Exception);
            throw new Error('Bad XML');
        }
        return true;
    }
}