Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xml-tools/simple-schema

Package Overview
Dependencies
Maintainers
3
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xml-tools/simple-schema

XML Simple Schema

  • 3.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

npm (scoped)

@xml-tools/simple-schema

This package includes three elements:

  • Data structure definition for a basic XML schema represented as a JavaScript object literal.
  • Validation logic to inspect a given XML conforms to a Schema.
  • Content Assist logic that provides suggestions using the Schema information.

Installation

With npm:

  • npm install @xml-tools/simple-schema

With Yarn

  • yarn add @xml-tools/simple-schema

Usage

Please see the TypeScript Definitions for full API details.

  • Defining a Schema:

    const schema = {
      name: "people",
      required: true,
      cardinality: "single",
      attributes: {},
      elements: {
        person: {
          name: "person",
          required: false,
          cardinality: "many",
          attributes: {
            eyeColor: {
              key: "eyeColor",
              required: false,
              value: ["grey", "blue", "green", "red"],
            },
          },
          elements: {
            name: {
              cardinality: "single",
              required: true,
              name: "name",
              attributes: {},
              elements: {},
            },
          },
        },
      },
    };
    
  • A Simple XML Text

    // A Sample XML Text
    const xmlText = `<people>
                           <person eyeColor="violet">
                             <name>Daenerys Targaryen</name>
                           </person>
                        </people>`;
    
  • Performing Validations

    const { parse } = require("@xml-tools/parser");
    const { buildAst } = require("@xml-tools/ast");
    const { validate } = require("@xml-tools/validation");
    const { getSchemaValidators } = require("@xml-tools/simple-schema");
    
    const { cst, tokenVector } = parse(xmlText);
    const xmlDoc = buildAst(cst, tokenVector);
    const schemaValidators = getSchemaValidators(schema);
    const issues = validate({
      doc: xmlDoc,
      validators: {
        attribute: [schemaValidators.attribute],
        element: [schemaValidators.element],
      },
    });
    
    console.log(issues[0].msg); // Expecting one of <grey,blue,green,red> but found <violet>
    console.log(issues[0].position); // { startOffset: 46, endOffset: 53 }
    
  • Using Content Assist APIs

    const { getSuggestions } = require("@xml-tools/content-assist");
    const { getSchemaSuggestionsProviders } = require("@xml-tools/simple-schema");
    
    const schemaSuggestionsProviders = getSchemaSuggestionsProviders(schema);
    
    const suggestions = getSuggestions({
      text: xmlText,
      offset: 47, // within the eyeColor quoted value
      providers: {
        attributeValue: [
          schemaSuggestionsProviders.schemaAttributeValueCompletion,
        ],
        attributeName: [schemaSuggestionsProviders.schemaAttributeNameCompletion],
        elementName: [schemaSuggestionsProviders.schemaElementNameCompletion],
      },
    });
    
    console.log(suggestions[0]); // { text: 'grey', label: 'grey' }
    console.log(suggestions[1]); // { text: 'blue', label: 'blue' }
    

Support

Please open issues on github.

Contributing

See CONTRIBUTING.md.

Keywords

FAQs

Package last updated on 03 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc