Skip to content

Latest commit

 

History

History

validation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

npm (scoped)

@xml-tools/validation

APIs which assist in implementing Validations / Diagnostics logic for XML.

Installation

With npm:

  • npm install @xml-tools/validation

With Yarn

  • yarn add @xml-tools/validation

Usage

Please see the TypeScript Definitions for full API details.

A simple usage example:

const { parse } = require("xml-tools/parser");
const { buildAst } = require("xml-tools/ast");
const { validate } = require("xml-tools/validation");

const xmlText = `<note>
                     <to>Bill</to>
                 </note>
`;

const { cst, tokenVector } = parse(xmlText);
const xmlDocAst = buildAst(cst, tokenVector);
const issues = validate({
  doc: xmlDocAst,
  validators: {
    element: [
      (node) => {
        if (node.name === "note") {
          const hasFrom = node.subElements.find(
            (subNode) => subNode.name === "from"
          );
          if (hasFrom === undefined) {
            return [
              {
                msg: "A Note Element **must** have a `from` subElement",
                node: node,
              },
            ];
          }
        }
        return [];
      },
    ],
  },
});

console.log(issues[0].msg); // -> "A Note Element **must** have a `from` subElement"
// Issue position can be extracted from the relevant ASTNode.
console.log(issues[0].node.position.endLine); // -> 3

Support

Please open issues on github.

Contributing

See CONTRIBUTING.md.