diff --git a/spec/validator_spec.js b/spec/validator_spec.js index ea07bc14..c282415f 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -129,6 +129,12 @@ describe("XML Validator", function () { }); }); + it("should not validate xml with unexpected closing tag", function () { + validate("", { + InvalidTag: "Closing tag 'rootNode' has not been opened." + }); + }); + it("should validate xml with comment", function () { validate("1val"); }); diff --git a/src/validator.js b/src/validator.js index 11b051b1..3b1b2efb 100644 --- a/src/validator.js +++ b/src/validator.js @@ -103,6 +103,8 @@ exports.validate = function (xmlData, options) { return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i)); } else if (attrStr.trim().length > 0) { return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos)); + } else if (tags.length === 0) { + return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos)); } else { const otg = tags.pop(); if (tagName !== otg.tagName) {