npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

detailed-xml-validator

v1.1.0

Published

Validate for XML schema and returns all the possible failures

Downloads

96

Readme

detailed-xml-validator

Validate for XML schema and returns all the possible failures

This module uses it's own rule file which is different than XSD and looks more like XML data file. More features would be added in future versions. Currently, it just ensures frequency, type, range, value length, value pattern and null validations only on top of syntax check done by FXP.

If there is no syntax error, then this module reports all failures and don't exit on first faliure. So you can report all the issues in one go.

Sample Rules file

<?xml version = "1.0"?>

<students nillable="false">
    <student repeatable minOccurs="1">
        <:a>
            <id length="6"></id>
        </:a>
        <firstname minLength="3" maxLength="10" nillable="false"></firstname>
        <lastname minLength="3" maxLength="10" nillable="false"></lastname>
        <nickname minLength="3" maxLength="10"></nickname>
        <email pattern="[a-z0-9][email protected]" nillable="false"></email>
        <age type="positiveInteger" min="9" max="19"></age>
        <contact>
            <phone length="10"></phone>
        </contact>
        <gender nillable="false" ></gender>
        <marks>
            <subject repeatable minOccurs="5" maxOccurs="6" checkBy="subjectValidator">
                <name pattern="math|hindi|english|science|history"></name>
                <!-- <name in="math,hindi,english,science,history"></name> -->
                <score type="positiveDecimal"></score>
            </subject>
        </marks>

    </student>
</students>
  • :a: This is the special tag used to define rules for attributes
  • nillable: By default all the elements are nillable. Set it to false to mark an element mandatory. For lists, if minOccurs is set to 1, it means it can't be nillable.
  • repeatable: A list must have this attribute
    • minOccurs: To validate minimum number of elements in a list
    • maxOccurs: To validate maximum number of elements in a list
  • type: You can define type to put the restriction on their values. Eg positiveInteger can't have negative values. Following types are supported
    • positiveInteger :
    • positiveDecimal :
    • integer :
    • decimal :
    • number :
    • date :
    • string : default type (optional)
    • map : object type (optional)
  • Number type: Following validations can be applied on number type
    • min:
    • max:
  • String type: Following validations can be applied on string type
    • minLength:
    • maxLength:
    • length:
    • in: comma separated string for exact match (from v1.0.0)
    • fixed: exact match (from v1.0.0)
    • pattern: regex match
    • pattern_i: regex (case insensitive)
    • pattern_m: regex (multiline)
    • pattern_im: regex (case insencitive and multiline)
  • checkBy: (from v1.0.0) Give the name of validator that you registered with validator. This validator will be called with an object of nested tags (or value if it is a leaf node) and path.

Sample code

const Validator = require("detailed-xml-validator");

const options = {
    unknownAllow: true,
    boolean: ["true", "false"],
};

const validator = new Validator(rules, options);

validator.register("subjectValidator", (obj, path) => { //From v1.0.0
    //return; //if no error
    //return {} //return an error msg object
})
const failures = validator.validate(xmlStringData);
const originalXmlJsObj = validator.data;
console.log(`Found ${failures.length} issues`);

Sample Response

[
    { code: 'missing', path: 'root.d'} ,
    { code: 'unknown', path: 'root.f'} 
    { code: 'minLength', path: 'root.a[0]', actual: '0', expected: 15 },
    {
        code: 'pattern',
        path: 'root.a[0]',
        actual: '0',
        expected: '[a-z][email protected]'
    },
    { code: 'not a boolean', path: 'root.a[0]', value: 'yes' },
    { code: 'not a integer', path: 'root.f[2]', value: 'acbc' },
    { code: 'max', path: 'root.d', actual: 3.2, expected: 1.5 },
    { code: 'unexpected value in a map', path: 'root.b[1]', value: 'amit' }
]