Validate for XML schema and returns all the possible failures

Overview

detailed-xml-validator

Validate for XML schema and returns all the possible failures

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" >
                <name pattern="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:
    • pattern: regex

Sample code

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

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

const validator = new Validator(rules, options);
const failures = validator.validate(xmlStringData);
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' }
]
Comments
  • Not validating against a given XSD schema

    Not validating against a given XSD schema

    The module appears to just verify if the XML file is valid XML, I can pass in an XSD schema which it accepts, eg:

    <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.1"> <xs:element name="job-opportunities"> <xs:complexType> <xs:sequence> <xs:element ref="job-opportunity" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="isMigration" type="shortTextInputType"/> <xs:attribute name="isIncremental" type="xs:boolean" default="false"/> <xs:attribute name="datasourceKey" type="shortTextInputType"/> </xs:complexType> </xs:element> <xs:element name="job-opportunity"> <xs:complexType> <xs:sequence> <xs:element name="job-id" type="textInputID" minOccurs="1" maxOccurs="1"/> ...

    And if I remove for eg 'job-id' from one of the input objects (XML fields) then it should fail validation - however it passes validation

    opened by Leigh-M 1
  • node_modules package.json issue

    node_modules package.json issue

    (This module if running would be extremely useful as there are issues with all of the others on npm performing XML validation against a given XSD - if there is a donate page to encourage it's development please post it somewhere happy to donate)

    However, running into a require issue (node 14.15.0 npm v 8.3.0 Ubuntu Linux 21.10) right at the outset - the npm packaging does not look to be set up correctly:

    After npm i detailed-xml-validator

    I see:

    Error: Cannot find module '/home/my-home/projects/some-project/node_modules/detailed-xml-validator/validator.js'. Please verify that the package.json has a valid "main" entry

    package.json has a main entry, but appears to point to: validator.js - you can get this to run correctly (I think) by changing that to: "main": "./src/validator.js"

    opened by Leigh-M 1
  • Make the validator concurrent requests safe

    Make the validator concurrent requests safe

    If someone wants to call validate function multiple times, she needs to create validator instance everytime.

    Currently, validator saves the failures in this.failures. And it can be overridden when the validate() is called multiple times.

    hacktoberfest 
    opened by amitguptagwl 1
  • Return parsed XML equvilant json

    Return parsed XML equvilant json

    A user may need to report failures with real values or additional information. So the parsed XML should be saved in an instance variable so it can be accessed by user for further processing.

    opened by amitguptagwl 1
  • It is validating against rules but not syntax.

    It is validating against rules but not syntax.

    Hello, I am using this in a project and noticed that it is validating against rules written only when XML is valid but considering reallife scenarios people make syntax error. but in that case it is returning undefined.

    opened by gautam-jha 1
  • Supports type

    Supports type

    For the complex XML where some object structures are repeated, we should be able to define the type and use it to define tags of this type;

    Eg

    <?xml version = "1.0"?>
    
    <students nillable="false">
       <:type>
           <name minLength="3" maxLength="10" ></name>
           <subject>
                 <name pattern="math|hindi|english|science|history"></name>
                  <score type="positiveDecimal"></score>
           </subject>
       <:type>
        <student repeatable minOccurs="1">
            <:a>
                <id length="6"></id>
            </:a>
            <firstname type="name" nillable="false"></firstname>
            <lastname type="name" nillable="false"></lastname>
            <nickname type="name" ></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 type="subject" minOccurs="5" maxOccurs="6" ></subject>
            </marks>
        </student>
    </students>
    
    good first issue hacktoberfest 
    opened by amitguptagwl 0
Owner
Natural Intelligence
Natural Intelligence
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)

Ajv JSON schema validator The fastest JSON validator for Node.js and browser. Supports JSON Schema draft-06/07/2019-09/2020-12 (draft-04 is supported

Ajv JSON schema validator 12k Jan 4, 2023
A simple and composable way to validate data in JavaScript (and TypeScript).

A simple and composable way to validate data in JavaScript (and TypeScript). Usage • Why? • Principles • Demo • Examples • Documentation Superstruct m

Ian Storm Taylor 6.3k Jan 9, 2023
jQuery library to validate html forms. compatible with bootstrap v4 and bootstrap v3

jQuery form validation jQuery form validation is a library that helps you to validate your HTML form, it's completable with both Bootstrap 3 and Boots

Bassam Nabriss 33 Jun 10, 2021
Validate properties and well known annotations in your Backstage catalog-info.yaml files.

Backstage entity validator This package can be used as a GitHub action or a standalone node.js module GitHub action Inputs path Optional Path to the c

Roadie 39 Dec 26, 2022
Validate your forms, frontend, without writing a single line of javascript

Parsley JavaScript form validation, without actually writing a single line of JavaScript! Version 2.9.2 Doc See index.html and doc/ Requirements jQuer

Guillaume Potier 9k Dec 27, 2022
The best @jquery plugin to validate form fields. Designed to use with Bootstrap + Zurb Foundation + Pure + SemanticUI + UIKit + Your own frameworks.

FormValidation - Download http://formvalidation.io - The best jQuery plugin to validate form fields, designed to use with: Bootstrap Foundation Pure S

FormValidation 2.8k Mar 29, 2021
[DISCONTINUED] jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code.

jQuery Form Validator [DISCONTINUED] Validation framework that let's you configure, rather than code, your validation logic. I started writing this pl

Victor Jonsson 976 Dec 30, 2022
This Login Form made using React hooks , React Js , Css, Json. This form have 3 inputs, it also validate those inputs & it also having length limitations.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Yogesh Sharma 0 Jan 3, 2022
A library for validate a string using regular expressions.

Fogex Form Regex Quickly and easily check if the content is valid. Installation npm install fogex or yarn add fogex Usage import fogex from 'fogex';

null 5 May 5, 2022
A simple library to validate Mexican CURPs (Personal ID)

Validate CURP A simple and lightweight library to validate Mexican CURPs (Personal ID). Install NodeJS Use NPM: $ npm install --save validate-curp Or

Manuel de la Torre 15 Nov 24, 2022
Schema-Inspector is an JSON API sanitisation and validation module.

Schema-Inspector is a powerful tool to sanitize and validate JS objects. It's designed to work both client-side and server-side and to be scalable wit

null 494 Oct 3, 2022
Convert JSON examples into JSON schema (supports Swagger 2, OpenAPI 3 and 3.1)

json-to-json-schema Convert JSON examples into JSON schema. Supports JSON Schema draft-05 used in Swagger 2.0 and OpenAPI 3.0 and new draft draft-2020

Redocly 9 Sep 15, 2022
TypeScript-first schema validation for h3 and Nuxt applications

h3-zod Validate h3 and Nuxt 3 requests using zod schema's. Install npm install h3-zod Usage import { createServer } from 'http' import { createApp } f

Robert Soriano 48 Dec 28, 2022
Dead simple Object schema validation

Yup Yup is a JavaScript schema builder for value parsing and validation. Define a schema, transform a value to match, validate the shape of an existin

Jason Quense 19.2k Jan 2, 2023
Tiny Validator for JSON Schema v4

Tiny Validator (for v4 JSON Schema) Use json-schema draft v4 to validate simple values and complex objects using a rich validation vocabulary (example

Geraint 1.2k Dec 21, 2022
Schema validation utilities for h3, using typebox & ajv

h3-typebox JSON schema validation for h3, using typebox & ajv. Install # Using npm npm install h3-typebox # Using yarn yarn install h3-typebox # Usi

Kevin Marrec 43 Dec 10, 2022
Codestamp - Stamp and verify your files and contents

A language-agnostic tool for signing and verifying your (codegen'd) files and contents.

Keyan Zhang 4 Jan 26, 2022