@xml-tools/validation
TypeScript icon, indicating that this package has built-in type declarations

1.0.16 • Public • Published

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.

Readme

Keywords

Package Sidebar

Install

npm i @xml-tools/validation

Weekly Downloads

1,343

Version

1.0.16

License

Apache-2.0

Unpacked Size

25.7 kB

Total Files

8

Last publish

Collaborators

  • rima.sirich
  • oss_rima_bot
  • shahars